POJ 2387 Til the Cows Come Home (最短路 dijkstra)
Til the Cows Come Home
题目链接:
http://acm.hust.edu.cn/vjudge/contest/66569#problem/A
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 Input
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
题意:
n个点m条边的无向图,求1到n的最短路径.
题解:
裸的最短路题;
以下用朴素dijkstra和优先队列优化的dijkstra两种方法分别实现;
注意:
采用邻接数组来存储图时,必须判断重边(朴素法);
代码:
朴素dijkstra方法:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define mid(a,b) ((a+b)>>1)
#define LL long long
#define maxn 1010
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
int n,m;
int value[maxn][maxn];
int dis[maxn];
int pre[maxn];
bool vis[maxn];
void dijkstra(int s) {
memset(vis, 0, sizeof(vis));
memset(pre, -1, sizeof(pre));
for(int i=1; i<=n; i++) dis[i] = inf;
dis[s] = 0;
for(int i=1; i<=n; i++) {
int p, mindis = inf;
for(int j=1; j<=n; j++) {
if(!vis[j] && dis[j]<mindis)
mindis = dis[p=j];
}
vis[p] = 1;
for(int j=1; j<=n; j++) {
//if(dis[p]+value[p][j] < dis[j]) dis[j] = dis[p] + value[p][j];
if(dis[j] > dis[p]+value[p][j]) {
dis[j] = dis[p] + value[p][j];
pre[j] = p;
}
}
}
}
int main(int argc, char const *argv[])
{
//IN;
while(scanf("%d %d", &m,&n) != EOF)
{
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
value[i][j] = inf;
while(m--){
int u,v,w; cin>>u>>v>>w;
if(w < value[u][v]) value[u][v] = value[v][u] = w;
}
dijkstra(1);
printf("%d\n", dis[n]);
}
return 0;
}
优先队列优化的dijkstra方法:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define mid(a,b) ((a+b)>>1)
#define LL long long
#define maxn 5010
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
int n, m;
typedef pair<int,int> pii;
priority_queue<pii,vector<pii>,greater<pii> > q;
bool vis[maxn];
int edges, u[maxn], v[maxn], w[maxn];
int first[maxn], next[maxn];
int dis[maxn];
int pre[maxn];
void add_edge(int s, int t, int val) {
u[edges] = s; v[edges] = t; w[edges] = val;
next[edges] = first[s];
first[s] = edges++;
}
void dijkstra(int s) {
memset(pre, -1, sizeof(pre));
memset(vis, 0, sizeof(vis));
for(int i=1; i<=n; i++) dis[i]=inf; dis[s] = 0;
while(!q.empty()) q.pop();
q.push(make_pair(dis[s], s));
while(!q.empty()) {
pii cur = q.top(); q.pop();
int p = cur.second;
if(vis[p]) continue; vis[p] = 1;
for(int e=first[p]; e!=-1; e=next[e]) if(dis[v[e]] > dis[p]+w[e]){
dis[v[e]] = dis[p] + w[e];
q.push(make_pair(dis[v[e]], v[e]));
pre[v[e]] = p;
}
}
}
int main(int argc, char const *argv[])
{
//IN;
while(scanf("%d %d", &m,&n) != EOF)
{
edges = 1;
memset(first, -1, sizeof(first));
for(int i=1; i<=m; i++){
int u,v,w; scanf("%d %d %d", &u,&v,&w);
add_edge(u, v, w);
add_edge(v, u, w);
}
dijkstra(1);
printf("%d\n", dis[n]);
// int cur = n;
// while(1) {
// printf("%d ", cur);
// if(cur == 1) break;
// cur = pre[cur];
// }
}
return 0;
}
POJ 2387 Til the Cows Come Home (最短路 dijkstra)的更多相关文章
- 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 ...
- POJ 2387 Til the Cows Come Home(最短路模板)
题目链接:http://poj.org/problem?id=2387 题意:有n个城市点,m条边,求n到1的最短路径.n<=1000; m<=2000 就是一个标准的最短路模板. #in ...
- POJ 2387 Til the Cows Come Home --最短路模板题
Dijkstra模板题,也可以用Floyd算法. 关于Dijkstra算法有两种写法,只有一点细节不同,思想是一样的. 写法1: #include <iostream> #include ...
- 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代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...
- Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化)
Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化) 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回 ...
- 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(最短路 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 ...
随机推荐
- Android开发之Android Context,上下文(Activity Context, Application Context)
转载:http://blog.csdn.net/lmj623565791/article/details/40481055 1.Context概念Context,相信不管是第一天开发Android,还 ...
- mssql修改链接数为默认值
EXEC sys.sp_configure N'show advanced options', N'1' RECONFIGURE WITH OVERRIDE GO EXEC sys.sp_confi ...
- UVa 11992 (线段树 区间修改) Fast Matrix Operations
比较综合的一道题目. 二维的线段树,支持区间的add和set操作,然后询问子矩阵的sum,min,max 写完这道题也是醉醉哒,代码仓库里还有一份代码就是在query的过程中也pushdown向下传递 ...
- LA 3890 (半平面交) Most Distant Point from the Sea
题意: 给出一个凸n边形,求多边形内部一点使得该点到边的最小距离最大. 分析: 最小值最大可以用二分. 多边形每条边的左边是一个半平面,将这n个半平面向左移动距离x,则将这个凸多边形缩小了.如果这n个 ...
- tomcat调优的几个方面
转载自:http://my.oschina.net/u/593721/blog/146710 作者:小报童 和早期版本相比最新的Tomcat提供更好的性能和稳定性.所以一直使用最新的Tomcat版本. ...
- Android下fragment切换的动画效果
网上看到的例子,转过来记录一下,学习一下,感谢原作者的辛勤编码,效果非常好 基于Android3.0新增的动画api,效果很赞 共21种动画效果: <item>X轴缩放</item& ...
- 利用反射自动生成SQL语句(仿Linq)
转:http://www.cnblogs.com/the7stroke/archive/2012/04/22/2465597.html using System; using System.Colle ...
- win下Java环境安装
1.eclipse:eclipse.org 解压后直接打开 2.JDK:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-do ...
- MSSQL 2005数据库与SP4补丁安装
Sql Server 2005 正确安装之前的win7配置: http://wenku.baidu.com/link?url=6T3jzVnu2XY_sfqfe9ZqQ_6dUOdrZwHc83baW ...
- LXD 2.0 系列(二):安装与配置
导读 简单来说,LXD是一个守护进程,为LXC容器的管理提供一组REST API.主要目标是提供一种类虚拟机的用户体验,是一种第三方的容器管理工具.下面呢,我们来介绍LXD 2.0 的安装与配置 安装 ...