Http://acm.sdut.edu.cn/sdutoj/problem.php? Action = showproblem & problemid = 2894
Let's talk about bermanford (refer to others)
BF is the side of the operation, Dijkstra is the point of operation, n vertices of the most short circuit is the N-1 side, so the need to cycle N-1 times
1. Initialization
2. Iterative Solution: Repeated relaxation operation on each side of the edge set, so that each vertex in vertex set v vde shortest distance gradually approaches its shortest distance, run the V-1
3: Check the negative weight loop: judge whether the two endpoints of each edge in the edge set converge. If there are unthin face vertices, false is returned, indicating that the problem is not resolved. Otherwise, the algorithm returns true, and the shortest distance of vertex v reachable from the source point is stored in D [v.
4. descriptive proof: The editor first points out that any shortest path in the graph can neither contain a negative weight loop nor a positive weight loop. Therefore, it can contain at most | v |-1 edge. Second, if there is a shortest path for all vertices that can be reached from the Source Vertex s, these Shortest Paths constitute a shortest path tree with s as the root. The iterative relaxation operation of the Bellman-Ford algorithm is actually the process of generating the shortest path tree layer by layer based on the vertex distance S. When each side is relaxed for 1st times, the branches starting from S and whose layers are mostly 1 are generated. That is to say, the shortest path of those vertices associated with at most one edge of S is found. When every side is relaxed for 2nd times, 2nd layers of branches are generated, that is to say, we have found the shortest path of those vertices connected by two edges ....... Because the shortest path can only contain | v |-1 edge, you only need to loop | v |-1 times. Each time a relaxation operation is performed, a vertex on the shortest path tree reaches its shortest distance, and the shortest distance value of the vertex on the shortest path will remain unchanged and will not be affected by subsequent relaxation operations. (However, we need to judge relaxation every time. This wastes a lot of time. How can we optimize it? Is pure optimization feasible ?) Note: The above is only valid for the right image. If the negative weight is not necessarily the first time, the shortest path can be determined and related to the edge sequence. If no negative weight loop exists, the shortest path tree height can only be | v |-1, so after a maximum of | v |-1 relaxation operations, all vertices reachable from S will surely find the shortest distance. If d [v] is still + ∞, it indicates that it is not reachable from S to v. If there is a negative weight loop, the | v | times relaxation operation will still succeed. In this case, the vertices on the negative weight loop will not converge. [1]
The bellman-Ford algorithm can be roughly divided into three parts:
First, Initialize all vertices. Save a value for each vertex, indicating the distance from the origin to this vertex. Set the value of the origin to 0, and set the value of other vertices to infinity (indicating that the origin cannot be reached ).
Second, perform a loop. The subscripts of the loop are from 1 to n-1 (N is equal to the number of vertices in the graph ). In the loop, traverse all edges for relaxation calculation.
Third, traverse all the edges (u, v) on the way to determine whether such a situation exists:
D (v)> d (u) + W (u, v)
The return value is false, indicating that there is a loop with a negative permission from the source point.
The reason for the third part is that if there is a loop with the right to reach from the source point as negative. Therefore, the shortest path cannot be obtained because it cannot be converged.
Consider the following figure:
After the first traversal, the value of point B is changed to 5, and the value of point C is changed to 8. At this time, pay attention to the edge with a weight of-10 and the existence of this edge, the value of vertex A changes to-2. (8 +-10 =-2)
After the second traversal, the value of point B changes to 3, point C changes to 6, and point a changes to-4. It is precisely because there is a negative edge in the loop that the values of each vertex keep decreasing after each traversal.
Let's look back at the third part of the Bellman-Ford algorithm. traverse all edges and check whether d (v)> d (u) + W (u, v) exists ). Because the number of cycles in the second part is fixed, if convergence is not possible, it can be checked out in the third part. For example
In this case, the value of vertex A is-2, the value of vertex B is 5, and the weight of edge AB is 5, 5,>-2 + 5. Check that this edge does not converge.
Therefore, the bellman-Ford algorithm can solve the single-source shortest path problem of an edge with a negative number in the graph.
Http://blog.csdn.net/u012860063/article/details/24492003
// BF Bellman Ford code template, learning # include <stdio. h> # include <string. h ># define INF 999999 struct node {int U, V, W;} Q [4000002]; int dis [500002]; int T = 0; int n, m; int S, E; int U, V, W; int flag; void add (int u, int V, int W) {q [T]. U = u; Q [T]. V = V; Q [t ++]. W = W;} void BF () {int I, j; for (I = 0; I <= N; I ++) {dis [I] = inf ;} dis [s] = 0; for (I = 1; I <= n-1; I ++) {flag = 0; For (j = 0; j <t; j ++) {If (DIS [Q [J]. v]> dis [Q [J]. u] + Q [J]. w) {d Is [Q [J]. v] = dis [Q [J]. u] + Q [J]. w; flag = 1 ;}}if (flag = 0) break;} printf ("% d \ n", DIS [e]);} int main () {int I = 0; while (scanf ("% d", & N, & M )! = EOF) {for (I = 0; I <m; I ++) {scanf ("% d", & U, & V, & W ); add (U, V, W); add (v, U, W) ;}scanf ("% d", & S, & E); BF ();} return 0 ;}