1. Algorithmic thinking
Bellman-ford algorithm time complexity is high, in Bellman-ford need to recursive n times, each recursive need to scan all sides, in the process of recursive n times, many judgments are superfluous, so consider using queue optimization, reduce unnecessary judgment, This algorithm is called SPFA (shortest Path Faster algorithm)
The approximate process of the SPFA algorithm is to use a queue for maintenance, initially the source point into the queue, each time a vertex from the queue, and all its neighboring nodes to relax, if a vertex slack successfully, then queue it, repeat the process until the queues are empty. Time complexity in O (Km) (usually K is about 2) a vertex can be queued multiple times, but if there are vertices more than n times, there is a negative ring, you should return the existence of negative ring information
2. Algorithmic processes
In the SPFA algorithm, you can also use the Dist array to represent the shortest-path length, the paths array saves the route, you also need to set the CNT array record number of times, the VIS array record is currently in the queue
(1). Take out the queue head node U, scan each edge from the vertex u, set the end point of each edge to V, and the weight of the edge to W (U, v). If Dist[u] + W < dist[v], then DIST[V] is modified to Dist[u] + w<u, v>. Modify Path[v] = u, if vertex v is not in the queue, you also need to add V to the queue and the number of queues to add one. If the above conditions are not established, do not do any processing
(2). Repeat 1 until the queue is empty or a vertex is enqueued more than n
3. Algorithm implementation
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <cmath>6#include <queue>7#include <stack>8#include <map>9#include <Set>Ten#include <sstream> One using namespacestd; AtypedefLong Longll; - Const intMAXN = ++Ten; - Const intINF =1<< -; the intT, N, m, cases; - structEdge - { - intu, V, W; + Edge () {} -Edge (intUintVintW): U (U), V (v), W (w) {} + }; Avector<edge>edges;//save every side of the line atvector<int>Map[maxn];//G[i] This vector is the subscript of all sides in the edges that start with I - voidInitintN) - { - for(inti =0; I <= N; i++) map[i].clear (); - edges.clear (); - } in voidAddedge (intUintVintW) - { toEdges.push_back (Edge (U, V, W));//Note that there are two edges to be stored in the graph . +m =edges.size (); -Map[u].push_back (M-1); the } * voidFind (intU//traverse all sides with u as the starting point $ {Panax Notoginseng for(inti =0; I < map[u].size (); i++) - { theEdge&e =Edges[map[u][i]]; + //use E to traverse all edges starting with u A } the } + intCNT[MAXN]; - BOOLVIS[MAXN]; $ intD[MAXN], PATH[MAXN]; $ BOOLSPFA (intu) - { -queue<int>Q; thememset (Vis,0,sizeof(VIS));//Initialize -memset (CNT,0,sizeof(CNT));Wuyimemset (Path,-1,sizeof(path)); the for(inti =0; I < n; i++) D[i] =INF; -D[u] =0; WuVis[u] =1;//Mark into queue - q.push (u); About while(!q.empty ()) $ { - intU =Q.front (); - Q.pop (); -Vis[u] =0;//clear the Enter queue flag A for(inti =0; I < map[u].size (); i++) + { theedge& e =Edges[map[u][i]]; - if(D[u] < INF && D[E.V] > D[u] +E.W) $ { theD[E.V] = D[u] +E.W; thePATH[E.V] = Map[u][i];//Path is the subscript of the edge, so that you can find the previous point and the path of each road through the edge, if stored with the adjacency matrix, you can directly save the node U the if(!VIS[E.V]) the { - Q.push (E.V); inVIS[E.V] =1; the if(++CNT[E.V] > N)return true;//the number of incoming teams is greater than N, indicating the existence of negative loops the } About } the } the } the for(inti =0; I < n; i++) + { - if(i = = u)Continue; theprintf"The distance from%d to%d is:%2d", u, I, d[i]);Bayistack<int>q;//The number of the edge is stored the intx = i;//X is all the points on the path the while(path[x]! =-1) - { - Q.push (x); thex = edges[path[x]].u;//x becomes the beginning of this edge the } thecout<<u; the while(!q.empty ()) - { thecout<<" -"<<q.top (); the Q.pop (); the }94cout<<Endl; the } the return false; the }98 intMain () About { - intC;101CIN >> N >>C;102 intu, V, W;103 for(inti =0; I < C; i++)104 { thecin >> u >> v >>W;106 Addedge (U, V, W);107 }108 if(SPFA (0)) cout<<"there is a negative ring"<<Endl;109 Elsecout<<"there is no negative ring"<<Endl; the return 0;111}
Improved---SPFA Algorithm for Bellman-ford algorithm