HPUOJ---2017寒假训练--专题3/A-Til the Cows Come Home(最短路-dijkstra)
来源:互联网 发布:讨鬼传2最美捏脸数据 编辑:程序博客网 时间:2024/04/30 22:40
A - 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<cstdio>#define MAX 999999999int dp[1111][1111];int dis[1111];int vis[1111];int T,N,st,ed;int min(int a,int b){return (a<b)?a:b;}void init(){for(int i=1;i<=N;i++){dis[i]=MAX;vis[i]=0;for(int j=1;j<=N;j++) dp[i][j]=dp[j][i]=MAX;}}void dijkstra(){dis[st]=0;while(1){int v=-1;for(int i=1;i<=N;i++){if(!vis[i]&&(v==-1||dis[i]<dis[v])) v=i;}if(v==-1) break;vis[v]=1;for(int i=1;i<=N;i++){if(dis[i]>(dis[v]+dp[v][i])) dis[i]=dis[v]+dp[v][i];}}}int main(){scanf("%d%d",&T,&N);init();while(T--){int a,b,c;scanf("%d%d%d",&a,&b,&c);dp[a][b]=dp[b][a]=min(c,dp[a][b]);}st=1;ed=N;dijkstra();printf("%d\n",dis[ed]);return 0;}
0 0
- HPUOJ---2017寒假训练--专题3/A-Til the Cows Come Home(最短路-dijkstra)
- poj2387~Til the Cows Come Home(最短路dijkstra)
- poj Til the Cows Come Home (Dijkstra最短路~)
- POJ 2387 Til the Cows Come Home 最短路-Dijkstra
- POJ 2387 || Til the Cows Come Home(最短路,dijkstra
- [kuangbin带你飞]专题四 最短路练习 -A - Til the Cows Come Home
- 最短路----Til the Cows Come Home
- Til the Cows Come Home 【最短路】
- 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(最短路SPFA+Dijkstra)
- POJ 2387 Til the Cows Come Home(最短路dijkstra)
- POJ - 2387 Til the Cows Come Home(最短路Dijkstra模板题)
- POJ 2387 Til the Cows Come Home 【最短路(dijkstra)】
- Til the Cows Come Home(最短路—Dijkstra算法)
- Til the Cows Come Home(最短路)
- poj2387- Til the Cows Come Home (最短路)
- 全面理解Java内存模型
- 怎么解决maven管理jar包冲突问题
- VMware使用nat模式使主机系统和虚拟系统通信
- Container With Most Water问题及解法
- C#oop体检套餐管理系统
- HPUOJ---2017寒假训练--专题3/A-Til the Cows Come Home(最短路-dijkstra)
- Java多线程之Synchronized应用在static和非static方法上的区别
- MUI框架学习之[事件取消][事件触发][手势事件][自定义事件]
- jdbcTemplate的queryForList
- xhprof php7问题
- String(字符串)数目统计
- android广播中实现弹出窗口
- Java源代码阅读——HashMap实现原理
- Java 8 有用的新特性