POJ 2387 Til the Cows Come Home (图论,最短路径)
POJ 2387 Til the Cows Come Home (图论,最短路径)
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
Http
POJ:https://vjudge.net/problem/POJ-2387
Source
图论,最短路径
解决思路
最短路径,直接用spfa可以解决
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int maxN=1001;
const int maxM=2001;
const int inf=2147483647;
class Edge
{
public:
int v,w;
};
int n,m;
vector<Edge> E[maxN];
int Dist[maxN];
queue<int> Q;
bool inqueue[maxN];
int main()
{
cin>>m>>n;
for (int i=1;i<=m;i++)
{
int u,v,w;
cin>>u>>v>>w;
E[u].push_back((Edge){v,w});
E[v].push_back((Edge){u,w});
}
for (int i=1;i<=n;i++)
Dist[i]=inf;
memset(inqueue,0,sizeof(inqueue));
Dist[n]=0;
inqueue[n]=1;
Q.push(n);
do
{
int u=Q.front();
Q.pop();
inqueue[u]=0;
for (int i=0;i<E[u].size();i++)
{
int v=E[u][i].v;
int w=E[u][i].w;
if (Dist[u]+w<Dist[v])
{
Dist[v]=Dist[u]+w;
if (inqueue[v]==0)
{
Q.push(v);
inqueue[v]=1;
}
}
}
}
while (!Q.empty());
cout<<Dist[1]<<endl;
return 0;
}
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(Dijkstra 最短路径)
题目:从节点N到节点1的求最短路径. 分析:这道题陷阱比较多,首先是输入的数据,第一个是表示路径条数,第二个是表示节点数量,在 这里WA了四次.再有就是多重边,要取最小值.最后就是路径的长度的最大值不 ...
- POJ.2387 Til the Cows Come Home (SPFA)
POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...
- POJ 2387 Til the Cows Come Home
题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K ...
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
- 怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33015 Accepted ...
- POJ 2387 Til the Cows Come Home (最短路 dijkstra)
Til the Cows Come Home 题目链接: http://acm.hust.edu.cn/vjudge/contest/66569#problem/A Description Bessi ...
- POJ 2387 Til the Cows Come Home 【最短路SPFA】
Til the Cows Come Home Description Bessie is out in the field and wants to get back to the barn to g ...
- POJ 2387 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 as much s ...
随机推荐
- 20155223 Exp3 免杀原理与实践 实验报告
20155223 Exp3 免杀原理与实践 实验报告 实验前准备 安装Veil-Evasion 直接使用Kali快速安装命令,拒绝采用git安装命令 apt-get -y install veil-e ...
- 20155229《网络对抗技术》Exp:网络欺诈防范
实验内容 (1)简单应用SET工具建立冒名网站 (2)ettercap DNS spoof (3)结合应用两种技术,用DNS spoof引导特定访问到冒名网站. 实验步骤 简单应用SET工具建立冒名网 ...
- # 2017-2018-2 20155319《网络对抗技术》Exp9 :Web安全基础
2017-2018-2 20155319<网络对抗技术>Exp9 :Web安全基础 实践过程 webgoat准备 从GitHub上下载jar包(老师的虚拟机中有 无需下载) 拷贝到本地,并 ...
- TPM及TSS协议栈的安装使用
TPM及TSS协议栈的安装 标签: 可信计算. 目录 安装环境介绍 TPM及TSS安装 软件包下载 TPM 安装 安装TSS 安装tpm-tools 交互过程 编写代码测试TPM是否可用 编写代码测试 ...
- jira webhook 事件触发并程序代码调用jenkins接口触发构建操作
要解决的问题 开发管理工具触发站点构建事件,事件处理中需要调用Jenkins接口开始构建动作. 我的应用场景: 使用jira作为管理工具,在jira中创建自定义的工作流来规定测试,上线,发布等流程,并 ...
- 从头到尾谈一下HTTPS
引言 “你能谈一下HTTPS吗?” “一种比HTTP安全的协议.” “...” 如果面试这样说的话那差不多就gg了,其实HTTPS要展开回答的话内容还挺丰富的.本篇文章详细介绍了HTTPS是什么.为什 ...
- 镜像仓库管理:与Portus不得不说的那些事
背景: 目前在做一个云计算相关的项目,其中有这样一个需求:每个平台用户都有自己的docker镜像仓库(docker registry),用户可以对自己的镜像仓库的push/pull权限进行管理,也就是 ...
- PAT甲题题解-1004. Counting Leaves (30)-统计每层叶子节点个数+dfs
统计每层的叶子节点个数建树,然后dfs即可 #include <iostream> #include <cstdio> #include <algorithm> # ...
- Python中的字典详解
https://www.cnblogs.com/yjd_hycf_space/p/6880026.html
- Beta任务项录入
今天PM把任务项整理写入TFS中,明天开始正式开发工作: