Пример #1
0
    void Start()
    {
        int s = 5;
        EdgeWeightedDigraph G = new EdgeWeightedDigraph(txt);

        // find shortest path from s to each other vertex in DAG
        AcyclicSP sp = new AcyclicSP(G, s);

        for (int v = 0; v < G.V(); v++)
        {
            if (sp.hasPathTo(v))
            {
                string str = (s + " to " + v + "   ditance=" + sp.DistTo(v));
                foreach (DirectedEdge e in sp.pathTo(v))
                {
                    str += ("     " + e);
                }
                print(str);
            }
            else
            {
                print(s + " to " + v + "         no path\n");
            }
        }
    }
    /**/ public static void main(string[] strarr)
    {
        In  i  = new In(strarr[0]);
        int i2 = Integer.parseInt(strarr[1]);
        EdgeWeightedDigraph edgeWeightedDigraph = new EdgeWeightedDigraph(i);
        AcyclicSP           acyclicSP           = new AcyclicSP(edgeWeightedDigraph, i2);

        for (int j = 0; j < edgeWeightedDigraph.V(); j++)
        {
            if (acyclicSP.hasPathTo(j))
            {
                StdOut.printf("%d to %d (%.2f)  ", new object[]
                {
                    Integer.valueOf(i2),
                    Integer.valueOf(j),
                    java.lang.Double.valueOf(acyclicSP.distTo(j))
                });
                Iterator iterator = acyclicSP.pathTo(j).iterator();
                while (iterator.hasNext())
                {
                    DirectedEdge obj = (DirectedEdge)iterator.next();
                    StdOut.print(new StringBuilder().append(obj).append("   ").toString());
                }
                StdOut.println();
            }
            else
            {
                StdOut.printf("%d to %d         no path\n", new object[]
                {
                    Integer.valueOf(i2),
                    Integer.valueOf(j)
                });
            }
        }
    }