public DijkstraSP(EdgeWeightedDigraph G, int s)
 {
     edgeTo = new DirectedEdge[G.VNumber()];
     distTo = new double[G.VNumber()];
     pq     = new IndexMinPQ <double>(G.VNumber());
     for (int v = 0; v < G.VNumber(); v++)
     {
         distTo[v] = Double.PositiveInfinity;
     }
     distTo[s] = 0.0;
     pq.Insert(s, 0.0);
     while (!pq.IsEmpty())
     {
         Relax(G, pq.DelMin());
     }
 }
Example #2
0
 private IndexMinPQ <Double> pq; //有效的横切边
 public PrimMST(EdgeWeightedGraph G)
 {
     edgeTo = new Edge[G.VNumber];
     distTo = new double[G.VNumber];
     marked = new bool[G.VNumber];
     for (int v = 0; v < G.VNumber; v++)
     {
         distTo[v] = Double.PositiveInfinity;
     }
     pq        = new IndexMinPQ <double>(G.VNumber);
     distTo[0] = 0.0;
     pq.Insert(0, 0.0);         //用顶点0和权重0初始化pq
     while (!pq.IsEmpty())
     {
         Visit(G, pq.DelMin()); //将最近的顶点添加到树中
     }
 }