// 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]); } }
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]); } } }
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]); } }
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]); } } }
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]); } } } }