SPFA is adding a pruning (ignoring nodes that don't need to be considered)
1. Start Join queue O (1)
2. Take the nearest point in the queue from the beginning I O (1) or O (1) = O (1)
2.1 Determine if I is the end point if it is over O (1)
2.2 Determine whether I is worth further calculation (at present I to the starting distance is known I to the smallest of the starting distance) O (1) or O (1) = O (1)
2.2.1 If not worth returning 2 O (1)
2.2.2 If it is worth recording the current distance as the minimum distance and continue O (1)
3. Traverse the adjacent node of I J. O (m/n)
3.1 Determine if J is worth joining the queue (the current J-to-start distance is the smallest of known J-to-start distances) O (log (len (prority_queue)))
3.1.1 If not worth, continue 3 traversal O (1)
3.1.2 If it is worth joining the queue record the current distance is the minimum distance and continue O (log (len (prority_queue)))
4 returns 2;
Two or three to execute up to n times
Isn't this dijstra+ pruning?
Did you write it wrong?
#include <iostream> #include <vector> #include <algorithm> #include <cstring> #include < queue>using namespace Std;const int max_point = 100100;struct node{int target;int Dist;} Node,node_t;struct Cmp{bool operator () (node N1,node other) {return n1.dist>other.dist;}}; Vector<node> edge[max_point];int Dis[max_point];int Main () {priority_queue<node,vector<node>,cmp > pri_que; int n,m,s,t; cin>>n>>m>>s>>t; s--, t--; memset (dis,0x0f,sizeof (dis)); int v,u,d; for (int i=0;i<m;i++) {cin>>v>>u>>d; V--, u--;node.target = u;node.dist = d;edge[v].push_back (node); node.target=v;edge[u].push_back (node); }node.target = S;node.dist = 0;pri_que.push (node); while (true) {node = Pri_que.top (); Pri_que.pop (); if (dis[node.target]<node.dist) {continue;} else dis[node.target] = node.dist; if (Node.target = = t) {break; } for (int i=0;i<edge[node.target].sizE (); i++) {int next_tar = Edge[node.target][i].target; int next_dis = edge[node.target][i].dist; int new_dis = next_dis+node.dist; if (New_dis>dis[next_tar]) continue; else{node_t.target = Next_tar; Node_t.dist = New_dis; Dis[next_tar]=new_dis; Pri_que.push (node_t); }}} cout<<dis[t]<<endl; return 0;}
#1093: Shortest Path • Three: SPFA algorithm Hihocoder SPFA