POJ 2387 Til the Cows Come Home (最短路径,Dijkstra算法)
来源:互联网 发布:手机淘宝查看天猫积分 编辑:程序博客网 时间:2024/06/05 08:17
Til the Cows Come Home
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.
n^2算法:
AC代码:
#include<cstdio>#include<algorithm>#include<cstring>using namespace std;const int maxn = 1e3+10;const int INF = 1e8;int d[maxn];int w[maxn][maxn];int vist[maxn];int main(){int T,N;while(scanf("%d%d",&T,&N)==2){memset(w,0,sizeof(w));for(int i = 1; i <= T; i++){int a,b,t;scanf("%d%d%d",&a,&b,&t);if(w[a][b] && w[a][b] < t) continue; //注意这里需要判断w[a][b] = w[b][a] = t;}for(int i = 1; i <= N; i++) i == 1 ? d[i] = 0 : d[i] = INF;memset(vist,0,sizeof(vist));for(int i = 1; i <= N; i++){int Min = INF,x;for(int j = 1; j <= N; j++) if(!vist[j] && d[j] <= Min) Min = d[x=j]; vist[x] = 1; for(int y = 1; y <= N; y++) if(w[x][y]) d[y] = min(d[y],d[x] + w[x][y]); }printf("%d\n",d[N]);}return 0;}
阅读全文
0 0
- POJ 2387 Til the Cows Come Home (最短路径,Dijkstra算法)
- poj 2387Til 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(最短路径)
- Poj 2387 Til the Cows Come Home(Dijkstra 最短路径)
- POJ 2387 Til the Cows Come Home(Dijkstra算法)
- POJ 2387 Til the Cows Come Home(Dijkstra算法)
- (阶段三 dijkstra算法温习 1.6)POJ 2387 Til the Cows Come Home(使用dijkstra算法求单源起点和单源终点的最短路径)
- POJ 2387 Til the Cows Come Home(最短路径,模板题)
- POJ 2387 Til the Cows Come Home(最短路径)
- POJ 2387Til the Cows Come Home(最短单源路径)(dijkstra)
- Til the Cows Come Home(最短路径)
- Til the Cows Come Home (最短路径)
- poj2387 - Til the Cows Come Home(最短路径问题) (Dijkstra)
- POJ 2387 Til the Cows Come Home 最短路-Dijkstra
- POJ 2387 || Til the Cows Come Home(最短路,dijkstra
- poj Til the Cows Come Home (Dijkstra最短路~)
- hdu 1217 Arbitrage
- 学习笔记02(JDBC)
- HDOJ 2107 Founding of HDU(水题)
- ollvm编译环境配置
- http协议分析
- POJ 2387 Til the Cows Come Home (最短路径,Dijkstra算法)
- 3.canvas:圆弧的绘制
- lombok常用注解整理
- 使用Properties获取数据配置信息
- NDK socket 非阻塞方式设定 以及 返回值
- Java 中 ++ 操作符的线程安全
- 【Java作业】Week08
- 判断一个数是否为素数
- dialog使用案例,包含增加按钮关闭弹窗