Beispiel #1
0
 private void Visit(EdgeWeightedGraph G, int v)
 {
     marked[v] = true;
     foreach (Edge e in G.Adj(v))
     {
         int w = e.Other(v);
         if (marked[w])
         {
             continue;              //v-w失效
         }
         if (e.Weight < distTo[w])
         {                         //连接w和树的最佳边Edge变为e
             edgeTo[w] = e;        //保存w和最小生成树相连的边
             distTo[w] = e.Weight; //即w和最小生成树最近的距离为e.Weight
             if (pq.Contains(w))
             {
                 pq.Change(w, distTo[w]);
             }
             else
             {
                 pq.Insert(w, distTo[w]);
             }
         }
     }
 }//将顶点v添加到树中,更新数据
 private void Relax(EdgeWeightedDigraph G, int v)
 {
     foreach (DirectedEdge e in G.Adj(v))
     {//  v->w   e:v和w相连的边
         int w = e.To();
         if (distTo[w] > distTo[v] + e.Weight())
         {// 起点到达w点的路径 > 起点到达v的路径+e的权重
             distTo[w] = distTo[v] + e.Weight();
             edgeTo[w] = e;
             if (pq.Contains(w))
             {
                 pq.Change(w, distTo[w]);
             }
             else
             {
                 pq.Insert(w, distTo[w]);
             }
         }
     }
 }