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.

题目的意思是求解从路标N到路标1的最短路径,简单的最短路径题目。

题目有一个坑:输入有重边,所以要选择最小的长度。

#include<string.h>
#include<stdio.h>
#include<math.h>
#define typec int
const int MAXN=1010;
const typec INF=0x3f3f3f3f;//防止后面溢出,这个不能太大
bool vis[MAXN];
int pre[MAXN];
typec cost[MAXN][MAXN];
typec l[MAXN];
void D(int n,int beg)
{
for(int i=1;i<=n;i++)
{
l[i]=INF;vis[i]=false;pre[i]=-1;
}
l[beg]=0;
for(int j=1;j<=n;j++)
{
int k=-1;
int Min=INF;
for(int i=1;i<=n;i++)
if(!vis[i]&&l[i]<Min)
{
Min=l[i];
k=i;
}
if(k==-1)break;
vis[k]=true;
for(int i=1;i<=n;i++)
if(!vis[i]&&l[k]+cost[k][i]<l[i])
{
l[i]=l[k]+cost[k][i];
pre[i]=k;
}
}
} int main()
{
int n,i,j,t,a,b,c;
while(scanf("%d%d",&t,&n)!=EOF)
{
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
cost[i][j]=(i==j)? 0:INF; for(i=0;i<t;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(cost[a][b]>c)
cost[a][b]=cost[b][a]=c;
}
D(n,n);
printf("%d\n",l[1]);
}
return 0;
}

  

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

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

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

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

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

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

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

  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 (Bellman_Ford || Dijkstra || SPFA)

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

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

  7. 3386/1752: [Usaco2004 Nov]Til the Cows Come Home 带奶牛回家

    3386/1752: [Usaco2004 Nov]Til the Cows Come Home 带奶牛回家 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit ...

  8. (Dijkstra) POJ2387 Til the Cows Come Home

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

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

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

随机推荐

  1. css绝对定位问题

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 38. Same Tree && Symmetric Tree

    Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...

  3. out参数,ref参数,params参数数组

    params参数数组 params关键字可以为方法指定数目可变的参数.params关键字修饰的参数,可以传入任意数目的同类型参数,甚至可以不传入参数. 不过params修饰的参数必须是方法的最后一个参 ...

  4. eclipse下tomcat添加部署Module,Web名称与项目名称不一致的解决方法

    问题描述: 使用eclipse,因为某种原因项目名称修改后,使用tomcat进行web发布时,选择“Add and Remove Projects...”或双击打开选择Modules选项卡后点击“An ...

  5. arcgis engine 基础代码

    1.开始编辑,save feature property,停止编辑 IWorkspace workspace = ((IDataset)pFeatureClass).Workspace;IWorksp ...

  6. 【转】用C#调用Windows API向指定窗口发送

    一.调用Windows API. C#下调用Windows API方法如下: 1.引入命名空间:using System.Runtime.InteropServices; 2.引用需要使用的方法,格式 ...

  7. 深入解析js中基本数据类型与引用类型,函数参数传递的区别

    ECMAScript的数据有两种类型:基本类型值和引用类型值,基本类型指的是简单的数据段,引用类型指的是可能由多个值构成的对象. Undefined.Null.Boolean.Number和Strin ...

  8. 第五百八十二天 how can I 坚持

    好吧,是我错了,昨天,做好自己就行了,别人怎么样是别人的事,永远保持一颗单纯向上的心. 时间过得真快,明天又周六了.. 睡觉.

  9. js编码

    var url = encodeURI(encodeURI("search-keyword-"+keyword+".html")); 后台uri = Strin ...

  10. Egret和Http请求 (Ajax、XMLHttpRequest、Post、Get)

    一  Http请求 二  AJax和XMLHttpRequest 三  一个Ajax例子 四 Egret中的egret.HttpRequest 五 Post和Get区别 一 Http请求 Http深入 ...