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. glibc的了解,对内核的封装

    glibc除了提供最底层的C运行库,还封装了kernel提供的API,程序通过glibc进行系统调用( syscall). 应用层面的C库,比如OpenSSL库: /usr/include/opens ...

  2. Oracle优化笔记

    2016-11-22   子查询:标量子查询 内联视图(in-line view) 半连接/反连接   标量子查询 select 后跟子查询 类似自定义函数 可用开窗函数之类的改写   内联视图(in ...

  3. 黑马程序员_Java_String

    String类 一.概述 字符串是一个特殊的对象. 字符串一旦初始化就不可以被改变. String s1 = "abc";//s1是一个类类型变量,"abc"是 ...

  4. UVA11922--Permutation Transformer (伸展树Splay)

    题意:m条操作指令,对于指令 a  b 表示取出第a~b个元素,翻转后添加到排列的尾部. 水题卡了一个小时,一直过不了样例.  原来是 dfs输出的时候 忘记向下传递标记了. #include < ...

  5. animation之translate、scale、alpha、rotate动画效果呈现

    动画类型 Android的animation由四种类型组成 XML中 alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动画效果 rotate 画面 ...

  6. Linux中输入命令按tab提示后会自动转义解决方案(xjl456852原创)

    linux在命令行输入命令时,如果有$字符,按tab键时会自动在前面加入转义字符,反而达不到自己需要的效果. 例如: 在Centos7下,我要进入一个环境变量,并编辑一个文件: 比如我要进入$JAVA ...

  7. winzip15.0注冊码

    username:Juzhaofeng 授权码:MPZRP-Y7LWW-K1DKG-FM92E-2C5F5-ZEKFF

  8. Windows命令行(DOS命令)教程-4(转载)http://arch.pconline.com.cn//pcedu/rookie/basic/10111/15325_3.html

    2. md md是英文make directory(创建目录)的缩写 [功能] 创建一个子目录 [格式] md [C:]path [举例] 用md 建立一个叫做purple的目录 3. cd cd是英 ...

  9. AngularJs练习Demo8 自定义过滤器

    @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...

  10. Homebrew -- Mac软件管家(套件管理yun……)

    也许是之前使用linux系统的时候总是习惯使用wget 在mac中只有curl,有点略显不习惯 于是乎某天在搜索mac开发者的时候发现了Homebrew这个东西 ok,是那么句话--惰性是人的天性 有 ...