jianupc

 

poj 2387 Til the Cows Come Home dijkstra模板

Til the Cows Come Home

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22455   Accepted: 7526

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 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.

Source

 
这道题用dijkstra算法解决,关键是在建图时注意重边
View Code
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 const int MAX = 1005;
 7 const int INF = 0x7ffffff;
 8 int map[MAX][MAX];
 9 int dist[MAX];
10 int vis[MAX];
11 int n,m;
12 
13 void dijkstra()
14 {
15     int i,j,k,min;
16     for (i=1; i<=n; i++)
17     {
18         dist[i] = map[1][i];
19         vis[i] = 0;
20     }
21     vis[1] = 1;
22     dist[1] = 0;
23 
24     for (i=1; i<n; i++)
25     {
26         k = 0;
27         min = INF;
28         for (j=1; j<=n; j++)
29         {
30             if (!vis[j] && min>dist[j])
31             {
32                 min = dist[j];
33                 k = j;
34             }
35         }
36 
37         if (k == 0)return;
38         vis[k] = 1;
39 
40         for (j=1; j<=n; j++)
41         {
42             if (!vis[j] && dist[j]>min+map[k][j])
43             {
44                 dist[j] = min + map[k][j];
45             }
46         }
47     }
48 }
49 
50 int main()
51 {
52 //    freopen("in.txt","r",stdin);
53     int i,j;
54     int s,t,w;
55     while (scanf("%d %d",&m,&n)!=EOF)
56     {
57         for (i=1; i<=n; i++)
58         {
59             for (j=i; j<=n; j++)
60             {
61                 map[i][j] = map[j][i] = INF;
62             }
63         }
64         for (i=0; i<m; i++)
65         {
66             scanf("%d %d %d",&s,&t,&w);
67             if (map[s][t]>w)
68             {
69                 map[s][t] = map[t][s] = w;
70             }
71         }
72         dijkstra();
73         printf("%d\n",dist[n]);
74     }
75     return 0;
76 }

 

posted on 2013-01-22 20:36  shijianupc  阅读(214)  评论(0)    收藏  举报

导航