Bellman-ford can solve the shortest-circuit problem with negative weight edge
Solving Negative weights is an advantage compared to Dijkstra, and the core code ofBellman-ford is only 4 lines:
U[],v[],w[] A vertex of one edge, weight, dis[] The distance from 1 source points to each vertex
for (i=1; i<=n-1; i++) for (j=1; j<=m;j++) if (Dis[v[i]] > dis[u[i]]+w[i]) = Dis[u[i]]+w[i];
Wish process:
Cycle n-1 times, each vertex of each edge is relaxed;
Optimization method:
①, the worst case is the cycle of n-1 times to find the shortest path to each vertex, if the n-1 times before it is all slack, then the back of the loop is superfluous
Optimization:
for(k=1; k<=n-1; k++)//a total of n vertices, cycle n-1 times can{flag=0; for(i=1; i<=m;i++)//slack on all current edges { if(Dis[v[i]] > dis[u[i]]+W[i]) {Dis[v[i]]= dis[u[i]]+W[i]; Flag=1; } } if(Flag = =0) Break;//the slack is done, the end}
②, the original process: after each cycle of relaxation, there has been determined from the source point to a certain point the shortest path, after these vertices of the most short-circuiting values will remain unchanged, no longer affected by subsequent relaxation operations, but each time to determine whether the need for relaxation, here wasted time.
Optimization: To determine the total number of edges after a reduction of one side, the non-relaxation of the edge again back to the original array, the relaxation of the successful side of the discard, once again slack only on the non-slack side of the operation, the value of M will be smaller with the relaxation, until complete.
for(k=1; k<=n-1; k++)//a total of n vertices, cycle n-1 times can{m= M;//the re-assigned m is the number of bars in the array where the edges are storeds =1; flag =0; for(i=1; i<=m;i++)//slack on all current edges { if(Dis[v[i]] > dis[u[i]]+W[i]) {Dis[v[i]]= dis[u[i]]+W[i]; M--;//relaxation succeeds, the number of edges minus oneFlag =1; } Else//To re-store the edges that cannot be relaxed to the current array{U[s]=U[i]; V[s]=V[i]; W[s]=W[i]; S++; } } if(Flag = =0) Break;//the slack is done, the end}
Complete code is included:
#include <stdio.h>intMain () {intdis[Ten],i,k,m,n,s=1, u[Ten],v[Ten],w[Ten],m,flag; intINF =99999999; scanf ("%d%d",&n,&m); M=m; for(i=1; i<=m;i++) {scanf ("%d%d%d", &u[i],&v[i],&w[i]);//input each edge and weight value } for(i=1; i<=n;i++) {Dis[i]= INF;//Initialize to positive infinity} dis[1] =0;//use 1 as the source point for(k=1; k<=n-1; k++)//a total of n vertices, cycle n-1 times can{m= M;//the re-assigned m is the number of bars in the array where the edges are storeds =1; flag =0; for(i=1; i<=m;i++)//slack on all current edges { if(Dis[v[i]] > dis[u[i]]+W[i]) {Dis[v[i]]= dis[u[i]]+W[i]; M--;//relaxation succeeds, the number of edges minus oneFlag =1; } Else//To re-store the edges that cannot be relaxed to the current array{U[s]=U[i]; V[s]=V[i]; W[s]=W[i]; S++; } } if(Flag = =0) Break;//the slack is done, the end } for(i=1; i<=n;i++) {printf ("%d", Dis[i]); } return 0;}
Test data:
5 5
2 3 2
1 2-3
1 5 5
4 5 2
3 4 3
Run Result: 0-3-1 2 4
Test Data 2:
5 7
1 2 2
1 5 10
2 3 3
2 5 7
3 4 4
4 5 5
5 3 6
Operating Result: 0 2 5 9 9
Bellman-ford (can solve negative weight edge)--time complexity optimization