Time Limit: 1000MS
Memory Limit: 65536K

Total Submissions: 59755
Accepted: 20336

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.

Source

USACO 2004 November

题解:

        ①啊其实就是粘一个BFS-SPFA的板子。

#include<stdio.h>
#define go(i,a,b) for(int i=a;i<=b;i++)
#define fo(i,a,x) for(int i=a[x],v=e[i].v;i;i=e[i].next,v=e[i].v)
struct E{int v,next,w;}e[5002];
int n,m,head[2003],d[2003],k=1,U,V,W;
void ADD(int u,int v,int w){e[k]=(E){v,head[u],w};head[u]=k++;}
void dfs(int u){fo(i,head,u)if(d[u]+e[i].w<d[v])d[v]=d[u]+e[i].w,dfs(v);}
int main()
{
scanf("%d%d",&m,&n);
go(i,1,m)scanf("%d%d%d",&U,&V,&W),ADD(U,V,W),ADD(V,U,W);
go(i,1,n)d[i]=1e9;d[1]=0;dfs(1);printf("%d\n",d[n]);return 0;
}//Paul_Guderian

你要成为那美丽的向阳花,在布满创痛的凄风苦雨中,

坚韧地辉煌地绽放……——————汪峰《向阳花》

【POJ 2387 Til the Cows Come Home】的更多相关文章

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

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

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

  3. POJ 2387 Til the Cows Come Home

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

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

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

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

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

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

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

  9. POJ 2387 Til the Cows Come Home (最短路径 模版题 三种解法)

    原题链接:Til the Cows Come Home 题目大意:有  个点,给出从  点到  点的距离并且  和  是互相可以抵达的,问从  到  的最短距离. 题目分析:这是一道典型的最短路径模版 ...

随机推荐

  1. percona-zabbix-templates插件安装监控MySQL

    前期准备:被监控机已经安装好php 1.在zabbix客户端安装mysql监控插件rpm包 rpm -ivh https://www.percona.com/downloads/percona-mon ...

  2. C++继承权限

    牢记两句话: 1.三种继承方式不影响子类对父类的访问权限,子类对父类的访问权限取决于父类的访问控制权. 2.三种继承方式是为了控制子类的调用方对父类的访问权限.比如private继承,对于子类的调用方 ...

  3. C语言进阶—— 单引号和双引号14

    单引号和双引号 C语言中的单引号用来表示字符字面量 C语言中的双引号用来表示字符串字面量 ‘a’表示字符字面量,在内存中占用一个字节,'a'+1表示'a'的ASCII码加1,结果为'b' " ...

  4. C# 设置窗口大小为不可调、取消最大化、最小化窗口按键

    this.FormBorderStyle = FormBorderStyle.FixedDialog;//设置边框为不可调节 this.MaximizeBox = false; //取消最大化按键 t ...

  5. 霍夫圆检测 opencv

    进行霍夫圆变换中有一个API:HoughCircles(). 第五个参数为double类型的minDist(),为霍夫变换检测到的圆的圆心之间的最小距离,即让算法能明显区分的两个不同圆之间的最小距离. ...

  6. [Codeforces958A2]Death Stars (medium)(字符串+hash)

    Description 题目链接 Solution 这里用类似hash的方法将判断2个矩阵是否相同的时间降为O(m),总时间复杂度为O(m3) Code #include <cstdio> ...

  7. 17-比赛1 B - 子串计算 Chef and his string challenge (string的运用)

    Chef's best friend Jerry gives Chef a string A and wants to know the number of string A that can be ...

  8. mysql学习第三天练习(日期和时间函数)

    -- 日期和时间函数 -- 获取当前日期 select curdate(),current_date() from dual -- 返回服务器当前的日期和时间 select NOW(),SYSDATE ...

  9. 在MAC下使用Robotframework+Selenium2【第二枪】如何处理Table点击指定记录

    1.通过关键字Get Matching Xpath Count获取table中的记录 2.遍历Table所有记录 3.判断记录是否符合条件,做点击操作

  10. 使用闭包和lambda解决问题与常规方式解决问题的对比。

    先来描述一下问题吧,游戏中的物品原来只有一个属性加成:攻击,防御,获得经验加成,金币加成,等等.现在要增加一个属性,这个属性可以为之前的属性之一. 这个属性加成涉及到类里的三个属性,value,typ ...