【POJ 2387 Til the Cows Come Home】
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 59755
Accepted: 20336
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.
Source
题解:
①啊其实就是粘一个BFS-SPFA的板子。
#include<stdio.h>
#define go(i,a,b) for(int i=a;i<=b;i++)
#define fo(i,a,x) for(int i=a[x],v=e[i].v;i;i=e[i].next,v=e[i].v)
struct E{int v,next,w;}e[5002];
int n,m,head[2003],d[2003],k=1,U,V,W;
void ADD(int u,int v,int w){e[k]=(E){v,head[u],w};head[u]=k++;}
void dfs(int u){fo(i,head,u)if(d[u]+e[i].w<d[v])d[v]=d[u]+e[i].w,dfs(v);}
int main()
{
scanf("%d%d",&m,&n);
go(i,1,m)scanf("%d%d%d",&U,&V,&W),ADD(U,V,W),ADD(V,U,W);
go(i,1,n)d[i]=1e9;d[1]=0;dfs(1);printf("%d\n",d[n]);return 0;
}//Paul_Guderian
你要成为那美丽的向阳花,在布满创痛的凄风苦雨中,
坚韧地辉煌地绽放……——————汪峰《向阳花》
【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
题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K ...
- 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/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 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 题目大意:有 个点,给出从 点到 点的距离并且 和 是互相可以抵达的,问从 到 的最短距离. 题目分析:这是一道典型的最短路径模版 ...
随机推荐
- tp3.2 excel导出
//导出操作 function exportExcel($expTitle,$expCellName,$expTableData,$names,$width){ $xlsTitle = iconv(' ...
- spring-mybatis整合异常
Failed to read artifact descriptor for XXXXXX:jar:XXXX.RELEAS 原因是maven的本地仓库没有设置好.在别处拷贝过来的项目会有自己的仓库位置 ...
- linux下Tomcat配置提示权限不够解决办法
在终端输入命令 sudo chmod -R 777 /opt/Tomcat,那么Tomcat文件夹和它下面的所有子文件夹的属性都变成了777(读/写/执行权限)
- windows下使用curl.exe模拟ajax请求
curl 是一般linux发行版中都带有的小工具,利用这个工具可以很方便的下载文件,我一般使用这个工具来查看某个页面相应的HTTP头信息,在Windows系统中我们也一样可以使用这个工具,如果不需要支 ...
- 《Python语言及其应用》学习笔记
第二章 ========== 对象的类型决定了可以对它进行的操作.对象的类型还决定了它装着的数据是允许被修改的变量(可变的),还是不可被修改的常量(不可变的). Python是强类型的,你永远无法修改 ...
- C语言进阶——注释符号12
注释可能大认为非常简单,但是注释其实在C语言中很关键,来看下面一段对话. ---学生: 老师,我觉得注释没有必要深究,因为很简单,对程序功能也没有影响. ---老师: 注释是C语言最重要的工具,我们先 ...
- Pandas 数据结构Series:基本概念及创建
Series:"一维数组" 1. 和一维数组的区别 # Series 数据结构 # Series 是带有标签的一维数组,可以保存任何数据类型(整数,字符串,浮点数,Python对象 ...
- Lighting System Design UVA - 11400 动态规划
题目:题目链接 思路:简单的动态规划问题,先把灯泡按照电压从小到大排序.设s[i]为前i种灯泡的总数量(即L值之和),d[i]为灯 泡1-i的最小开销,则d[i] = min{d[j] + (s[i] ...
- zeppelin之连接mysql
上面的一篇文章,对于zeppelin的使用,只是我们对于数据存储在文件中,每一次对于当我们连接数据库的时候都会有问题,今天刚好 把这个问题解决今天我们刚好来介绍如何使用zeppelin来与数据进行连接 ...
- 微信小程序 | 49,小程序入门集锦系列文章20篇
以下20篇文章,都是关于微信小程序的文章,以入门常见问题为主.如发现谬误,请与笔者联系. [小程序入门集锦]1,微信小程序在哪里打开 [小程序入门集锦]2,小程序商店 [小程序入门集锦]3,微信小程序 ...