Topic Link http://acm.hdu.edu.cn/showproblem.php?pid=2680
The main idea is that a person can start from multiple starting points and see the shortest possible end point. Only the same ideas can be used and hdu2066, for each starting point can be seen as the beginning of the most points with a weight of 0 of the edge. You can do 0 points at the beginning of the note. So you can use the single source shortest. Have not used SPFA before, today to use a bit.
The approximate process of the algorithm is to use a queue for maintenance. Initially, the source is added to the queue. Each time an element is taken out of the queue, and all points adjacent to him are relaxed, and if an adjacent point relaxes successfully, it is enqueued. The algorithm ends until the queue is empty.
This algorithm, which is simply said to be the bellman-ford of the queue optimization, utilizes the feature invented by each point not to update too many times.
Summing up, SPFA is based on the bellmanford of a queue to optimize, the idea is that if the shortest distance of the point is updated and he is not in the queue to join the queue, if you do not need to repeat the queue to join. Because it was the last value that was updated when he was taken out. This avoids repeated operations over and over again. The inverse of the bellman-ford is not optimized, for each side of the n-1 times to relax, so as to try to update the shortest distance of the end of each edge, and SPFA directly from the beginning of the starting point to find the adjacent side, determine whether it can be updated, and determine whether to join the queue (has not added in the queue), This reduces the number of updates.
Example HDU 2680
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Vector> #include <queue>using namespace std; #define M 1009#define INF 0x3f3f3f3fstruct edge{int to,w;//Save Edge Information, Including the end of the edge and weight};int dis[m]; Estimation of the shortest distance (the shortest distance at the current point) bool Inq[m]; Mark whether the point is in the queue vector<edge> g[m]; With a vector save, g[i] represents the information of all sides with I as the starting point int n,m,ee;void SPFA (int u) {for (int i = 0;i <= n;i++)//initialization {Dis[i] = I NF; Initialize the estimate to inf inq[i] = false; Initialize to not in queue} Dis[u] = 0; The estimate of the starting point is directly 0 inq[u] = true; Join the queue and Mark Queue<int> Q; Q.push (U); while (!q.empty ()) {u = Q.front (); Inq[u] = false; Q.pop (); for (int i = 0;i < G[u].size (); i++) {int v = g[u][i].to;//Find out the end of this edge int w = G[U][I].W; This edge corresponds to the right value if (Dis[v] > Dis[u]+w)//If the shortest distance of the end point is the shortest distance from the beginning plus the weight of the edge then update {DIS[V] = Dis[u ]+w; if (!inq[v])//If the minimum distance of the V point is updated and is not in the queue,To join the queue. {//Otherwise you do not need to repeat the join queue to increase unnecessary operations. INQ[V] = true; Join the queue and Mark Q.push (v); }}}}}int main () {while (scanf ("%d%d", &n,&m,&ee) ==3) {for (int i = 0;i <= n;i++)//emptying the vector avoids multiple kase interactions g[i].clear (); for (int i = 0;i < m;i++) {int a,b,c; scanf ("%d%d%d", &a,&b,&c); Edge e; e.to = B;E.W = C; G[a].push_back (e); E.to = A; G[b].push_back (e); It is said that the road from A to B is not bidirectional. } int s; scanf ("%d", &s); for (int i = 0;i < s;i++) {int A; scanf ("%d", &a); Edge e; E.to = A; E.W = 0; G[0].push_back (e); Set the home-to-start weight to 0//e.to = 0; G[a].push_back (e); Whether there is a way from the beginning to the home does not matter, because think about the way the beginning to the beginning is also 0, so does not affect the result} SPFA (0); printf ("Debug-----%d\n ", Dis[ee]); if (dis[ee]==inf) printf (" -1\n"); else printf ("%d\n", Dis[ee]); } return 0;}
HDU 2680 Shortest Circuit basic problem SPFA Realization Algorithm Summary