**Description**

The Dijkstra algorithm is an effective algorithm to deal with the shortest path of single source, but it is limited to the non-negative weight of the edge, if the weighted value is negative, the Dijkstra algorithm will fail, and the shortest path can be wrong.

At this time, we need to use other algorithms to solve the shortest path, the Bellman-ford algorithm is one of the most commonly used.

**Applicable Conditions & Scope:**

Single Source Shortest path (from source point s to all other vertex V);

The direction graph & the non-direction graph (u,v), (V,u) belongs to the direction graph of the edge set E;

Side right can be negative (if negative power loop output error);

**Thought:**

We specify that the node has a key value, and the key value records the minimum distance from the start node to this node, and each node has a P pointer pointing to his predecessor node. Here we specify that an operation is called a relaxation operation, and our algorithm is ultimately based on this operation. The relaxation operation is to update the value of key.

Node B has a key value of 8, which indicates that the shortest estimated distance from the start node to the B node is 8, and the key value of Node A is 3, which means that the shortest estimate from the start node to the a node is 3, and when we find this edge, the distance from A to B is closer, so we go to update the key value At the same time, the precursor node of B is set to a. This process is a relaxation operation.

The Bellman-ford algorithm we are talking about is the simplest algorithm, which is to cycle through each edge from the start node to relax the operation. The last path you get is the shortest path. Process

**Algorithm steps:**

1. Initialize: The shortest distance estimate of all vertices except source point d[v]←+∞, d[s]←0;

2. Iterative solution: Repeatedly to the edge set E each edge of the relaxation operation, so that the minimum distance of each vertex v in the vertex set V of the shortest range estimate to gradually approximate its shortest distance; (run |v|-1 times)

3. Test the negative power loop: Determine whether the two endpoints of each edge in the edge set E converge. If there are non-convergent vertices, the algorithm returns false, indicating that the problem is not solved, otherwise the algorithm returns true, and the shortest distance from Vertex v that the source point can reach is saved in d[v].

**Code:**

#include <iostream>#include<cstdio>using namespacestd;#defineMAX 0x3f3f3f3f#defineN 1010intNodenum, Edgenum, original;//point, side, beginningtypedefstructEdge//side{ intu, v; intCost ;} Edge; Edge Edge[n];intDis[n], pre[n];BOOLBellman_ford () { for(inti =1; I <= nodenum; ++i)//initialization, the starting point itself is assigned a value of 0, the remaining assignment is the maximumDis[i] = (i = = original?)0: MAX); for(inti =1; I <= Nodenum-1; ++i) for(intj =1; J <= Edgenum; ++j)if(DIS[EDGE[J].V] > dis[edge[j].u] + edge[j].cost)//relaxation (order must not be reversed){DIS[EDGE[J].V]= Dis[edge[j].u] +Edge[j].cost; PRE[EDGE[J].V]=edge[j].u; } BOOLFlag =1;//determine if a negative power loop is included for(inti =1; I <= edgenum; ++i)if(DIS[EDGE[I].V] > dis[edge[i].u] +edge[i].cost) {Flag=0; Break; } returnFlag;}voidPrint_path (intRoot//Print the shortest path (reverse){ while(Root! = Pre[root])//precursor{printf ("%d-->", Root); Root=Pre[root]; } if(Root = =Pre[root]) printf ("%d\n", root);}intMain () {scanf ("%d%d%d", &nodenum, &edgenum, &original);//Enter the starting point, the general starting point is set at 1Pre[original] = original;//In order to output the shortest circuit, the precursor is itself for(inti =1; I <= edgenum; ++i) {scanf ("%d%d%d", &edge[i].u, &EDGE[I].V, &edge[i].cost);//a map of the direction } if(Bellman_ford ())//If there is no negative right for(inti =1; I <= nodenum; ++i)//Shortest path per point{printf ("%d\n", Dis[i]); printf ("Path:"); Print_path (i); } Elseprintf ("Have negative circle\n"); return 0;}

Bellman-ford algorithm of shortest circuit