最短路问题--Bellman-Ford Til the Cows Come Home

Posted very-beginning

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了最短路问题--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:以边为思考中心的最短路径算法。可以发现负环的情况。
图结构存储:把所有边信息存储起来
1 const int maxm = 1010; 
2 struct edge{ 
3     int u; 
4     int v; 
5     int dist; 
6 }; 
7 edge E[maxm];

 流程 1. 初始化 初始化所有节点距离源节点的距离

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

Bellman-Ford 流程

 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     }

时间复杂度 节点个数 N,边个数 M O(N*M)

 举例 • 求所有节点到节点 1 的最短距离

技术图片

 

 1. 初始化
• 所有节点距离源节点的距离 dist

技术图片

 

 2. 循环
(a) step 1 • 遍历所有边, 更新边两端端点距离源点的距离

  – 1 - 2 2 变为 5

  – 1 - 3 3 变为 1

  – 1 - 4 4 变为 5

  – 2 - 3 不能更新

  – 2 - 5 不能更新

  – 4 - 5 不能更新

• 所有节点距离源节点的距离 dist

技术图片

 

 (b) step 2

• 遍历所有边, 更新边两端端点距离源点的距离

   – 1 - 2 不更新

  – 1 - 3 不更新

  – 1 - 4 不更新

  – 2 - 3 2 变成 2

  – 2 - 5 5 变为 10

   – 4 - 5 5 变为 11

• 所有节点距离源节点的距离 dist

技术图片

 

 (c) step 3

• 遍历所有边, 更新边两端端点距离源点的距离

  – 1 - 2 不更新

  – 1 - 3 不更新

  – 1 - 4 不更新

  – 2 - 3 不更新

  – 2 - 5 5 变为 7

  – 4 - 5 不更新

• 所有节点距离源节点的距离 dist

技术图片

 

 (d) step 4

• 遍历所有边, 更新边两端端点距离源点的距离

  – 1 - 2 不更新

  – 1 - 3 不更新

  – 1 - 4 不更新

  – 2 - 3 不更新

  – 2 - 5 不更新

  – 4 - 5 不更新

• 所有节点距离源节点的距离 dist

技术图片

 

 (e) 终止条件为全部不更新

大概过程就是这样啦,这道题的代码如下:

 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++)//*距离初始化为无穷;
17     {
18         d[i]=MAX;
19     }
20     d[1]=0;//*初始地点为0;
21     for(i=1;i<=n;i++)
22     {
23         for(j=1;j<=m;j++)
24         {
25             if(d[edge[j].a]>d[edge[j].b]+edge[j].w) d[edge[j].a]= d[edge[j].b]+edge[j].w;
26             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
",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 }

 现在就只能掌握这几种了啦,还有SPFA什么的,好难,嘤,慢慢来!集训过半,加油!

以上是关于最短路问题--Bellman-Ford Til the Cows Come Home的主要内容,如果未能解决你的问题,请参考以下文章

POJ 2387 Til the Cows Come Home

Bellman-ford 单源最短路径算法

最短路径之Bellman-Ford算法

最短路问题之Bellman-ford算法

四大算法解决最短路径问题(Dijkstra+Bellman-ford+SPFA+Floyd)

Bellman-Ford算法(最短路)