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 ...
随机推荐
- WebBrowser自动点击链接 广告自动点击 Ads Auto Click
点击付费广告, 是目前比较流行的一种网络营销模式, 但是,如果你单纯的靠手工一个一个去点, 总觉得不划算 , 能不能实现自动的去点击呢? 答案是肯定的 .NET 里面的 WebBrowser, 可以 ...
- JasperReports+iReport在eclipse中的使用
转载:http://blog.csdn.net/daming924/article/details/7402295 一.介绍1)它可以PDF,HTML,XML等多种形式产生报表或动态报表,在新版本还支 ...
- POJ1037A decorative fence(好dp)
1037 带点组合的东西吧 黑书P257 其实我没看懂它写的嘛玩意儿 这题还是挺不错的 一个模糊的思路可能会好想一些 就是大体的递推方程 dp1[][]表示降序 dp2[][]表示升序 数组的含义为长 ...
- SetCapture、ReleaseCapture、GetCapture
正常情况下,鼠标指针位于哪个窗口区域内,鼠标消息就自动发给哪个窗口.如果调用了SetCapture,之后无论鼠标的位置在哪,鼠标消息都发给指定的这个窗口,直到调用ReleaseCapture或者调用S ...
- POJ 2947 Widget Factory (高斯消元 判多解 无解 和解集 模7情况)
题目链接 题意: 公司被吞并,老员工几乎全部被炒鱿鱼.一共有n种不同的工具,编号1-N(代码中是0—N-1), 每种工具的加工时间为3—9天 ,但是现在老员工不在我们不知道每种工具的加工时间,庆幸的是 ...
- 使用Quartz创建定时任务
项目开发中经常需要定时循环执行某些任务 比如定时发送报表,定时发送邮件,亦或者定时清理缓存,定时更新数据等等 有些时候可以简单地利用Windows Server的计划任务执行程序 Linux也有相应的 ...
- codevs 1171 潜伏者
要是NOIP自己这样水就完了... 仔细啊!!!! #include<iostream> #include<cstdio> #include<cstring> #i ...
- apache开源项目-- UIMA
UIMA (Unstructured Information Management applications) 是一个软件系统,用来分析大量的非结构化信息从而发掘中对最终用户有用的知识点,一个最典型的 ...
- 【转】iOS开发UITableViewCell的选中时的颜色设置
原文网址:http://mobile.51cto.com/hot-404900.htm 1.系统默认的颜色设置 //无色 cell.selectionStyle = UITableViewCellSe ...
- [Papers]NSE, $u_3$, Lebesgue space [Kukavica-Ziane, Nonlinearity, 2006]
$$\bex u_3\in L^p(0,T;L^q(\bbR^3)),\quad \frac{2}{p}+\frac{3}{q}=\frac{5}{8},\quad \frac{24}{5}<q ...