最短路问题--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\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 }

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

原文地址:https://www.cnblogs.com/very-beginning/p/12207391.html

时间: 2024-08-30 11:13:25

最短路问题--Bellman-Ford Til the Cows Come Home的相关文章

POJ 2387 Til the Cows Come Home Dijkstra求最短路径

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.

ACM/ICPC 之 最短路径-Bellman Ford范例(POJ1556-POJ2240)

两道Bellman Ford解最短路的范例,Bellman Ford只是一种最短路的方法,两道都可以用dijkstra, SPFA做. Bellman Ford解法是将每条边遍历一次,遍历一次所有边可以求得一点到任意一点经过一条边的最短路,遍历两次可以求得一点到任意一点经过两条边的最短路...如 此反复,当遍历m次所有边后,则可以求得一点到任意一点经过m条边后的最短路(有点类似离散数学中邻接矩阵的连通性判定) POJ1556-The Doors 初学就先看POJ2240吧 题意:求从(0,5)到

POJ 2387 Til the Cows Come Home (最短路+Dijkstra)

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29550   Accepted: 9935 Description 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

最短路问题(Bellman/Dijkstra/Floyd)

最短路问题(Bellman/Dijkstra/Floyd) 寒假了,继续学习停滞了许久的算法.接着从图论开始看起,之前觉得超级难的最短路问题,经过两天的苦读,终于算是有所收获.把自己的理解记录下来,可以加深印象,并且以后再忘了的时候可以再看.最短路问题在程序竞赛中是经常出现的内容,解决单源最短路经问题的有bellman-ford和dijkstra两种算法,其中,dijikstra算法是对bellman的改进.解决任意两点间的最短路有Floyd-warshall算法. 单源最短路1(bellman

POJ 1860 Currency Exchange (Bellman ford)

Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22405   Accepted: 8095 Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and pe

POJ 2387 Til the Cows Come Home dijkstra算法 用邻接表和邻接矩阵

题目如下: Til the Cows Come Home Time Limit: 1000MS        Memory Limit: 65536K Total Submissions: 27726        Accepted: 9353 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wa

POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37662   Accepted: 12836 Description 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

poj 2387 Til the Cows Come Home(dijkstra算法)

题目链接:http://poj.org/problem?id=2387 题目大意:起点一定是1,终点给出,然后求出1到所给点的最短路径. 注意的是先输入边,在输入的顶点数,不要弄反哦~~~ 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 int map[2010][2010],Min,node[2010],vis[2010],t,q; 5 const int INF=9999999; 6 7 vo

POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)

传送门 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46727   Accepted: 15899 Description 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