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



INPUT DETAILS:



There are five landmarks.


Sample Output



90



OUTPUT DETAILS:



Bessie can get home by following trails 4, 3, 2, and 1.

这题各种单源最短路都能过

#include<cstdio>
#include<cstring>
int n,m,x,y,z,cnt,t,w=1;
int head[10001];
struct edge{
int to,next,v;
}e[50001];
int q[50001];
bool mark[50001];
int dist[50001];
inline void ins(int u,int v,int w)
{
e[++cnt].v=w;
e[cnt].to=v;
e[cnt].next=head[u];
head[u]=cnt;
}
void insert(int u,int v,int w)
{
ins(u,v,w);
ins(v,u,w);
}
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
inline void spfa()
{
q[1]=1;
mark[1]=1;
dist[1]=0;
while (t<w)
{
int now=q[++t];
for (int i=head[now];i;i=e[i].next)
if (dist[now]+e[i].v<dist[e[i].to])
{
dist[e[i].to]=dist[now]+e[i].v;
if (!mark[e[i].to])q[++w]=e[i].to;
}
mark[now]=0;
}
}
int main()
{
m=read();n=read();
memset(dist,127/3,sizeof(dist));
for(int i=1;i<=m;i++)
{
x=read();y=read();z=read();
insert(x,y,z);
}
spfa();
printf("%d",dist[n]);
}

bzoj1752 [Usaco2005 qua]Til the Cows Come Home的更多相关文章

  1. BZOJ1754: [Usaco2005 qua]Bull Math

    1754: [Usaco2005 qua]Bull Math Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 374  Solved: 227[Submit ...

  2. 1753: [Usaco2005 qua]Who's in the Middle

    1753: [Usaco2005 qua]Who's in the Middle Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 290  Solved:  ...

  3. 1754: [Usaco2005 qua]Bull Math

    1754: [Usaco2005 qua]Bull Math Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 398  Solved: 242[Submit ...

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

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

  5. Til the Cows Come Home(最短路)

    Til the Cows Come Home Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I ...

  6. POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37662   Accepted ...

  7. POJ 2387 Til the Cows Come Home

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

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

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

随机推荐

  1. 深入理解linux网络技术内幕读书笔记(一)--简介

    Table of Contents 1 基本术语 1.1 本书常用的缩写 2 引用计数 2.1 引用计数函数 3 垃圾回收 3.1 异步 3.2 同步 4 函数指针 4.1 缺点 5 goto语句 5 ...

  2. linux内存管理--伙伴系统和内存分配器

    3.1页框的管理 所有的页框描述符都存放在mem_map数组中. 3.1.1page数据结构 struct page { page_flags_t flags; //标志 atomic_t _coun ...

  3. phpcms:三、头部包含

    1‘标题:{if isset($SEO['title']) && !empty($SEO['title'])}{$SEO['title']}{/if}{$SEO['site_title ...

  4. Gson源码分析之Json结构抽象和注解使用

    github上的博客地址: http://chuyun923.github.io/blog/2015/01/06/gsonyuan-ma-fen-xi/ XML和Json作为最常用的两种网络传输格式而 ...

  5. Mysql User表为空

    Mysql5.6刚安装完成,未设置过密码,root账号登录提示:root@localhost mysql]# mysqlERROR 1045 (28000): Access denied for us ...

  6. 编译U-boot时,make[1]: *** 没有规则可以创建mkimage.o”

    执行完make smdk2440_config 对Uboot重行编译怎么会出现这样的错误 make[1]: Entering directory `/home/win/S3-ARM/Part4/ubo ...

  7. mysql常用操作(转自阿铭linux)

    在前面两个章节中已经介绍过MySQL的安装了,但是光会安装还不够,您还需要会一些基本的相关操作.当然了,关于MySQL的内容也是非常多的,只不过对于linux系统管理员来讲,一些基本的操作已经可以应付 ...

  8. <经验杂谈>C#/.Net中xml的Serialization序列化与DeSerializetion反序列化

    1.先讲概念:.Net Framework提供了对应的System.Xml.Seriazliation.XmlSerializer负责把对象序列化到XML,和从XML中反序列化为对象.Serializ ...

  9. redis使用Java学习

    一.连接到redis服务 import redis.clients.jedis.Jedis; public class RedisJava { public static void main(Stri ...

  10. DTO学习系列之AutoMapper(五)----当EntityFramework爱上AutoMapper

    有时候相识即是一种缘分,相爱也不需要太多的理由,一个眼神足矣,当EntityFramework遇上AutoMapper,就是如此,恋爱虽易,相处不易. 在DDD(领域驱动设计)中,使用AutoMapp ...