Dijkstra-POJ-2387-Til the Cows Come Home
Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu
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
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
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
* 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
Sample Input
5 5 1 2 20 2 3 30 3 4 20 4 5 20 1 5 100
Sample Output
90
Hint
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
但是要注意的是数据输入时的判断,可能会有相同两点多条路径,一定要保留最短的那条的数据。
#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相关推荐
- POJ 2387 Til the Cows Come Home (最短路径 模版题 三种解法)
原题链接:Til the Cows Come Home 题目大意:有 个点,给出从 点到 点的距离并且 和 是互相可以抵达的,问从 到 的最短距离. 题目分析:这是一道典型的最短路径模版 ...
- Poj 2387 Til the Cows Come Home 迪杰斯特拉(普通+优化)
Til the Cows Come Home 迪杰斯特拉(普通+优化) 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回来. 农场主约翰的田里有n( ...
- poj 2387 Til the Cows Come Home dijkstra
题意: 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回来. 农场主约翰的田里有n(2<=n<=1000)个地标,唯一编号为1-n.地标 ...
- POJ 2387 Til the Cows Come Home
传送门:http://poj.org/problem?id=2387 这是最短路径问题,本题有重边,但是spfa能解决这个问题: 实现代码: SPFA: 1 #include <iostream ...
- POJ - 2387 Til the Cows Come Home
感觉一直写的dij堆优化都是假的,最短路板子spfa+ dij堆优化 SPFA #include<stdio.h> #include<string.h> #include< ...
- 【POJ】2387 Til the Cows Come Home
题目链接:http://poj.org/problem?id=2387 题意:求从1到n的最短路 题解:板子题.spfa. 代码: 1 #include<iostream> 2 #incl ...
- pku 2387 Til the Cows Come Home
原来一直没去学spfa,感觉只一个Dij已经很够用了,昨天翻了一下最短路,如果路径中存在负权的话,Dij也只能素手无策,这时spfa就大显身手了.. 实现过程: 我们用数组d记录每个结点的最短路径估计 ...
- A - Til the Cows Come Home POJ - 2387
A - Til the Cows Come Home POJ - 2387 最短路 #include<iostream> #include<cstdio> #include&l ...
- DIJSPFA-Til the Cows Come Home POJ - 2387
Til the Cows Come Home POJ - 2387 用dij和spfa写了做个对比,看了一篇很好的文章对这两个算法有了更好的理解**<Dijkstra+heap和SPFA的区别& ...
- 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 ...
最新文章
- Tensorflow【实战Google深度学习框架】TensorFlow模型的保存与恢复加载
- 一文带你弄懂普里姆(Prim)算法和克鲁斯卡尔(Kruskal)算法
- 钉钉下载与安装过程 适用于windows系统 20200718
- 根据Ibatis的SqlMap配置文件生成表结构
- 手把手教你用Python来模拟绘制自由落体运动过程中的抛物线(附源码)
- 绝对定位(HTML、CSS)
- 剑指offer——面试题46:求1+2+...+n
- C语言程序设计第9堂作业
- verilog实现四位全加器(基于一位全加器)
- 华为防火墙IPsec点对点配置解析
- 问题 B: 栈的操作问题
- 【项目】前端实习——知识库项目总结
- Cesium geojson数据的添加与移除
- 桂电免出校器实现自动宽带拨号,免除你每天拨号的烦恼——路由器以斐讯K2为例
- Switch控件详解
- CCS5.5.0资源及license(免费下载)
- NDK from ndk.dir at ndk-bundle had version [22.1.7171670] which disagrees with android.ndkVersion [2
- 使用国内的WSUS服务
- 二值化图片数据解码显示(Grayscale8、Grayscale16、RGB888)————附带Qt版完整代码
- uniapp修改H5页面浏览器标签栏小图标