Shortest path problem --Bellman-Ford Til the Cows Come Home

Til the Cows Come Home

Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.

Farmer John's field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.

Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.

Input

* Line 1: Two integers: T and N

* Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.

Output

* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
 
Bellman-Ford: to the side for the shortest path algorithm thinking center. It can be found in the case of a negative ring.
FIG storage structure: all the side information stored
1  const  int MaxM = 1010 ; 
2  struct edge { 
 3      int u; 
4      int c; 
5      int dist; 
6  }; 
7 edge E [MaxM];

 1. Initialization Process Initialization distance from the source node to all nodes

1 const int INF = 1e9; 
2 for (int i = 0 ;i<= n ;i++) 
3     dist[i] = INF; 
4 dist[sNode] = 0;

Bellman-Ford Process

 1 for (int i = 0 ;i< n-1 ;i++){ 
 2     bool isOk = false; 
 3     for (int j = 0; j< m ;j++){ 
 4         int u = E[j].u; 
 5         int v = E[j].v; 
 6         int d = E[j].dist; 
 7         if (dist[u] + d < dist[v]){ 
 8             dist[v] = dist[u] + d; 
 9             isOk = true; 
10             } 
11         else if (dist[v] + d < dist[u]){ 
12             dist[u] = dist[v] + d; 
13             isOk = true; 
14             } 
15         } 
16         if (!isOk) break; 
17     }

The number of time complexity of the node N, the number of edges MO (N * M)

 • seeking a shortest distance example all the node to node 1

 

 1. Initialization
• distance from the source node to all nodes dist

 

 2. cycle
(a) step 1 • traversing all edges, both ends of the end edge updates a distance from the source point

  --1--22 becomes 5

  --1--33 to 1

  --1--44 becomes 5

  --2--3 can not be updated

  --2--5 can not be updated

  --4--5 not update

Dist distance from the source node • All nodes

 

 (b) step 2

• traverse all sides, both ends of the update side end point of the distance from the source

   --1--2 not updated

  --1--3 not updated

  --1--4 not updated

  --2--32 becomes 2

  --2--55 becomes 10

   --4--55 becomes 11

Dist distance from the source node • All nodes

 

 (c) step 3

• traverse all sides, both ends of the update side end point of the distance from the source

  --1--2 not updated

  --1--3 not updated

  --1--4 not updated

  --2--3 not updated

  --2--55 becomes 7

  --4--5 not updated

Dist distance from the source node • All nodes

 

 (d) step 4

• traverse all sides, both ends of the update side end point of the distance from the source

  --1--2 not updated

  --1--3 not updated

  --1--4 not updated

  --2--3 not updated

  --2--5 not updated

  --4--5 not updated

Dist distance from the source node • All nodes

 

 (E) all of the termination condition is not updated

Probably it is this process, solving the problem code is as follows:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 const int N=2020;
 6 const int MAX=1e9;
 7 using namespace std ;
 8 struct node{
 9     int a , b , w ;
10 }edge[N];
11 int n , m ;
12 void bell()
13 {
14     int i , j ;
15     int  d[N];
16     for ( int I = . 1 ; I <= n-; I ++) // * initialize infinite distance; 
. 17      {
 18 is          D [I] = MAX;
 . 19      }
 20 is      D [ . 1 ] = 0 ; // * 0 is the initial location; 
21 is      for (I = . 1 ; I <= n-; I ++ )
 22 is      {
 23 is          for (J = . 1 ; J <= m; J ++ )
 24          {
 25              IF (D [Edge [J] II.A]> D [Edge [J ] .B] Edge + [J] .W) D [Edge [J] II.A] = D [Edge [J] .B] + Edge [J] .W;
 26 is              IF(d[edge[j].b]>d[edge[j].a]+edge[j].w) d[edge[j].b]= d[edge[j].a]+edge[j].w;
27         }
28     }
29     printf("%d\n",d[n]);
30 }
31 int main()
32 {
33     int i , a   , b ,c;
34     cin>>m>>n;
35         for(int i =1 ; i<=m;i++)//*结构体存边和权
36         {
37             cin>>a>>b>>c;
38             edge[i].a=a;
39             edge[i].b=b;
40             edge[i].w=c;
41         }
42         bell();
43     return 0 ;
44 }

 Now we can only grasp these types of matter, there SPFA or something, so hard, trained, and slowly come! Training more than half, come on!

Guess you like

Origin www.cnblogs.com/very-beginning/p/12207391.html