Example #1
0
 private void visit(EdgeWightedGraph g, Int32 v)
 {
     marked[v] = true;
     foreach (Edge e in g.Adj(v))
     {
         int w = e.Other(v);
         if (marked[w])
         {
             continue;//访问过了
         }
         if (e.Weight < disTo[w])
         {
             //从MST到w的距离变短了。最佳边变成e
             edgeTo[w] = e;
             disTo[w]  = e.Weight;
             if (pq.Contains(w))
             {
                 pq.Change(w, disTo[w]);
             }
             else
             {
                 pq.Insert(w, disTo[w]);
             }
         }
     }
 }
 private void Relax(DirectedWeightedGraph g, int v)
 {
     foreach (DirectedEdge edge in g.GetEdge(v))
     {
         int w = edge.End;
         //如果存在放松的条件
         if (disTo[w] > disTo[v] + edge.Weight)
         {
             //存在,需要更新
             disTo[w]  = disTo[v] + edge.Weight;
             edgeTo[w] = edge;
             //重新累计从 s 到某个点的最短距离。这个距离只有可能减小
             if (pq.Contains(w))
             {
                 pq.Change(w, disTo[w]);
             }
             else
             {
                 pq.Insert(w, disTo[w]);
             }
         }
     }
 }