Time Limit: 1000MS     Memory Limit: 65536KB     64bit IO Format: %lld & %llu

SubmitStatus

Description

Input

Output

Sample Input

Sample Output

Hint

Description

某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。

现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。

Input

本题目包含多组数据,请处理到文件结束。
每组数据第一行包含两个正整数N和M(0<N<200,0<M<1000),分别代表现有城镇的数目和已修建的道路的数目。城镇分别以0~N-1编号。
接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B<N,A!=B,0<X<10000),表示城镇A和城镇B之间有一条长度为X的双向道路。
再接下一行有两个整数S,T(0<=S,T<N),分别代表起点和终点。

Output

对于每组数据,请在一行里输出最短需要行走的距离。如果不存在从S到T的路线,就输出-1.

Sample Input

3 3
0 1 1
0 2 3
1 2 1
0 2
3 1
0 1 1
1 2

Sample Output

2
-1

Hint

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 5
1 2 20
2 3 30
3 4 20
4 5 20
1 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.

直接用dijikstra求第N个点的单元最短路径,然后再输出第一个点到第N个点的最短路径距离便可。
但是要注意的是数据输入时的判断,可能会有相同两点多条路径,一定要保留最短的那条的数据。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define Inf 0x3fffffff
using namespace std;
int mp[1005][1005];
int dis[1005];
void dijkstra(int n)
{int vis[1001]={0};int minn,i,j,k;vis[1]=1;for(i=1;i<n;++i){minn=Inf;k=1;for(j=1;j<=n;++j){if(!vis[j]&&minn>dis[j]){minn=dis[j];k=j;}}vis[k]=1;for(j=1;j<=n;++j){if(!vis[j]&&dis[j]>dis[k]+mp[k][j])dis[j]=dis[k]+mp[k][j];}}printf("%d\n",dis[n]);
}int main()
{int t,n,i,j,s,t,val;while(scanf("%d%d",&t,&n)!=EOF){for(i=1;i<=n;++i){mp[i][i]=0;for(j=1;j<i;++j)mp[i][j]=mp[j][i]=Inf;}for(i=1;i<=t;++i){scanf("%d%d%d",&s,&t,&val);if(val<mp[s][t])mp[s][t]=mp[t][s]=val;}for(i=1;i<=n;++i)dis[i]=mp[1][i];dijkstra(n);}return 0;
}

Dijkstra-POJ-2387-Til the Cows Come Home相关推荐

  1. POJ 2387 Til the Cows Come Home (最短路径 模版题 三种解法)

    原题链接:Til the Cows Come Home 题目大意:有  个点,给出从  点到  点的距离并且  和  是互相可以抵达的,问从  到  的最短距离. 题目分析:这是一道典型的最短路径模版 ...

  2. Poj 2387 Til the Cows Come Home 迪杰斯特拉(普通+优化)

    Til the Cows Come Home 迪杰斯特拉(普通+优化) 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回来. 农场主约翰的田里有n( ...

  3. poj 2387 Til the Cows Come Home dijkstra

    题意: 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回来. 农场主约翰的田里有n(2<=n<=1000)个地标,唯一编号为1-n.地标 ...

  4. POJ 2387 Til the Cows Come Home

    传送门:http://poj.org/problem?id=2387 这是最短路径问题,本题有重边,但是spfa能解决这个问题: 实现代码: SPFA: 1 #include <iostream ...

  5. POJ - 2387 Til the Cows Come Home

    感觉一直写的dij堆优化都是假的,最短路板子spfa+ dij堆优化 SPFA #include<stdio.h> #include<string.h> #include< ...

  6. 【POJ】2387 Til the Cows Come Home

    题目链接:http://poj.org/problem?id=2387 题意:求从1到n的最短路 题解:板子题.spfa. 代码: 1 #include<iostream> 2 #incl ...

  7. pku 2387 Til the Cows Come Home

    原来一直没去学spfa,感觉只一个Dij已经很够用了,昨天翻了一下最短路,如果路径中存在负权的话,Dij也只能素手无策,这时spfa就大显身手了.. 实现过程: 我们用数组d记录每个结点的最短路径估计 ...

  8. A - Til the Cows Come Home POJ - 2387

    A - Til the Cows Come Home POJ - 2387 最短路 #include<iostream> #include<cstdio> #include&l ...

  9. DIJSPFA-Til the Cows Come Home POJ - 2387

    Til the Cows Come Home POJ - 2387 用dij和spfa写了做个对比,看了一篇很好的文章对这两个算法有了更好的理解**<Dijkstra+heap和SPFA的区别& ...

  10. Til the Cows Come Home(dijkstra)

    题目连接: Til the Cows Come Home 题目: Bessie is out in the field and wants to get back to the barn to get ...

最新文章

  1. dev chartcontrol获取x y轴的值_2020年深圳蛇口x情怀当铺展览详情(时间+地点+门票)...
  2. Rhel5.6下构建在线邮件服务系统并实现不同网段不同域名间的邮件互发
  3. 智慧办公的AI博弈——看飞企互联如何接招!
  4. 20220208--CTF刷题记录--6道简单的MISC题目
  5. dbeaver 数据转化 mapping_Python机器学习实例:数据竞赛-足球运动员身价估计
  6. 2 JVM 运行机制
  7. mysql 复制数据_MySQL快速复制数据库数据表的方法
  8. Activity工作流工作笔记001---快速上手_认识工作流
  9. MySQL数据库基本操作1
  10. ArcGIS API for Silverlight 调用GP服务绘制等值面
  11. 未解决:maven:Fatal error compiling: 无效的标记: -arg
  12. linux下exe软件反编译工具下载,ilspy.exe
  13. 【北京迅为】i.MX6ULL终结者RS232驱动测试RS485测试
  14. 算法竞赛入门经典(第二版) —— 第一章 程序设计入门
  15. JAVA对象转Json对象
  16. 飞控信号SBUS信号解析为PWM信号输出
  17. 【SQL】数据库模糊查询
  18. JS实现获取今天星期几
  19. SSL2893 谷仓的安保
  20. C# 几种选择文件Filter文件后缀很全

热门文章

  1. jenkins找不到Build when a change is pushed to GitHub
  2. 计算机基础知识与公文写作,公文写作与计算机基础知识.doc
  3. 区块链vs.DAG, 区别到底是什么? 一文读懂烧脑的数据结构之争
  4. 价格奥秘-在超市遇见亚当斯密--第二章 谁在决定一支铅笔的价格?
  5. 56网首发2012APEC青创会主题微电影
  6. 17 -Flask构建弹幕微电影网站- 电影播放及评论弹幕收藏实现
  7. GEE|下载研究区哨兵二号影像
  8. 写毕业论文期间的一些收获和感想
  9. STM32的超声波测距程序
  10. 喜马拉雅java社招面试_(转)喜马拉雅2018 Java面试题目