poj——2387——Til the Cows Come Home(简单最短路)
2014-01-21 10:17
465 查看
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
Sample Output
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
#include <iostream> #include <algorithm> #include <cstring> using namespace std; const int M=2005; const int inf=1000000; int map[M][M]; int dis[M]; int flag[M]; int dijkstra(int from,int n) { int temp; int i,j; int u; for (int i=1;i<=n;i++) { dis[i]=map[from][i]; } flag[from]=1; for (i=1;i<n;i++) { temp=inf; for (j=1;j<=n;j++) { if (!flag[j]&&dis[j]<temp) { temp=dis[j]; u=j; } } flag[u]=1; for (j=1;j<=n;j++) { if (!flag[j]&&map[u][j]!=inf) { temp=dis[u]+map[u][j]; if (temp<dis[j]) dis[j]=temp; } } } return dis[1]; } int main() { int t,n; while (cin>>t>>n) { for (int i=1;i<=n;i++) { map[i][i]=0; for (int j=1;j<i;j++) { map[i][j]=map[j][i]=inf; } } for(int i=0;i<t;i++) { int a,b,len; cin>>a>>b>>len; if(len<map[a][b]) map[a][b]=map[b][a]=len; } memset(flag,0,sizeof(flag)); cout<<dijkstra(n,n)<<endl; } return 0; }
相关文章推荐
- POJ - 2387 Til the Cows Come Home(最短路Dijkstra模板题)
- POJ 2387 Til the Cows Come Home(最短路dijkstra)
- (简单) POJ 2387 Til the Cows Come Home,Dijkstra。
- POJ 2387 Til the Cows Come Home 【最短路(dijkstra)】
- POJ 2387 Til the Cows Come Home (最短路+Dijkstra)
- POJ 2387 Til the Cows Come Home (最短路+Dijkstra)
- POJ 2387 Til the Cows Come Home【最短路】
- POJ 2387-Til the Cows Come Home(最短路Dijkstra+优先队列)
- POJ 2387 || Til the Cows Come Home(最短路,dijkstra
- POJ 2387 Til the Cows Come Home(最短路模板)**
- 最短路 - A - Til the Cows Come Home POJ - 2387
- POJ-2387 Til the Cows Come Home ( 最短路 )
- POJ 2387 Til the Cows Come Home (最短路 dijkstra)
- POJ 2387 Til the Cows Come Home(最短路,Dijkstra算法)
- 7_4_N题 Til the Cows Come Home 题解[poj 2387] (最短路)
- [kuangbin带你飞]专题4 最短路 A - Til the Cows Come Home POJ - 2387
- poj 2387 Til the Cows Come Home(最短路水题 = =)
- POJ 2387 Til the Cows Come Home (最短路)
- POJ 2387 Til the Cows Come Home(最短路)
- POJ 2387 Til the Cows Come Home 最短路