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(最短路)的更多相关文章

  1. POJ 2387 Til the Cows Come Home(最短路模板)

    题目链接:http://poj.org/problem?id=2387 题意:有n个城市点,m条边,求n到1的最短路径.n<=1000; m<=2000 就是一个标准的最短路模板. #in ...

  2. POJ 2387 Til the Cows Come Home --最短路模板题

    Dijkstra模板题,也可以用Floyd算法. 关于Dijkstra算法有两种写法,只有一点细节不同,思想是一样的. 写法1: #include <iostream> #include ...

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

  4. POJ.2387 Til the Cows Come Home (SPFA)

    POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...

  5. POJ 2387 Til the Cows Come Home

    题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K ...

  6. POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)

    传送门 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46727   Acce ...

  7. 怒学三算法 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 ...

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

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

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

随机推荐

  1. cocos2d-js反射

    如何在android平台上使用js直接调用Java方法 在cocos2d-js 3.0beta中加入了一个新特性,在android平台上我们可以通过反射直接在js中调用java的静态方法.它的使用方法 ...

  2. Java 代理机制

    Table of Contents 1 引言 2 常见的代理 3 代理模式UML图 4 代理模式实例 5 java动态代理 5.1 java动态代理UML图 6 代理模式与装饰者模式的区别 6.1 装 ...

  3. robotframework中文显示乱码

    问题描述:接口.数据库返回信息有中文的时候会显示unicode的样式,如图 解决方法: 1.robotframework为3.0.X 2.找到Python安装目录下的\Lib\site-package ...

  4. Openssl pkcs7命令

    一.简介 pkcs7命令用于处理DER或者PEM格式的pkcs#7文件.   二.语法 openssl pkcs7 [-inform PEM|DER] [-outform PEM|DER] [-in ...

  5. 读取mysq数据库l数据,并使用dataview显示

    来自<sencha touch权威指南>,约198页开始 通过php脚本,可以将mysql数据库的数据作为json数据格式进行读取. (1)php代码(bookinfo.php): < ...

  6. SQL基础问题(一)

    1.sql是什么? 结构化查询语言(Structured Query Language). 2.结构化查询语言的分类? 数据定义语言(DDL)Data Definition Language   操作 ...

  7. SliceBox

    SliceBox相当于一个轮播图插件,只不过是3D的. 先来查看它能实现的效果: 官网:http://tympanus.net/codrops/2011/09/05/slicebox-3d-image ...

  8. Perl 学习笔记-输入输出

    1.读取标准输入<STDIN>(行输入操作=> 读取一行直到换行符) chomp($line = <STDIN>); # 读取一行并去掉最后的换行符(不会自动去掉) pr ...

  9. java Concurrent包学习笔记(四):BlockingQueue

    一.BlockingQueue概述 1.阻塞的含义 BlockingQueue即阻塞队列,从阻塞这个词可以看出,在某些情况下对阻塞队列的访问可能会造成阻塞.被阻塞的情况主要有如下两种: ,当一个线程对 ...

  10. HDU 3361 ASCII (水题)

    题意: 析:不说话. #include <cstdio> #include <string> #include <cstdlib> #include <cma ...