Exemplo n.º 1
0
Arquivo: CPM.cs Projeto: zzhi/Algs4Net
        public static void MainTest(string[] args)
        {
            TextInput StdIn = new TextInput();
            // number of jobs
            int N = StdIn.ReadInt();

            // source and sink
            int source = 2 * N;
            int sink   = 2 * N + 1;

            // build network
            EdgeWeightedDigraph G = new EdgeWeightedDigraph(2 * N + 2);

            for (int i = 0; i < N; i++)
            {
                double duration = StdIn.ReadDouble();
                G.AddEdge(new DirectedEdge(source, i, 0.0));
                G.AddEdge(new DirectedEdge(i + N, sink, 0.0));
                G.AddEdge(new DirectedEdge(i, i + N, duration));

                // precedence constraints
                int M = StdIn.ReadInt();
                for (int j = 0; j < M; j++)
                {
                    int precedent = StdIn.ReadInt();
                    G.AddEdge(new DirectedEdge(N + i, precedent, 0.0));
                }
            }

            // compute longest path
            AcyclicLP lp = new AcyclicLP(G, source);

            // print results
            Console.WriteLine(" job   start  finish");
            Console.WriteLine("--------------------");
            for (int i = 0; i < N; i++)
            {
                Console.Write("{0,4} {1,7:F1} {2,7:F1}\n", i, lp.DistTo(i), lp.DistTo(i + N));
            }
            Console.Write("Finish time: {0,7:F1}\n", lp.DistTo(sink));
        }
Exemplo n.º 2
0
        public static void MainTest(string[] args)
        {
            EdgeWeightedDigraph G = new EdgeWeightedDigraph(new TextInput(args[0]));

            int       s  = int.Parse(args[1]);
            AcyclicLP lp = new AcyclicLP(G, s);

            for (int v = 0; v < G.V; v++)
            {
                if (lp.HasPathTo(v))
                {
                    Console.Write("{0} to {1} ({2:F2})  ", s, v, lp.DistTo(v));
                    foreach (DirectedEdge e in lp.PathTo(v))
                    {
                        Console.Write(e + "   ");
                    }
                    Console.WriteLine();
                }
                else
                {
                    Console.Write("{0} to {0}         no path\n", s, v);
                }
            }
        }