迪杰斯特拉哦,很挫哦
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.
AC代码:
1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 #include <iostream> 5 #include <queue> 6 #include <math.h> 7 #define INF 1<<29 8 using namespace std; 9 int s[1011][1011],dis[1010],n,vis[1010]; 10 void findeasy()//迪杰斯特拉算法是求从一个单源点到其余每个点的最短路径; 11 { 12 int x; 13 memset(vis,0,sizeof(vis)); 14 for(int i=1; i<=n; i++) 15 { 16 dis[i]=s[n][i];//每个节点的初始状态 17 } 18 vis[n]=1; 19 for(int i=0; i<n; i++) 20 { 21 int min1=INF; 22 for(int j=1; j<=n; j++) 23 { 24 if(!vis[j]&&dis[j]<min1)//每次从剩余节点里挑个最短的节点,对于这个节点来说这是他的最短路,不需要再继续更新了,就算继续更新也不会再变短了; 25 { 26 min1=dis[j]; 27 x=j; 28 } 29 } 30 vis[x]=1; 31 for(int j=1; j<=n; j++)//松弛操作,每次找到一个节点就检验一下是否能够使得其余节点松弛. 32 { 33 if(!vis[j]&& dis[j]> dis[x] + s[x][j]) 34 { 35 dis[j]=dis[x]+s[x][j]; 36 } 37 } 38 } 39 printf("%d ",dis[1]); 40 } 41 int main() 42 { 43 int T,a,b,c; 44 scanf("%d%d",&T,&n); 45 for(int i=1;i<=n;i++)//最短路问题初始化,不同点之间的距离设为无穷大,相同点的距离为零; 46 { 47 for(int j=1;j<=n;j++) 48 { 49 if(i!=j) 50 { 51 s[i][j]=INF; 52 } 53 else 54 { 55 s[i][j]=0; 56 } 57 } 58 } 59 while(T--) 60 { 61 scanf("%d%d%d",&a,&b,&c); 62 if(s[a][b]>c)//防止有坑的数据,使得不同点之间保存最短的那条路; 63 { 64 s[a][b]=c; 65 s[b][a]=c; 66 } 67 } 68 findeasy(); 69 return 0; 70 }