コード例 #1
0
ファイル: QuestionB.cs プロジェクト: terry-u16/AtCoder
        public override IEnumerable <object> Solve(TextReader inputStream)
        {
            var nmp        = inputStream.ReadIntArray();
            var nodesCount = nmp[0];
            var edgesCount = nmp[1];
            var penalty    = nmp[2];

            var graph = new WeightedGraph(nodesCount, Enumerable.Repeat(0, edgesCount).Select(_ =>
            {
                var abc = inputStream.ReadIntArray();
                var a   = abc[0] - 1;
                var b   = abc[1] - 1;
                var c   = abc[2];
                return(new WeightedEdge(a, b, penalty - c));
            }));

            var bellmanFord = new BellmanFord <BasicNode, WeightedEdge>(graph);

            var result             = bellmanFord.GetDistancesFrom(new BasicNode(0));
            var distances          = result.Item1;
            var negativeCycleNodes = result.Item2;

            if (negativeCycleNodes[nodesCount - 1])
            {
                yield return(-1);
            }
            else
            {
                yield return(Math.Max(-distances[nodesCount - 1], 0));
            }
        }
コード例 #2
0
        public override IEnumerable <object> Solve(TextReader inputStream)
        {
            var(nodeCount, edgeCount, penalty) = inputStream.ReadValue <int, int, int>();
            var graph = new WeightedGraph(nodeCount);

            for (int i = 0; i < edgeCount; i++)
            {
                var(from, to, coin) = inputStream.ReadValue <int, int, int>();
                from--;
                to--;
                graph.AddEdge(new WeightedEdge(from, to, penalty - coin));
            }

            var bf = new BellmanFord <BasicNode, WeightedEdge>(graph);

            var(distances, negativeCycles) = bf.GetDistancesFrom(new BasicNode(0));

            if (negativeCycles[^ 1])
コード例 #3
0
ファイル: QuestionA.cs プロジェクト: terry-u16/AtCoder
        public override IEnumerable <object> Solve(TextReader inputStream)
        {
            var nm         = inputStream.ReadIntArray();
            var nodesCount = nm[0];
            var edgesCount = nm[1];

            var bellmanFord = new BellmanFord <BasicNode, WeightedEdge>(Enumerable.Repeat(0, edgesCount)
                                                                        .Select(_ => LoadEdge(inputStream.ReadIntArray())), nodesCount);
            var result             = bellmanFord.GetDistancesFrom(new BasicNode(0));
            var distances          = result.Item1;
            var negativeCycleNodes = result.Item2;

            if (negativeCycleNodes[nodesCount - 1])
            {
                yield return("inf");
            }
            else
            {
                yield return(-distances[nodesCount - 1]);
            }
        }