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 ...
随机推荐
- iOS中wkwebview加载本地html的要点
项目中有些页面,我采用了html页面开发,然后用wkwebview加载的设计.在加载过程中遇见了一些问题,在这里进行一些记载和讨论.如有不同意见欢迎进行评论沟通. 问题时候这样的: 在webview的 ...
- oo原则
基本原则: 封装变化Encapsulate what varies. 面向接口编程而非实现 Code to an interface rather than to an implementation. ...
- Qt webkitwidgets模块和webenginewidgets模块
问题 将Qt开发的程序从Qt5.5或更低的版本迁移到5.6或更高的版本时,会提示webkitwidgets是unknown module. Project ERROR: Unknown module( ...
- code1006 等差数列
我绞尽脑汁想一个更好的算法,然而不能如愿,只好写一个n^3的了 很简单,就是暴力搜索(还好n<100) 先排序,然后循环i=1ton,j=i+1ton 把a[i]a[j]确定为等差数列开始的两个 ...
- Web实践—Rec 1
累计完成任务情况: 阶段内容 参与人 开会学习作业要求,取得共识 全体 注: 1."阶段内容"划斜线表示完成.2.采用倒序. 具体情况: 正式开展实践作业之前的说明: 按照之前达成 ...
- Swig在Mac OS X上的安装
网上有很多类似文章介绍Swig怎么在Mac OS X上安装和配置,一般来说就是: 下载pcre,configure & make & make install 下载swig,confi ...
- javascript总结45: HTML DOM media 属性
定义和用法 media 属性设置或返回显示文档的设备. 对于样式信息而言,目标媒介非常重要.移动设备和桌面计算机的样式可能是不同的. 实例 <html> <head> < ...
- java中的继承(is a )和组合(has a)
我们知道java语言有三大特性:封装,继承,多态 但是继承和封装却是一对有点矛盾的两个方面,怎么理解?? 我们想想:封装的目的是想让隐藏类中的属性和方法.但是在继承过程中,我们的子类肯定会继承父类的方 ...
- zuluCryt cli howto
1.解锁卷的命令. zuluCrypt-cli -o -d /dev/sdc1 -m blabla -e ro -f /home/keyFile zuluCrypt-cli -o -d /dev/sd ...
- [转][译] 存储引擎原理:LSM
原译文地址:http://www.tuicool.com/articles/qqQV7za http://www.zhihu.com/question/19887265 http://blog.csd ...