Summary of graph theory algorithm: the solution of secondary short circuit

Source: Internet
Author: User

Solving secondary short-circuit with Dijkstra algorithm

We have learned to use the Dijkstra algorithm to solve the shortest path, but what if we want to solve a single short-circuit of a node? In fact, we can still use the Dijkstra algorithm to solve it.


First of all, to review the principle of the Dijkstra algorithm: first of all nodes to set the shortest distance to infinity, and then make d[0]=0. Next, each time we find the shortest path that has been identified, update the minimum distance from the neighboring node from which it departs. We no longer consider the nodes that have been identified by the shortest distance.


The above is the main process of the Dijkstra algorithm, it is important to note that we no longer consider the " shortest distance has been determined by the node ", do not mistakenly understood as the update of the shortest distance value of the node. Because some of the junction's shortest path may require multiple updates to be finalized. So the question is, how do you know which nodes are the shortest distance is OK? Here we take advantage of a bit of greedy thought, each time the shortest current distance of the node, think it is the most short-circuit is already determined. It can be proved that this is the right thing to do. This is why the optimized version of the Dijkstra algorithm is used for priority_queue reasons.


So back to the topic, how to solve the short-circuit? If we want to solve the short-circuit of the starting S to the end point T, then there are two possible cases: (1) The Shortest Path +d (u,t) of the beginning S to a vertex u. (2) The secondary short-circuit +d (U,T) of the starting point to a vertex u. Therefore, for each node, we record not only the shortest distance, but also a short distance, and then with a similar to the Dijkstra algorithm constantly update these two distances can be found in the secondary short-circuit.

#define N 100000+10#define INF 100000000typedef pair<int, int>p;int n,r;struct edge{int to, cost;}; Vector<edge>g[n];int Dist[n], dist2[n];void addedge (int u, int v,int w) {G[u].push_back (edge{V, w}); G[v].push_back (edge{u, w});} void Solve () {priority_queue<p, vector<p>, greater<p> >q;fill (Dist, dist + n, INF); Fill (dist2, Dist2 + N , INF);d ist[0] = 0;q.push (p (0, 0)), while (!q.empty ()) {p u = q.top (); Q.pop (); int v = u.second, d = u.first;if (Dist2[v] &l T D) continue;//take out not a short distance, discard for (int i = 0; i < g[v].size (); i++) {edge&e = G[v][i];int D2 = d + e.cost;if (dist[e.to]& GT;D2)//update the shortest distance {swap (dist[e.to], D2); Q.push (P (dist[e.to], e.to));} if (Dist2[e.to]>d2&&dist[e.to] < D2)//update minor short distance {dist2[e.to] = D2;q.push (P (dist2[e.to], e.to));}}} printf ("%d\n", Dist2[n-1]);}


Summary of graph theory algorithm: the solution of secondary short circuit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.