Beispiel #1
0
 // relax edge e and update pq if changed
 private void relaxC(Route r, int v) {
   City city2 = r.other(cities[v]);
   int w = city2.id() - 1;
   if (costTo[w] > costTo[v] + r.price()) {
     costTo[w] = costTo[v] + r.price();
     edgeTo[w] = r;
     if (costPQ.contains(w)) costPQ.change(w, costTo[w]);
     else costPQ.insert(w, costTo[w]);
   }
 }
Beispiel #2
0
	private void relax(EdgeWeightedDigraph G ,int v){
		for(DirectedEdge e:G.adj(V)){
			int w = e.to();
			if(distTo[w]>distTo[v]+e.weight()){
				distTo[w] = distTo[v]+e.weight();
				edgeTo[w] = e;
				if(pq.contains(w)) pq.change(w,distTo[w]);
				else pq.insert(w,distTo[w]);
			}
		}
	}
Beispiel #3
0
 private void relaxD(Route r, int v) {
   // relax edge e and update pq if changed
   City city2 = r.other(cities[v]);
   int w = city2.id() - 1;
   if (distTo[w] > distTo[v] + r.distance()) {
     distTo[w] = distTo[v] + r.distance();
     edgeTo[w] = r;
     if (pq.contains(w)) pq.change(w, distTo[w]);
     else pq.insert(w, distTo[w]);
   }
 }
Beispiel #4
0
 public void visit(int v) {
   marked[v] = true;
   for (Edge e : G.adj(v)) {
     int w = e.other(v);
     if (marked[w]) continue;
     if (disTo[w] > e.weight()) {
       disTo[w] = e.weight();
       if (!pq.contains(w)) {
         pq.insert(w, e.weight());
       } else pq.changeKey(w, e.weight());
       edgeTo[w] = v;
     }
   }
 }
Beispiel #5
0
 private void visit(EdgeWeightedGraph G, int v) // Add v to tree; update data structures.
     {
   marked[v] = true;
   for (Edge e : G.adj(v)) {
     int w = e.other(v);
     if (marked[w]) continue; // v-w is ineligible.
     if (e.weight() < distTo[w]) // Edge e is new best connection from tree to w.
     {
       edgeTo[w] = e;
       distTo[w] = e.weight();
       if (pq.contains(w)) pq.change(w, distTo[w]);
       else pq.insert(w, distTo[w]);
     }
   }
 }
Beispiel #6
0
 private void scan(int v) {
   marked[v] = true;
   for (Route r : adj[v]) {
     int w = r.other(cities[v]).id() - 1; // index of other city in route
     if (marked[w]) {
       continue; // v-w is obsolete edge
     }
     if (r.distance() < distTo[w]) {
       distTo[w] = r.distance();
       edgeTo[w] = r;
       if (pq.contains(w)) {
         pq.change(w, distTo[w]);
       } else {
         pq.insert(w, distTo[w]);
       }
     }
   }
 }