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(简单的最短路)相关推荐

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

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

  2. Til the Cows Come Home(最短路-Dijkstra)

    Til the Cows Come Home(最短路-Dijkstra) judge:https://vjudge.net/contest/297882#problem/A Time limit:10 ...

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

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

  4. 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 ...

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

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

  6. 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 ...

  7. 【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 ...

  8. 【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 ...

  9. 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 ...

最新文章

  1. android使用okthtp
  2. 第一次接触 SharpHsql(纯C#开源数据库引擎)
  3. PHP中遍历stdclass object 及 json 总结[中国航天神舟十号以json形式向地面返回数据]...
  4. 技术系列课|“主动降噪”到底有多厉害?
  5. leetcode 593. Valid Square | 593. 有效的正方形(Java)
  6. 【EF学习笔记07】----------加载关联表的数据 贪婪加载
  7. 2014.4.21 福州 晴 离京第一次面试(某天) 失败啊
  8. HTML- 锚点实例
  9. Vue 配置请求本地Json数据
  10. vue怎么编辑已有视频_vue如何编辑视频 vue编辑视频方法
  11. 会议会展活动管理软件可实现哪些功能
  12. 牛客网sql练习题解(34-42)
  13. 假定一种编码的编码范围是a-y的25个字母,形成一个数组如下: a,aa,aaa,aaaa,aaab,aaac,...yyyy 其中a的Index为0aa的Index为1aaa为2,以此类推。
  14. 灯管实验的em算法_【大学物理实验】日光灯电路的组装及参数测量.ppt
  15. AlphaFold2代码阅读(一)
  16. CUDA Installer 前面的 X
  17. 用计算机术语写情书,大学各专业学霸情书火了,医学专业浪漫风趣,数学专业很难看懂...
  18. 【转】通信人必看之区别:S/N,C/N,Eb/N0,Ec/N0,Es/N0
  19. 【ZYNQ Ultrascale+ MPSOC FPGA教程】第十三章 RS485实验
  20. 用Python将多个Excel的sheet分别合并到一个Excel的不同sheet

热门文章

  1. 遗传算法之01背包问题 (C语言实现)
  2. Django--中间件
  3. tomcat 历史版本下载
  4. 《调色师手册:电影和视频调色专业技法(第2版)》——调色所需的其他硬件...
  5. Linux (centos7)安装字体
  6. 使用Systemctl命令来管理系统服务
  7. 12个助记词(24个助记词)怎么保存最安全
  8. 芯华章完成数亿元A+轮融资,红杉宽带数字产业基金领投,高瓴、高榕跟投|钛媒体首发...
  9. 内网服务器安装docker
  10. CVE-2017-8464远程命令执行漏洞(震网漏洞)复现