Til the Cows Come Home(简单的最短路)
Til the Cows Come Home
Bessie 在外地,想要在 Farmer John 叫醒她早上挤奶之前回到谷仓尽可能多地睡觉。Bessie 需要她的美容觉,所以她想尽快回来。
Farmer John 的田地中有 N (2 <= N <= 1000) 个地标,唯一编号为 1…N。地标 1 是谷仓;Bessie 整天站立的苹果树树林是 N 的地标。奶牛使用 T (1 <= T <= 2000) 条地标之间不同长度的双向牛道在田间行进。Bessie 对自己的导航能力没有信心,所以一旦开始,她总是从头到尾留在一条小路上。
给定地标之间的小径,确定 Bessie 必须步行才能返回谷仓的最小距离。保证存在一些这样的路由。
输入
第 1 行:两个整数:T 和 N
第 2…T+1 行:每行将一条路径描述为三个空格分隔的整数。前两个整数是路径经过的地标。第三个整数是路径的长度,范围是 1…100。
输出第 1 行:单个整数,即 Bessie 从地标 N 到地标 1 必须经过的最小距离。
样本输入
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
样本输出
90
暗示
输入细节:
有五个地标。
输出详细信息:
Bessie 可以沿着路径 4、3、2 和 1 回家。
思路:简单的迪杰斯特拉,不过可能有一个坑,如本来给出
x y 2,后面又给出,x y 20,即重边问题。
#include <cstdio>
#include <queue>
#include <climits>
#include <string.h>
#include <iostream>
using namespace std;
int vist[1005];
int dis[1005];
int e[1005][1005];
int main()
{int n,m,x,y,zhi,inf,i;while(cin>>m>>n){memset(vist,0,sizeof(vist));memset(e,0x3f3f3f,sizeof(e));inf=e[0][0];for(i=1;i<=m;i++){scanf("%d%d%d",&x,&y,&zhi);if(zhi<e[x][y]){//如果遇到重边,选择最小的边e[x][y]=zhi;e[y][x]=zhi;} }for(i=1;i<=n;i++){//dis[i]保存的是起点到i点的最短距离dis[i]=e[1][i];}int cnt=0,d,xiao;cnt++;vist[1]=1;while(cnt<n){//n个点只需要选n-1条边xiao=inf;d=-1;for(i=1;i<=n;i++){if(!vist[i]&&xiao>dis[i]){//选则起点到i点最小的边xiao=dis[i];d=i;}}if(d==-1) break;cnt++;vist[d]=1;//标记这个点已经选了for(i=1;i<=n;i++){if(!vist[i]&&e[d][i]+dis[d]<dis[i]){//通过d点到i的距离比直接到i的距离小,所以更新dis[i]=e[d][i]+dis[d];}}} printf("%d\n",dis[n]);}
}
Til the Cows Come Home(简单的最短路)相关推荐
- A - Til the Cows Come Home POJ - 2387
A - Til the Cows Come Home POJ - 2387 最短路 #include<iostream> #include<cstdio> #include&l ...
- Til the Cows Come Home(最短路-Dijkstra)
Til the Cows Come Home(最短路-Dijkstra) judge:https://vjudge.net/contest/297882#problem/A Time limit:10 ...
- POJ 2387 Til the Cows Come Home (最短路径 模版题 三种解法)
原题链接:Til the Cows Come Home 题目大意:有 个点,给出从 点到 点的距离并且 和 是互相可以抵达的,问从 到 的最短距离. 题目分析:这是一道典型的最短路径模版 ...
- 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 ...
- Poj 2387 Til the Cows Come Home 迪杰斯特拉(普通+优化)
Til the Cows Come Home 迪杰斯特拉(普通+优化) 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回来. 农场主约翰的田里有n( ...
- Til the Cows Come Home (最短路问题, 模板)
题目:https://vjudge.net/problem/POJ-2387 Bessie is out in the field and wants to get back to the barn ...
- 【POJ2387】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 bef ...
- 【POJ - 2387】 Til the Cows Come Home(单源最短路Dijkstra算法)
题干: Bessie is out in the field and wants to get back to the barn to get as much sleep as possible be ...
- 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 be ...
最新文章
- android使用okthtp
- 第一次接触 SharpHsql(纯C#开源数据库引擎)
- PHP中遍历stdclass object 及 json 总结[中国航天神舟十号以json形式向地面返回数据]...
- 技术系列课|“主动降噪”到底有多厉害?
- leetcode 593. Valid Square | 593. 有效的正方形(Java)
- 【EF学习笔记07】----------加载关联表的数据 贪婪加载
- 2014.4.21 福州 晴 离京第一次面试(某天) 失败啊
- HTML- 锚点实例
- Vue 配置请求本地Json数据
- vue怎么编辑已有视频_vue如何编辑视频 vue编辑视频方法
- 会议会展活动管理软件可实现哪些功能
- 牛客网sql练习题解(34-42)
- 假定一种编码的编码范围是a-y的25个字母,形成一个数组如下: a,aa,aaa,aaaa,aaab,aaac,...yyyy 其中a的Index为0aa的Index为1aaa为2,以此类推。
- 灯管实验的em算法_【大学物理实验】日光灯电路的组装及参数测量.ppt
- AlphaFold2代码阅读(一)
- CUDA Installer 前面的 X
- 用计算机术语写情书,大学各专业学霸情书火了,医学专业浪漫风趣,数学专业很难看懂...
- 【转】通信人必看之区别:S/N,C/N,Eb/N0,Ec/N0,Es/N0
- 【ZYNQ Ultrascale+ MPSOC FPGA教程】第十三章 RS485实验
- 用Python将多个Excel的sheet分别合并到一个Excel的不同sheet