POJ-2387-Til the Cows Come Home(最短路)
Til the Cows Come Home
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 72844 | Accepted: 24344 |
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
Hint
INPUT DETAILS:
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
模板题;
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#define oo 0x3f3f3f3f
using namespace std;
const int N = 10009, M = 20018;
struct Edge
{
int to;
int va;
int next;
} edge[N];
int head[N], k;
bool visit[N];
int dist[N];
void Addedge( int x, int y, int v )
{
edge[k].to = y;
edge[k].va = v;
edge[k].next = head[x];
head[x] = k++;
}
void Init( int n, int m )
{
int i, x, y, v;
memset( head, -1, sizeof( head ) );
k = 1;
for( i=1; i<=m; i++ )
{
cin >> x >> y >> v;
Addedge( x, y, v);
Addedge( y, x, v);
}
fill( visit, visit+n+1, false );
fill( dist, dist+n+1, oo );
}
queue <int> Q;
void SPFA ( int s )
{
visit[s] = true;
dist[s] = 0;
Q.push(s);
while( !Q.empty() )
{
int v = Q.front();
Q.pop();
visit[v] = false;
for( int i=head[v]; i!=-1; i=edge[i].next )
{
int w = edge[i].to;
if( dist[w] > dist[v] + edge[i].va )
{
dist[w] = dist[v] + edge[i].va;
if( visit[w] == false )
{
Q.push(w);
visit[w] = true;
}
}
}
}
}
int main()
{
int n, m, s;
while( cin >> m >> n )
{
Init( n, m );
s = n;
SPFA( s );
cout << dist[1] << endl;
}
return 0;
}
POJ-2387-Til the Cows Come Home(最短路)的更多相关文章
- POJ 2387 Til the Cows Come Home(最短路模板)
题目链接:http://poj.org/problem?id=2387 题意:有n个城市点,m条边,求n到1的最短路径.n<=1000; m<=2000 就是一个标准的最短路模板. #in ...
- POJ 2387 Til the Cows Come Home --最短路模板题
Dijkstra模板题,也可以用Floyd算法. 关于Dijkstra算法有两种写法,只有一点细节不同,思想是一样的. 写法1: #include <iostream> #include ...
- 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 ba ...
- 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 ...
随机推荐
- 563. Binary Tree Tilt 子节点差的绝对值之和
[抄题]: Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as ...
- AM使用指南:如何在Managed Bean中获取AM实例?
AM是放置服务方法的地方,有时我们需要在Managed Bean中调用这些方法.要调用这些方法,首先要在Managed Bean中获取AM实例.这里要用到<ADF工具类:ADFUtil.java ...
- SQL Server 2005/2008/2012中应用分布式分区视图
自2000版本起,SQL Server企业版中引入分布式分区视图,允许你为分布在不同的SQL 实例的两个或多个水平分区表创建视图. 简要步骤如下:根据Check约束中定义的一组值把大表分割成更小的一些 ...
- IPP库下FFT的基本实现
首先感谢韩昊同学,他的傅里叶分析入门给我们对数学公式不熟悉的人了解傅里叶算法打开了一扇窗户,其原文发表于知乎:https://zhuanlan.zhihu.com/p/19763358 在了解其基本原 ...
- centOS下JDK1.8的安装
1.下载JDK1.8 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 有两种格式 ...
- 执行“hdfs dfs -ls”时报ConnectException
原因可能是指定的端口号不对,该端口号由hdfs-site.xml中的属性"dfs.namenode.rpc-address"指定,即为NameNode的RPC服务端口号. 文件上传 ...
- numpy.convolve()
卷积函数: numpy.convolve(a, v, mode='full') Parameters: a : (N,) array_like First one-dimensional input ...
- Android滑动冲突解决方法
叙述 滑动冲突可以说是日常开发中比较常见的一类问题,也是比较让人头疼的一类问题,尤其是在使用第三方框架的时候,两个原本完美的控件,组合在一起之后,忽然发现整个世界都不好了. 关于滑动冲突 滑动冲突分类 ...
- SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)(Finchley版本)
在上一篇文章,讲了服务的注册和发现.在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式,一种是ribbon+r ...
- C++语言运算符的功能、优先级和结合性
优先级 运算符 名称或含义 使用形式 结合方向 说明 1 [] 数组下标 数组名[常量表达式] 左到右 () 圆括号 (表达式)/函数名(形参表) . 成员选择(对象) 对象.成员名 -& ...