题目链接:http://poj.org/problem?id=2387

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

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
题目大意:有N个节点,T条路(带权值),求1到N的最短路径。
解题思路:最短路迪杰斯特拉算法(模板题)。
AC代码:
 #include <stdio.h>
#include <string.h>
#define inf 999999999
int visit[];
int dis[];
int p[][];
int n;
int dijkstra()
{
int i,j,pos = ,minn;
memset(visit,,sizeof(visit));
visit[] = ;
dis[] = ;
for (i = ; i <= n; i ++)
dis[i] = p[][i];
for (i = ; i <= n; i ++)
{
minn = inf;
for (j = ; j <= n; j ++)
{
if (!visit[j] && dis[j] < minn)
{
minn = dis[j];
pos = j;
}
}
visit[pos] = ;
for (j = ; j <= n; j ++)
if (!visit[j] && dis[j] > dis[pos]+p[pos][j])
dis[j] = dis[pos]+p[pos][j];
}
return dis[n];
}
int main ()
{
int t,a,b,c,i,j;
while (~scanf("%d%d",&t,&n))
{
for (i = ; i <= n; i ++)
for (j = ; j <= n; j ++)
p[i][j] = inf; for (i = ; i < t; i ++)
{
scanf("%d%d%d",&a,&b,&c);
if (p[a][b] > c)
p[a][b] = p[b][a] = c;
}
printf("%d\n",dijkstra());
}
return ;
}
 

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

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

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

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

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

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

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

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

  7. POJ 2387 Til the Cows Come Home Dijkstra求最短路径

    Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to get as much s ...

  8. POJ 2387 Til the Cows Come Home (最短路径 模版题 三种解法)

    原题链接:Til the Cows Come Home 题目大意:有  个点,给出从  点到  点的距离并且  和  是互相可以抵达的,问从  到  的最短距离. 题目分析:这是一道典型的最短路径模版 ...

  9. POJ 2387 Til the Cows Come Home(模板——Dijkstra算法)

    题目连接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to ...

随机推荐

  1. Halcon 10.0:Hobject图像转CBitmap

    void HImage2CBitmap(Hobject pImage,CBitmap *wImage) { char lpcsType[MAX_STRING]; Hlong lPointer,widt ...

  2. Oracle内置函数内容整理

    --绝对值select abs(-100) from dual; --取余select mod(8,3) from dual; --取整,大于该数的最小整数(上限值)select ceil(12.0) ...

  3. Oracle中any和all的区别用法

    对于any,all的用法,书中说的比较绕口,难以理解,如果通过举例就会比较清晰. any的例子: select * from t_hq_ryxx where gongz > any (selec ...

  4. Windows8 10设置程序为 系统默认浏览器

    从win8 开始,MS修改了文件和协议的关联方式,普通的注册表修改是无效的. 必须使用组策略(group policy )对象GP才行. http://blogs.technet.com/b/mrml ...

  5. JS获取上传文件的名称、格式、大小

    <input id="File1" type="file" onchange="checkFile(this)" /> 方式一) ...

  6. [Java]Java简介

    Java版本历史 1995年5月23日,Java语言诞生 1996年1月,第一个JDK1.0诞生 1996年4月,10个最主要的操作系统供应商申明将在其产品中嵌入JAVA技术 1996年9月,约8.3 ...

  7. Line计划今年全面进军中国市场:建立本地团队

    北京时间6月13日下午消息,<华尔街日报>报道称,移动消息应用Line计划于今年晚些时候进军中国市场.Line将在中国建立本地团队,开发内容和功能,从而进一步开拓中国这一全球最大的移动市场 ...

  8. XAMPP启动mysql遇到的问题

    Version: '10.1.9-MariaDB' socket: '' port: 3306 mariadb.org binary distribution2016-07-18 10:42:04 1 ...

  9. js对象的定义及处理

    一,概述 在Java语言中,我们可以定义自己的类,并根据这些类创建对象来使用,在Javascript中,我们也可以定义自己的类,例如定义User类.Hashtable类等等. 目前在Javascrip ...

  10. PAT 05-树8 Huffman Codes

    以现在的生产力,是做不到一天一篇博客了.这题给我难得不行了,花了两天时间在PAT上还有测试点1没过,先写上吧.记录几个做题中的难点:1.本来比较WPL那块我是想用一个函数实现的,无奈我对传字符串数组无 ...