Til the Cows Come Home (最短路模板题)

个人心得:模板题,不过还是找到了很多问题,真的是头痛,为什么用dijkstra算法book【1】=1就错了.....

纠结中....

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.

Sample Input

5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100

Sample Output

90

Hint

INPUT DETAILS:

There are five landmarks.

OUTPUT DETAILS:

Bessie can get home by following trails 4, 3, 2, and 1.

Dijkstra算法

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<iomanip>
 6 #include<algorithm>
 7 using namespace std;
 8 #define inf 1<<29
 9 int t,n;
10 int cow[1005][1005];
11 int dis[1005];
12 int book[1005];
13 void dijkstra()
14 {
15     memset(book,0,sizeof(book));
16     for(int i=1;i<=n;i++) dis[i]=cow[1][i];
17     dis[1]=0;
18     book[1]=1;
19     for(int i=1;i<=n;i++)
20     {
21         int mina=inf;
22         int k;
23         for(int j=1;j<=n;j++)
24         {
25             if(book[j]==0&&mina>dis[j])
26             {
27                 mina=dis[j];
28                 k=j;
29                 }
30         }
31         book[k]=1;
32         for(int j=1;j<=n;j++)
33         {
34                 if(book[j]==0&&dis[j]>dis[k]+cow[k][j])
35                     dis[j]=dis[k]+cow[k][j];
36             }
37     }
38 }
39 int main()
40 {
41     while(cin>>t>>n){
42     int x,y,z;
43     for(int i=1;i<=n;i++)
44          for(int j=1;j<=n;j++)
45               if(i==j) cow[i][j]=0;
46        else cow[i][j]=inf;
47     for(int i=1;i<=t;i++){
48         cin>>x>>y>>z;
49         if(cow[x][y]>z)
50        cow[x][y]=cow[y][x]=z;
51
52     }
53     dijkstra();
54     cout<<dis[n]<<endl;}
55         return 0;
56 }

Bellman-Ford算法

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<iomanip>
 6 #include<algorithm>
 7 using namespace std;
 8 #define inf 1<<29
 9 int t,n;
10 int u[2005],v[2005],w[2005];
11 int dis[1005];
12 int book[1005];
13 int main()
14 {
15     while(cin>>t>>n)
16     {
17         for(int i=1;i<=t;i++)
18             cin>>u[i]>>v[i]>>w[i];
19         for(int i=1;i<=n;i++)
20             dis[i]=inf;
21         dis[1]=0;
22         for(int i=1;i<=n;i++)
23             for(int j=1;j<=t;j++){
24                if(dis[v[j]]>dis[u[j]]+w[j])
25                   dis[v[j]]=dis[u[j]]+w[j];
26                   if(dis[u[j]]>dis[v[j]]+w[j])
27                   dis[u[j]]=dis[v[j]]+w[j];
28
29             }
30         cout<<dis[n]<<endl;
31
32     }
33     return 0;
34 }

Queue

 1 /*
 2 spfa
 3 Memory 256K
 4 Time   32MS
 5 */
 6 #include <iostream>
 7 #include <queue>
 8 using namespace std;
 9 #define inf 1<<29
10 #define MAXM 4005
11 #define MAXV 1005
12
13 typedef struct{
14     int a,b,w,next;
15 }Edge;
16
17 Edge edge[MAXM];
18 int n,m,headlist[MAXV];
19
20 void spfa(){
21     int i,d[MAXV],v,vis[MAXV];
22     queue <int>q;
23     for(i=2;i<=n;i++){
24         d[i]=inf;
25         vis[i]=0;
26     }
27     d[1]=0;
28     vis[1]=1;
29     q.push(1);
30     while(!q.empty()){
31         v=q.front();q.pop();
32         vis[v]=0;
33
34         for(i=headlist[v];i!=-1;i=edge[i].next)
35             if(d[v]+edge[i].w<d[edge[i].b]){
36                 d[edge[i].b]=d[v]+edge[i].w;
37                 if(!vis[edge[i].b]){
38                     vis[edge[i].b]=1;
39                     q.push(edge[i].b);
40                 }
41             }
42     }
43     printf("%d\n",d[n]);
44 }
45
46 int main(){
47     int i,a,b,c;
48     while(~scanf("%d%d",&m,&n)){
49         for(i=1;i<=n;i++) headlist[i]=-1;
50         for(i=1;i<=2*m;i+=2){
51             scanf("%d%d%d",&a,&b,&c);
52             edge[i].a=a;
53             edge[i].b=b;
54             edge[i].w=c;
55             edge[i].next=headlist[a];
56             headlist[a]=i;
57             edge[i+1].a=b;
58             edge[i+1].b=a;
59             edge[i+1].w=c;
60             edge[i+1].next=headlist[b];
61             headlist[b]=i+1;
62         }
63         spfa();
64     }
65     return 0;
66 }

时间: 2024-11-06 00:46:41

Til the Cows Come Home (最短路模板题)的相关文章

POJ 2387 Til the Cows Come Home (dijkstra模板题)

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 morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible. Farmer Joh

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

题目链接: http://poj.org/problem?id=2387 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 morning milking. Bessie needs her beauty sleep, so she wants to get ba

HDU 5521.Meeting 最短路模板题

Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 3361    Accepted Submission(s): 1073 Problem Description Bessie and her friend Elsie decide to have a meeting. However, after Farmer Jo

poj1511/zoj2008 Invitation Cards(最短路模板题)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Invitation Cards Time Limit: 5 Seconds      Memory Limit: 65536 KB In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fa

[poj2449]Remmarguts&#39; Date(K短路模板题,A*算法)

解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<iostream> #include<cmath> #include<queue> using namespace std; typedef long long ll; const int N=1e3+10; const

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

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 morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible. Farmer Joh

hdu1874 最短路模板题

之所以做了第二道模板题还要写是因为发现了一些自己的问题 用的是dij 最简单的松弛 需要注意的地方是松弛的时候 判断dis[i]<dis[w]+tance[w][i]时 还要再判断 vis[i] 要保证这个点没有成为过最小点 即这个点不会是已经被松弛过的点 输入的时候要注意 可能会有重边的输入 每次输入的时候进行一次判断 如果输入的是较大值 就不用更换了 关于memset的使用 它只能用来设置0与-1 别的值会出现莫名的错误 #include<stdio.h> #include<s

poj 2387 Til the Cows Come Home -- 最短路dijstra

<span style="font-size:18px;">利用dijstra求单源最短路,利用floyd会超时的</span> #include <stdio.h> #include <string.h> #include <algorithm> #define inf 0x3f3f3f3f using namespace std; const int N = 1005; int map[N][N]; int vis[N];

poj 2387 最短路模板题

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 ha