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 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
O(n^2)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 #include <set> 8 #include <map> 9 #include <string> 10 #include <cmath> 11 #include <stdlib.h> 12 #define MAXSIZE 1005 13 using namespace std; 14 15 int t,n; 16 int m[MAXSIZE][MAXSIZE],dis[MAXSIZE],vis[MAXSIZE]; 17 18 void dij(int s) 19 { 20 int i,j,k; 21 for(i = 0;i<n;i++) 22 { 23 dis[i] = m[s][i]; 24 vis[i] = 0; 25 } 26 dis[s] = 0; 27 vis[s] = 1; 28 for(i = 1;i<=n;i++) 29 { 30 k = 0; 31 int mn = 1000000; 32 for(j = 1;j<=n;j++) 33 if(!vis[j]&&dis[j]<mn) 34 { 35 mn = dis[j]; 36 k = j; 37 } 38 vis[k] = 1; 39 for(j = 1;j<=n;j++) 40 if(!vis[j]&&dis[j]>dis[k]+m[k][j]) 41 dis[j] = dis[k]+m[k][j]; 42 } 43 } 44 45 int main() 46 { 47 freopen("caicai.txt","r",stdin); 48 cin>>t>>n; 49 int i,j,a,b,w; 50 for(i = 0;i<=n;i++) 51 for(j = 0;j<=n;j++) 52 m[i][j] = 1000; 53 for(i = 0;i<t;i++) 54 { 55 scanf("%d%d%d",&a,&b,&w); 56 if(m[a][b]>w) 57 { 58 m[a][b] = w; 59 m[b][a] = w; 60 } 61 } 62 dij(n); 63 cout<<dis[1]<<endl; 64 return 0; 65 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步