POJ 2387 Til the Cows Come Home
题目链接:http://poj.org/problem?id=2387
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 45414 | Accepted: 15405 |
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
题目大意:有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的更多相关文章
- 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 ...
- POJ.2387 Til the Cows Come Home (SPFA)
POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
- 怒学三算法 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 ...
- 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 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 ...
- POJ 2387 Til the Cows Come Home (最短路径 模版题 三种解法)
原题链接:Til the Cows Come Home 题目大意:有 个点,给出从 点到 点的距离并且 和 是互相可以抵达的,问从 到 的最短距离. 题目分析:这是一道典型的最短路径模版 ...
- 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 ...
随机推荐
- tic/toc/cputime测试时间
cputime测试代码运行时间可能不及tic/toc准确是众所周知的事情.本文并非旧话重提,而是期望起到抛砖引玉的效果,从而找到cputime与tic/toc内在的区别.望不吝赐教! 用tic/toc ...
- Nginx 缓存参数
看看这下面两个指令参数: ----------------------------------------------------------------- proxy_cache_path /ho ...
- goldengate 12c对oracle DB的改进
1. 现在可使用Oracle Universal Installer,即安装时有图形化界面,同时会自动安装java runtime environment,不过个人认为,还是ZIP安装包方便,解压即用 ...
- unity3d角色控制器01
参考出处貌似是雨松大神.如有侵权,立即删除. 需要导入包 ①将FirstPerson Controller拖拽入Hierarchy(层次视图)中.由于角色控制器是具有一定物理引擎的,所以一定要将它放在 ...
- 支撑向量机(SVM)
转载自http://blog.csdn.net/passball/article/details/7661887,写的很好,虽然那人也是转了别人的做了整理(最原始文章来自http://www.blog ...
- hdu 2058
PS:TLE了N次...虽然结果对了...后来看了公式才知道要枚举项数才行... 代码: #include "stdio.h"#include "math.h" ...
- UITouch的用法
UITouch一般无法直接获取,是通过UIView的touchesBegan等函数获得. //这四个方法是UIResponder中得方法 // Generally, all responders wh ...
- UIkit框架之Uivew
1.继承链:UIresponder:NSObject 2.通过使用 addGestureRecognizer:方法可以为视图添加手势 3.下面的属性都可以用来用于动画 @property frame ...
- 查询某个表或者所有表的字段说明 SQLServer
查询某个表或者所有表的字段说明SELECT [Table Name] = OBJECT_NAME(c.object_id), [Column Name] = c.name, [Des ...
- Mac快捷键 Xcode快捷键
Mac OSX 快捷键&命令行 一.Mac OSX 快捷键 ctrl+shift 快速放大dock的图标会暂时放大,而 ...