poj2873 Til the Cows Come Home

来源:互联网 发布:三步倒淘宝叫什么暗号 编辑:程序博客网 时间:2024/06/05 21:58

A - Til the Cows Come Home
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status Practice POJ 2387

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

5 51 2 202 3 303 4 204 5 201 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.


题目大意:找到从起点1到终点n的最短路径(迪杰斯特拉算法)


#include<stdio.h>#include<string.h>#define MAX 10000000int n,t;int dis[1010];int map[1010][1010];bool mark[10010];void dijstra() {int i,j,k;int min,p;memset(mark,0,sizeof(mark));for(i=1;i<=n;i++){dis[i]=map[1][i];}dis[1]=0;mark[1]=true;for(i=1;i<=n;i++){min=MAX;for(j=1;j<=n;j++){if(!mark[j]&&dis[j]<min){min=dis[j];k=j;//标记最小值 }}mark[k]=true;//最小值找到,标记//接下来更新路径for(p=1;p<=n;p++){if(!mark[p]&&map[k][p]!=MAX)//此路是桐庐{if(dis[p]>dis[k]+map[k][p])//更新小路径 {dis[p]=dis[k]+map[k][p];} }  }  }}int main(){int i,j;int a,b,c;while(scanf("%d%d",&t,&n)!=EOF){for(i=0;i<=n;i++){for(j=0;j<=n;j++){map[i][j]=MAX;}}for(i=1;i<=t;i++){scanf("%d%d%d",&a,&b,&c);if(c<map[a][b]){map[a][b]=c;map[b][a]=c;}}dijstra();printf("%d\n",dis[n]);}return 0;}





0 0