Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 45515   Accepted: 15434

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
 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int w[][],dis[],team[*];
bool exist[];
void SPFA(int s)
{
int head=,tail=,k=;
team[head]=s;
dis[s]=;
while(head<tail)
{
k=team[head];
exist[k]=false;
for(int i=s;i>=;i--)
{
if((w[k][i]>)&&(dis[i]>dis[k]+w[k][i]))
{
dis[i]=dis[k]+w[k][i];
if(!exist[i])
{
team[tail++]=i;
exist[i]=true; }
}
}
head++;
}
// return dis[n];
}
int main()
{
int T,N,x,y,z;
cin>>T>>N;
for(int i=;i<=T;i++)
{
cin>>x>>y>>z;
if(w[x][y]==) w[x][y]=w[y][x]=z;
else if(w[x][y]>z) w[y][x]=w[x][y]=z; }
for(int i=;i<=;i++) dis[i]=;
SPFA(N);
cout<<dis[];
return ;
}

POJ 2837 Til the Cows Come Home的更多相关文章

  1. 【POJ - 2387】Til the Cows Come Home(最短路径 Dijkstra算法)

    Til the Cows Come Home 大奶牛很热爱加班,他和朋友在凌晨一点吃完海底捞后又一个人回公司加班,为了多加班他希望可以找最短的距离回到公司.深圳市里有N个(2 <= N < ...

  2. 【POJ】2387 Til the Cows Come Home

    题目链接:http://poj.org/problem?id=2387 题意:求从1到n的最短路 题解:板子题.spfa. 代码: #include<iostream> #include& ...

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

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

  4. POJ 2387 Til the Cows Come Home

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

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

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

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

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

随机推荐

  1. iOS开发——UI篇Swift篇&UIAlertView/UIActionSheet

    UIAlertView/UIActionSheet UIAlertView //一个按钮的提醒 @IBAction func oneButtonAler() { //创建单一按钮提醒视图 let on ...

  2. How to Copy and Paste in the Ubuntu Gnome Terminal

    How to Copy: Select the content in terminal use your mouse , and then use Ctrl + Shift + C to copy t ...

  3. ORCLE INNODB 博客与 innodb_lru_scan_depth

    https://blogs.oracle.com/mysqlinnodb/ http://mysqllover.com/?p=485 •MySQL. MySQL 5.6.10 http://www.m ...

  4. 基于jquery的侧边栏分享导航

    今天给大家分享一款基于jquery的侧边栏分享导航.这款分享钮一直固定于左侧,鼠标经过的时候凸出显示,这款分享按钮适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲 ...

  5. 项目源码--Android美图秀秀源码

    下载源码   技术要点:   1. 多种分类分享高清图片 2. 图片缓存技术 3. 图片缩图显示 4. 图片实时加载技术 5. 多点触控技术 6.  HTTP网络数据搜索技术 7.  精美UI图片显示 ...

  6. IIS 之 打开/关闭 Internet 信息服务

    由于建立测试网站测试代码,重装电脑后不知道IIS在哪打开.下面以windows7为例介绍,打开IIS管理器的简要步骤. 第一步.查找IIS 1.点击" 开始 "→" 控制 ...

  7. 一、Android应用程序的基本原理(Fundamentals [,fʌndə'mentlz])

    Android 应用程序以java作为编程语言.Android SDK工具把代码连同数据.资源文件一起编译成一个以.apk为后缀(suffix)的android 程序存档(archive)文件包.在一 ...

  8. html&css&js随笔-问题集锦

    1.IE6/7不支持display:inline_block属性:(解决办法) ;*display:inline;} 给元素增加*zomm和*display即可: 2.IE监听事件与W3C标准不一致: ...

  9. [转]DllMain中不当操作导致死锁问题的分析——DllMain中要谨慎写代码(完结篇)

    在CSDN中发现这篇文章,讲解的比较详细,所以在这里备份一个.原文链接:http://blog.csdn.net/breaksoftware/article/details/8167641 DllMa ...

  10. 【Mood-6】空气显示触摸屏、智能钱夹

    空气显示触摸屏 这款屏幕借助从设备中送出的空气和水在空中形成投影,可以用来展示计算机或平板中的图像.此外,它还可以实现人机互动.你只需在虚拟屏幕前挥动双手,就能完成一系列的隔空操作,如滑动.捏合.缩放 ...