POJ - 2387 Til the Cows Come Home

Posted 西北会法语

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ - 2387 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.

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.
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #define INF 0x3f3f3f3f
 6 
 7 using namespace std;
 8 int G[1010][1010];
 9 int vis[1010],cost[1010],n,m;
10 
11 void Dijkatra(int s)
12 {
13     int d,minn;
14     cost[s]=0;
15     vis[s]=1;
16     for(int i=1;i<=n;i++)
17         cost[i]=G[s][i];
18     for(int i=2;i<=n;i++)
19     {
20         minn=INF;
21         for(int j=1;j<=n;j++)
22         {
23             if(cost[j]<minn&&vis[j]==0)
24             {
25                 d=j;
26                 minn=cost[j];
27             }
28         }
29         vis[d]=1;
30         for(int j=1;j<=n;j++)
31             if(cost[d]+G[j][d]<cost[j]&&!vis[j])
32                 cost[j]=cost[d]+G[j][d];
33     }
34 }
35 
36 int main()//标号从0开始 
37 {
38     int u,v,w;
39 while(~scanf("%d%d",&m,&n))
40     {
41         memset(vis,0,sizeof(vis));
42         memset(G,INF,sizeof(G));
43         for(int i=1;i<=1000;i++)
44             G[i][i]=0;
45         for(int i=0;i<m;i++)
46         {
47             scanf("%d%d%d",&u,&v,&w);
48             if(w<G[v][u])
49                 G[u][v]=G[v][u]=w;
50         }
51         Dijkatra(1);
52         printf("%d\n",cost[n]);
53     
54     
55 }
56     return 0;    
57 } 

 

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

POJ 2387 Til the Cows Come Home (Dijkstra)

POJ 2387 Til the Cows Come Home

POJ - 2387 Til the Cows Come Home

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

[POJ - 2387] L - Til the Cows Come Home(图论)

POJ 2387 Til the Cows Come Home