Til the Cows Come Home 【最短路】
来源:互联网 发布:键盘弹钢琴软件下载 编辑:程序博客网 时间:2024/05/02 06:13
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.
思路: 裸 最短路
代码
#include<stdio.h>#include<string.h>#include<algorithm>#include<math.h>#define inf 0x3f3f3f#define mod 1000007#define LL long long #define M 1000+10 using namespace std;int t,n;int map[M][M];int v[M];int dis[M];void getmap(){int i,j;for(i=1;i<=n;i++)for(j=1;j<=n;j++)if(j==i) map[i][j]=0;else map[i][j]=map[j][i]=inf;for(i=1;i<=t;i++){int a,b,c;scanf("%d%d%d",&a,&b,&c);if(map[a][b]>c)map[a][b]=map[b][a]=c;}}void floyd() // 超时间{int i,j,k;for(k=1;k<=n;k++){for(i=1;i<=n;i++){for(j=1;j<=n;j++)if(map[i][j]>map[i][k]+map[k][j])map[i][j]=map[i][k]+map[k][j];}}printf("%d\n",map[1][n]);}void djk(int st,int ed) //93ms{ int i,j,k,u; int min,next; for(i=1;i<=n;i++) { v[i]=0; dis[i]=map[st][i]; } v[st]=1; for(u=2;u<=n;u++) { min=inf; for(j=1;j<=n;j++) { if(!v[j]&&dis[j]<min) { min=dis[j]; next=j; }} if(min==inf) break; v[next]=1; for(j=1;j<=n;j++) { if(!v[j]&&dis[j]>dis[next]+map[next][j]) dis[j]=dis[next]+map[next][j]; } } printf("%d\n",dis[ed]);}int main(){scanf("%d%d",&t,&n);getmap();//djk(n,1);floyd();return 0; }
0 0
- 最短路----Til the Cows Come Home
- Til the Cows Come Home 【最短路】
- Til the Cows Come Home 【最短路】
- [最短路]pku2387-Til the Cows Come Home
- POJ 2387Til the Cows Come Home(最短路)
- Til the Cows Come Home(最短路)
- POJ 2387 Til the Cows Come Home 最短路-Dijkstra
- Til the Cows Come Home poj2387的最短路题目
- 最短路 (Dijstra) Til the Cows Come Home
- POJ 2387 || Til the Cows Come Home(最短路,dijkstra
- poj 2387 Til the Cows Come Home(最短路)
- poj2387- Til the Cows Come Home (最短路)
- poj 2387 Til the Cows Come Home -- 最短路dijstra
- poj2387~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 (最短路)
- poj 2387 Til the Cows Come Home (最短路)
- ASP.NET MVC3.0 中 Razor 母版嵌套
- 在Mac上将未签名的apk签名
- Bootstrap复选框和单选按钮美化插件
- C# SemophoreSlim
- Jquery Layer 插件关闭刷新父窗体
- Til the Cows Come Home 【最短路】
- ExtJS4——TextField
- 文章标题
- POJ2492 A Bug's Life
- app中扩大按钮(UIButton)响应区域
- 1000
- 如何滚动到UITabelView的底部(bottom)
- pod search XXX 库版本低于 Github 上库最新版本
- Mac OS X El Capitan 下安装 Scrapy