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 ...
 
随机推荐
- 关于HDFS默认block块大小
			
这是有疑惑的一个问题,因为在董西成的<Hadoop技术内幕--深入解析MapReduce架构设计与实现原理>中提到这个值是64M,而<Hadoop权威指南>中却说是128M,到 ...
 - 2-R型聚类
			
将相似的属性聚合在一起 clc, clear; % a = load('E:\a-建模\<数学建模算法与应用>课件资源\数学建模算法与应用\程序及数据\10第10章\gj.txt'); a ...
 - 28-组合数(dfs)
			
http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=32 组合数 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 ...
 - Openssl errstr命令
			
一.简介 errstr命令用于查询错误代码 二.语法 errstr [-stats] <errno> 选项 -stats:打印哈希表状态 errno:错误号 三.实例 1.查看错误信息 : ...
 - [C++] CONST 2
			
The C++ 'const' Declaration: Why & How The 'const' system is one of the really messy features of ...
 - linux环境下搭建osm_web服务器一(Postgresql配置及osm2pgsql原始数据导入):
			
Postgresql配置及osm2pgsql原始数据导入 2012年,Ubuntu 12.04LTS发布,又一个长效支持版,我们又该更新OpenStreetMap服务器了,这次,将详细在博客中记录配置 ...
 - JMS学习之理论基础
			
本文代码使用ActiveMq5.6 一.什么是JMS JMS(Java Message Service,Java消息服务)是一组Java应用程序接口(Java API),它提供创建.发送.接收.读取消 ...
 - C#中利用LINQ to XML与反射把任意类型的泛型集合转换成XML格式字符串
			
在工作中,如果需要跟XML打交道,难免会遇到需要把一个类型集合转换成XML格式的情况.之前的方法比较笨拙,需要给不同的类型,各自写一个转换的函数.但是后来接触反射后,就知道可以利用反射去读取一个类型的 ...
 - windows server2012如何开启mysql远程登录
			
开发的首要任务就是要搭建起自己的服务器,下面主要是我这搭建记录下 我的各种环境 服务器为Windows server2012 安装的MySQL数据的版本是5.6.10 ,64位.当然了版本对于安装没 ...
 - Tomcat 开机自启动
			
一.安装JDK和Tomcat 1,安装JDK:直接运行jdk-7-windows-i586.exe可执行程序,默认安装即可. 备注:路径可以其他盘符,不建议路径包含中文名及特殊符号. 2.安装Tomc ...