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. Tensorflow【实战Google深度学习框架】TensorFlow模型的保存与恢复加载
  2. 一文带你弄懂普里姆(Prim)算法和克鲁斯卡尔(Kruskal)算法
  3. 钉钉下载与安装过程 适用于windows系统 20200718
  4. 根据Ibatis的SqlMap配置文件生成表结构
  5. 手把手教你用Python来模拟绘制自由落体运动过程中的抛物线(附源码)
  6. 绝对定位(HTML、CSS)
  7. 剑指offer——面试题46:求1+2+...+n
  8. C语言程序设计第9堂作业
  9. verilog实现四位全加器(基于一位全加器)
  10. 华为防火墙IPsec点对点配置解析
  11. 问题 B: 栈的操作问题
  12. 【项目】前端实习——知识库项目总结
  13. Cesium geojson数据的添加与移除
  14. 桂电免出校器实现自动宽带拨号,免除你每天拨号的烦恼——路由器以斐讯K2为例
  15. Switch控件详解
  16. CCS5.5.0资源及license(免费下载)
  17. NDK from ndk.dir at ndk-bundle had version [22.1.7171670] which disagrees with android.ndkVersion [2
  18. 使用国内的WSUS服务
  19. 二值化图片数据解码显示(Grayscale8、Grayscale16、RGB888)————附带Qt版完整代码
  20. uniapp修改H5页面浏览器标签栏小图标

热门文章

  1. 端到端与点到点到底是什么?
  2. 泛娱乐生态倒逼,各大视频为何抢位3D动画?
  3. 我为什么放弃360千万期权,走向创业这条搬砖路?
  4. 二,八,十六进制数转换为十进制数
  5. 9,共模抑制比一-不受输入信号中共模波动的影响。【如何分析共模CM抑制比。】
  6. XDoj 1037 希希的多项式 (python)
  7. 测距必备,8个超声波测距方案,实时更可控
  8. 生成Solr增量索引配置xml
  9. c语言for循环多条件判断,解决在for循环内判断条件多次执行
  10. 博文视点译者招募令!