【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 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
题解
题目大意:给定N个点,T条边
求出从节点1到节点N的最短路径长度。
直接求最短路即可
习惯用SPFA。。。
如果用dijkstra要考虑重边的情况(舍掉之类的)
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
#define MAX 11000
#define MAXL 22000
struct Line
{
int v,next,w;
}e[MAXL];
int u,v,w;
int h[MAX],cnt=1;
int T,N;
queue<int> Q;
bool vis[MAX];
int dis[MAX];
inline void Add(int u,int v,int w)
{
e[cnt]=(Line){v,h[u],w};
h[u]=cnt++;
}
int main()
{
cin>>T>>N;
for(int i=1;i<=T;++i)
{
cin>>u>>v>>w;
Add(u,v,w);
Add(v,u,w);
}
for(int i=1;i<=N;++i)
dis[i]=1050000000;
/*********SPFA***********/
vis[1]=true;dis[1]=0;
Q.push(1);
while(!Q.empty())
{
u=Q.front();Q.pop();
vis[u]=false;
for(int i=h[u];i;i=e[i].next)
{
v=e[i].v;
if(dis[v]>dis[u]+e[i].w)
{
dis[v]=dis[u]+e[i].w;
if(!vis[v])
{
vis[v]=true;
Q.push(v);
}
}
}
}
cout<<dis[N]<<endl;
return 0;
}
【POJ2387】Til the Cows Come Home (最短路)的更多相关文章
- POJ2387 Til the Cows Come Home (最短路 dijkstra)
AC代码 POJ2387 Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to ...
- POJ-2387 Til the Cows Come Home ( 最短路 )
题目链接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to ...
- Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化)
Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化) 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回 ...
- POj2387——Til the Cows Come Home——————【最短路】
A - Til the Cows Come Home Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & ...
- POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37662 Accepted ...
- (Dijkstra) POJ2387 Til the Cows Come Home
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 81024 Accepted ...
- poj2387 Til the Cows Come Home 最短路径dijkstra算法
Description Bessie is out in the field and wants to get back to the barn to get as much sleep as pos ...
- poj2387 Til the Cows Come Home
解题思路:最短路的模板题,注意一个细节处理即可. 见代码: #include<cstdio> #include<cstring> #include<algorithm&g ...
- POJ2387 Til the Cows Come Home 【Dijkstra】
题目链接:http://poj.org/problem?id=2387 题目大意; 题意:给出两个整数T,N,然后输入一些点直接的距离,求N和1之间的最短距离.. 思路:dijkstra求单源最短路, ...
- POJ-2387.Til the Cows Come Home.(五种方法:Dijkstra + Dijkstra堆优化 + Bellman-Ford + SPFA + Floyd-Warshall)
昨天刚学习完最短路的算法,今天开始练题发现我是真的菜呀,居然能忘记邻接表是怎么写的,真的是菜的真实...... 为了弥补自己的菜,我决定这道题我就要用五种办法写出,并在Dijkstra算法堆优化中另外 ...
随机推荐
- 关于git post-receive 钩子
登录 git服务器 进入你项目所在git文件夹 cd /var/opt/gitlab/git-data/repositories/liangyuquan/yfg.git cd hooks vim po ...
- Nginx Rewrite规则详解
Rewrite规则含义就是某个URL重写成特定的URL,从某种意义上说为了美观或者对搜索引擎友好,提高收录量及排名等. Rewrite规则的最后一项参数为flag标记,支持的flag标记主要有以下几种 ...
- 拥抱.NET Core系列:MemoryCache 初识
Cache是一个绝大多数项目会用到的一个技术,说起到缓存可能就联想到 Set.Add.Get.Remove.Clear 这几个方法.那么在.NET Core中微软给我们带来了什么样的缓存体验呢?今天我 ...
- aria2 加速百度网盘下载
准备工作: chrome浏览器: BaiduExporter插件(下载地址:https://github.com/acgotaku/BaiduExporter): aria2工具(下载地址:http: ...
- qt 如何安装 Debuggers 调试器 ?
1.下载 SDK 或 WDK 打开网址:https://developer.microsoft.com/zh-cn/windows/hardware/windows-driver-kit 选择 SDK ...
- 买帽子 (hash)
思路:表示数字i出现的次数,在输入的同时记录每个数字出现的次数.最后从0枚举到1000判断第三个是否存在,存在则记录该数字. #include <stdio.h> #include < ...
- DOCKER 无法获取使用宿主机DNS 的原因,解决方法
今天在公司服务器上部署项目,遇到一个大坑.接口怎么请求都不同,宿主机DNS已经改了.宿主可以请求,找了半天,原来是DOCKER 没有获取到主机的DNS 进去DOCKER解析不了域名 指定DNS 启动也 ...
- SpringBoot SpringSecurity4整合,灵活权限配置,弃用注解方式.
SpringSecurity 可以使用注解对方法进行细颗粒权限控制,但是很不灵活,必须在编码期间,就已经写死权限 其实关于SpringSecurity,大部分类都不需要重写,需要的只是妥善的配置. 每 ...
- linux虚拟化概述
虚拟化硬件虚拟化:一台物理机虚拟出多台逻辑上的计算机cpu,内存可分配给多个虚拟机软件虚拟化:一个LAMP平台支撑多个网站桌面虚拟化...... 虚拟机:通过软件平台模拟出的计算机对最终用户来说,感受 ...
- JLINK 10针J和20针JTAG接口连接方法
我的JLINK终于用上了,哈哈,好开心,终于不用考虑是不是要借用别人的PC机了,昨天到城隍庙电子市场忙活了一下午,终于算是满载而归,呵呵,好了,下面说一下接法,其实根本不需要什么转接板什么的,直接把相 ...