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. (String) 压缩String

    e.g.  aaabbcccc    返回a3b2c4 public static String compressString(String str) { StringBuilder sb=new S ...

  2. XE6移动开发环境搭建之IOS篇(5):解决Windows和虚拟机下Mac OSX的共享问题(有图有真相)

    网上能找到的关于Delphi XE系列的移动开发环境的相关文章甚少,本文尽量以详细的图文内容.傻瓜式的表达来告诉你想要的答案. 原创作品,请尊重作者劳动成果,转载请注明出处!!! 在安装XE6 PAS ...

  3. java几种常见加密算法小试

    http://www.cnblogs.com/JCSU/articles/2803598.html http://www.open-open.com/lib/view/open139727425732 ...

  4. lintcode-【简单题】合并区间

    题目: 给出若干闭合区间,合并所有重叠的部分. 样例: 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 1 ...

  5. System.getProperty()方法获取大全

    System.out.println("java版本号:" + System.getProperty("java.version")); // java版本号 ...

  6. SQLite数据库文件格式

    数据库命名约定 sqlite3_open()API用到数据库的文件名,可以是相对当前工作目录的相对路径名,也可以是从系统根文件树开始的完整路径名.任何被本地文件系统接受的正规文件名都是好的. 如果文件 ...

  7. Linux VM acquisition

    The evidence is a VM as below. The flat vmdk is the real disk, and the vmdk only 1kb is just a descr ...

  8. 移动端自动化环境搭建-JDK的安装

    一.安装jdk A.安装依赖 JDK作为JAVA开发的环境,不管是做JAVA开发的学生,还是做安卓开发的同学,都必须在电脑上安装JDK. B.安装过程 安装JDK 选择安装目录 安装过程中会出现两次 ...

  9. Redmine2.5+CentOS6+Apache2

    redmine是使用ruby开发的一款无任何商业限制且可自行部署的项目管理软件,其简洁的界面比较符合程序猿的定位,使用起来比较方便,由于我之前装3X没 成功,各版本之间的依存和配置都不一样,所以最后参 ...

  10. IOS-Appium 自动化测试——环境配置及模拟器、真机跑测试

    在MAC环境下配置IOS的appium的自动化测试环境,主要包含三个部分: 一.环境配置 1.安装homebrew(homebrew可以提供MAC OS无法提供的很多套件) ruby -e " ...