Zipper
2013-05-24 16:56
507 查看
Problem C
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)Total Submission(s) : 20 Accepted Submission(s) : 8
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
* 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.
[align=left]Output[/align]
* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
[align=left]Sample Input[/align]
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
[align=left]Sample Output[/align]
90
#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #include<cstring> #define M 2010 #define INF (int)1e7 using namespace std; int n,t; int v[M],map[M][M],dis[M]; int dijkstra() { int i,j; for(i=1;i<n;i++) { v[i]=0; dis[i]=map [i]; } dis =0; v =1; for(i=0;i<n;i++) { int min=INF; int pos=0; for(j=1;j<=n;j++) { if(!v[j]&&dis[j]<min) { min=dis[j]; pos=j; } } v[pos]=1; if(min==INF) { break; } for(j=1;j<=n;j++) { int p=dis[pos]+map[pos][j]; if(!v[j]&&p<dis[j]) { dis[j]=p; } } } } int main() { int i,j,k,a,b,c; while(~scanf("%d%d",&t,&n)) { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { map[i][j]=INF; } } for(i=0;i<t;i++) { scanf("%d%d%d",&a,&b,&c); if(map[a][b]>c) { map[a][b]=c; map[b][a]=c; } } dijkstra(); printf("%d\n",dis[1]); } return 0; }
相关文章推荐
- POJ 2192 判断两个字符串是否能按顺序组合成第三个字符串 ZIPPER
- poj 2192 Zipper
- DFS专题 Zipper
- poj_2192Zipper
- POJ 2192 Zipper
- sicily1010. Zipper
- HDU--杭电--1501--Zipper--深搜、DP都好
- Zipper
- HDU 1501 Zipper(DP,DFS)
- ACM POJ 2192 Zipper
- hdu1501 Zipper 动态规划
- POJ---2192-Zipper
- hdu1501 Zipper--DP
- hdu1501 Zipper(dfs && dp)
- POJ 2192 Zipper简单dp
- ACMzipper
- Zipper - DFS搜索+剪枝
- 0811 POJ#2192 Zipper
- HDOJ 1501 Zipper 【简单DP】
- POJ 2192 判断两个字符串是否能按顺序组合成第三个字符串 ZIPPER