POJ2387
Til the Cows Come Home
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 21530 | Accepted: 7161 |
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.
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.
* 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.
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
Source
思路:本题是典型的求最短路径。水题。不过要注意这题的输入 是先输入边数,在输入点数。(此处RE了好几次)
1 #include <cstdlib> 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <string> 7 #include <map> 8 9 #define MAXINT 99999999 10 11 using namespace std; 12 13 14 int data[1000+4][1000+4]; 15 int isvis[1000+4]; 16 int dis[1000+4]; 17 18 19 20 21 22 int main(int argc, char *argv[]) 23 { 24 25 int n,m; 26 27 int i,j,k; 28 29 scanf("%d%d",&m,&n); 30 31 32 for(i=1;i<=n;i++) 33 for(j=1;j<=n;j++) 34 data[i][j]=MAXINT; 35 36 37 38 for(i=0;i<m;i++) 39 { 40 int a,b,c; 41 42 scanf("%d%d%d",&a,&b,&c); 43 if(c<data[a][b]) 44 data[a][b]=data[b][a]=c; 45 } 46 47 48 for(i=1;i<=n;i++) 49 isvis[i]=0; 50 51 for(i=1;i<=n;i++) 52 {dis[i]=data[n][i];} 53 54 isvis[n]=1; 55 56 57 int tmpcost=MAXINT; 58 59 60 for(i=1;i<n;i++) 61 { 62 k=-1; 63 tmpcost=MAXINT; 64 65 for(j=1;j<=n;j++) 66 { 67 if((isvis[j]==0)&&(tmpcost>dis[j])) 68 {k=j;tmpcost=dis[j];} 69 } 70 71 if(k>0) 72 { 73 isvis[k]=1; 74 75 if(k==1) 76 break; 77 78 79 80 for(j=1;j<=n;j++) 81 { 82 if((isvis[j]==0)&&(dis[j]>dis[k]+data[k][j])) 83 dis[j]=dis[k]+data[k][j]; 84 } 85 } 86 } 87 88 printf("%d\n",dis[1]); 89 90 91 92 93 94 95 96 97 //system("PAUSE"); 98 return EXIT_SUCCESS; 99 }