Esempio n. 1
0
        public void TestMaxFlowMinCostLarge()
        {
            var inputStream  = new StreamReader(@"..\..\TestData\mincostmaxflow.in");
            var answerStream = new StreamReader(@"..\..\TestData\mincostmaxflow.ans");

            var a = Array.ConvertAll <string, int>(inputStream.ReadLine().Split(' '), int.Parse);

            while (a[0] > 0)
            {
                var b = Array.ConvertAll <string, int>(answerStream.ReadLine().Split(' '), int.Parse);
                int expectedMaxFlow = b[0], expectedMinCost = b[1];

                int         n = a[0], m = a[1], source = a[2], sink = a[3];
                CostNetwork cn = new CostNetwork();
                for (int i = 0; i < m; i++)
                {
                    a = Array.ConvertAll <string, int>(inputStream.ReadLine().Split(' '), int.Parse);
                    int src = a[0], dest = a[1], cap = a[2], cost = a[3];
                    if (src >= n || dest >= n)
                    {
                        throw new Exception();
                    }
                    cn.AddEdge(src, dest, cap, cost);
                }
                var ans     = cn.MaxFlowMinCost(source, sink);
                var maxFlow = ans[0];
                var minCost = ans[1];

                Assert.AreEqual(expectedMaxFlow, maxFlow);
                Assert.AreEqual(expectedMinCost, minCost);

                a = Array.ConvertAll <string, int>(inputStream.ReadLine().Split(' '), int.Parse);
            }
        }
Esempio n. 2
0
        public void TestMaxFlowMinCost()
        {
            CostNetwork network = new CostNetwork();

            /*network.AddEdge(0, 1, 15, 4);
             * network.AddEdge(0, 2, 8, 4);
             * network.AddEdge(1, 2, 1000, 2);
             * network.AddEdge(1, 3, 4, 2);
             * network.AddEdge(1, 4, 10, 6);
             * network.AddEdge(2, 3, 15, 1);
             * network.AddEdge(3, 4, 5, 3);
             * network.AddEdge(3, 4, 1000, 2);
             * network.AddEdge(4, 2, 4, 1);
             * network.AddEdge(3, 5, 1000, 0);
             * network.AddEdge(4, 5, 1000, 0);*/
            /*
             *                      network.AddEdge(0, 1, 3, 5);
             *                      network.AddEdge(0, 2, 8, 6);
             *                      network.AddEdge(1, 2, 4, 2);
             *                      network.AddEdge(1, 3, 6, 70);
             *                      network.AddEdge(2, 4, 7, 9);
             *                      network.AddEdge(3, 5, 5, 20);
             *                      network.AddEdge(4, 1, 5, 1);
             *                      network.AddEdge(4, 3, 2, 9);
             *                      network.AddEdge(4, 5, 8, 15);*/

            int    n = 20;
            Random r = new Random();

            for (int i = 0; i < 100; i++)
            {
                int x    = r.Next(0, n);
                int y    = r.Next(0, n);
                int cap  = r.Next(1, 100);
                int cost = 100 - i;                // r.Next(1, 100);
                if (x != y)
                {
                    network.AddEdge(x, y, cap, cost);
                }
            }
            network.MaxFlowMinCost(0, n - 1);
        }