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 ...
随机推荐
- 网络--三种网络通讯方式及Android的网络通讯机制
Android平台有三种网络接口可以使用,他们分别是:java.net.*(标准Java接口).Org.apache接口和Android.net.*(Android网络接口).下面分别介绍这些接口的功 ...
- java readLine()
原文 虽然写IO方面的程序不多,但BufferedReader/BufferedInputStream倒是用过好几次的,原因是: 它有一个很特别的方法:readLine(),使用起来特别方便,每次读回 ...
- EF框架 转载
http://www.cnblogs.com/zrdm/p/5060360.html Model First Model First我们称之为"模型优先",这里的模型指的是&quo ...
- 每天一个 Linux 命令(7):mv命令
mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录. 1.命令格式: mv [选项] 源文件或目 ...
- java程序打包成jar 配置文件信息路径
一个普通的java project,里面引用了config.properties配置文件,将项目打成Runnable jar,然后将config.properties放到打包后的jar路径下,执行该j ...
- Visual Studio Enterprise 2015下载 Update3
Visual Studio 2015 是一个丰富的集成开发环境,可用于创建出色的 Windows.Android 和 iOS 应用程序以及新式 Web 应用程序和云服务. 1.适用于各种规模和复杂程度 ...
- JAVA设计模式之建造模式
在阎宏博士的<JAVA与模式>一书中开头是这样描述建造(Builder)模式的: 建造模式是对象的创建模式.建造模式可以将一个产品的内部表象(internal representation ...
- IQ推理:P先生和Q先生
P先生.Q先生具有足够的推理能力.这天,他们正在接受面试. 他们知道桌子的抽屉里有16张扑克牌:红桃 A Q 4黑桃 J 8 4 2 7 3草花 K Q 5 4 6方块 A 5约瀚教授从这16张牌中挑 ...
- [转] Making GTFS query more convenient
url:http://ontrakinfo.wordpress.com/2012/10/29/making-gtfs-query-more-convenient/ 这简直说出了我的心声. I have ...
- archlinux更新错误
问题1 初始化下载: http://repo.archlinuxcn.org/x86_64/wps-office-10.1.0.5672_a21-2-x86_64.pkg.tar.xz 文件大小: 1 ...