poj 2387 Til the Cows Come Home (最短路)
来源:互联网 发布:龙 知乎 编辑:程序博客网 时间:2024/05/30 07:14
上题:
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.
* 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.
* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
5 51 2 202 3 303 4 204 5 201 5 100
90
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.
题意:
不解释 最短路裸题,直接看样例就知道怎么写
上代码
#include<stdio.h>#include<string.h>#define INF 999999999int map[1500][1500];int dis[2200],vis[2200];int t,n,a,b,c;void dijkstra(){for(int i=1;i<=n;i++){dis[i]=map[1][i];vis[i]=0;}vis[1]=1;int min=INF,u;for(int i=1;i<=n;i++){min=INF;for(int j=1;j<=n;j++){if(!vis[j]&&dis[j]<min){min=dis[j];u=j;}}vis[u]=1;for(int v=1;v<=n;v++){if(!vis[v]&&dis[v]>dis[u]+map[u][v])//记得标记 {dis[v]=dis[u]+map[u][v];}}}}int main(){while(scanf("%d%d",&t,&n)!=EOF){for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){if(i==j) map[i][j]=0;else map[i][j]=INF;}}for(int i=1;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[n]);} }
0 0
- POJ 2387 Til the Cows Come Home (最短路)
- poj 2387 Til the Cows Come Home (最短路)
- poj 2387 Til the Cows Come Home (最短路)
- POJ 2387Til 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(最短路)
- poj 2387 Til the Cows Come Home -- 最短路dijstra
- POJ 2387 Til the Cows Come Home 最短路
- POJ 2387 Til the Cows Come Home【最短路】
- POJ 2387 Til the Cows Come Home 最短路 Dijstra
- POJ-2387 Til the Cows Come Home【最短路】
- POJ-2387-Til the Cows Come Home [最短路]
- poj Til the Cows Come Home (Dijkstra最短路~)
- poj 2387 Til the Cows Come Home(最短路水题 = =)
- poj——2387——Til the Cows Come Home(简单最短路)
- POJ 2387 Til the Cows Come Home (最短路+Dijkstra)
- POJ 2387-Til the Cows Come Home(最短路Dijkstra+优先队列)
- 从操作系统的角度看进程的建立过程
- 04.27
- PAT B1043
- a标签自动触发
- 判断素数
- poj 2387 Til the Cows Come Home (最短路)
- protobuf-c 在arm linux 嵌入式平台的使用 protobuf和Json对比测试
- toLeftof、toRightOf、above、below等id报错的原因
- js原型链与闭包
- bzoj1177 [Apio2009] Oil 分类讨论
- PAT B1047
- Bi-shoe and Phi-shoe LightOJ
- AngularJS之组件化篇
- c语言链表的基本操作2