Til the Cows Come Home
Description
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
* 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
Sample Input
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
Sample Output
90
Hint
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的更多相关文章
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
- Til the Cows Come Home(最短路)
Til the Cows Come Home Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I ...
- POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37662 Accepted ...
- POJ 2387 Til the Cows Come Home
题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K ...
- 怒学三算法 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 ...
- 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 ...
- 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 ...
- (Dijkstra) POJ2387 Til the Cows Come Home
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 81024 Accepted ...
- 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 ...
- 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 ...
随机推荐
- Sonar升级遇到的那些事儿
目录 背景 如何升级 如何回滚 问题解决 参考 背景 目前我们用SonarQube版本是4.0,这次准备升级到最新版本5.1, 以便支持以后的JavaScript的项目. 如何升级 我们可以直接跨越版 ...
- XE6移动开发环境搭建之IOS篇(4):VMware9里安装Mac OSX 10.8(有图有真相)
网上能找到的关于Delphi XE系列的移动开发环境的相关文章甚少,本文尽量以详细的图文内容.傻瓜式的表达来告诉你想要的答案. 原创作品,请尊重作者劳动成果,转载请注明出处!!! 以下内容比较长,我们 ...
- DELPHI声明一个指针变量,什么时候需要分配内存,什么时候不需要分配内存?
DELPHI声明一个指针变量,什么时候需要分配内存,什么时候不需要分配内存?比如我定义个变量 var p:Pchar;如果这个变量声明为全局变量,需要分配内存吗?分配为局部变量,需要分为内存吗?为什么 ...
- lintcode-【简单题】快乐数
题目: 写一个算法来判断一个数是不是"快乐数". 一个数是不是快乐是这么定义的:对于一个正整数,每一次将该数替换为他每个位置上的数字的平方和,然后重复这个过程直到这个数变为1,或是 ...
- jquery事件合集
1.在input输入数据时执行的事件(边输入边触发事件) $("input[id='subjectNum']").bind('input propertychange', func ...
- 百度地图API示例之移动地图
级别为6 级别为8 级别为12 代码: <!DOCTYPE html> <html> <head> <meta http-equiv="Conten ...
- leetcode 160
160. Intersection of Two Linked Lists Write a program to find the node at which the intersection of ...
- 用shell脚本切分task_list,并分别执行的脚本
#/bin/sh TASK_LIST=$ len=`wc -l $TASK_LIST | awk '{print $1}'` #noExce(){ ) ))'p' $TASK_LIST > $T ...
- mvc多个按钮的提交方法
转载地址:http://www.cnblogs.com/wuchang/archive/2010/01/29/1658916.html 有时候会遇到这种情况:在一个表单上需要多个按钮来完成不同的功能, ...
- 【EF学习笔记11】----------查询中常用的扩展方法
先来看一下我们的表结构: 首先毫无疑问的要创建我们的上下文对象: using (var db = new Entities()) { //执行操作 } Average 平均值: //查询平均分 Con ...