Til the Cows Come Home
来源:互联网 发布:成都妇女儿童医院网络 编辑:程序博客网 时间:2024/06/05 22:44
Til the Cows Come Home (最短路问题)
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.
题解
理论上Dijkstra应该更快的,可不知为什么提交结果Bellman-Ford更快了,对该问题感兴趣的同学可以留言讨论一下。
方法一
Dijkstra算法,用优先队列优化后时间复杂度为 O(|E|log|V|)
为了节约空间使用了邻接链表
//656K 79MS#include<stdio.h>#include<queue>#include<vector>#define INF 0x3f3f3f#define MAX_N 1002using namespace std;struct edge{int to,cost;}E;typedef pair<int ,int> P;//f 距离 s 顶点int V,T;int d[MAX_N];bool operator <(P x,P y){ return x.first>y.first;}void dijkstra(int s,vector<edge> *G){ //vector<edge> G[V+1]; priority_queue<P> que; fill(d,d+V+1,INF); d[s]=0; que.push(P(0,s)); while(!que.empty()){ P p=que.top();que.pop(); int v=p.second; if(d[v]<p.first) continue; for(int i=0;i<G[v].size();i++){ edge e=G[v][i]; if(d[v]+e.cost<d[e.to]){ d[e.to]=d[v]+e.cost; que.push(P(d[e.to],e.to)); } } }}int main(){ while(~scanf("%d%d",&T,&V)){ vector<edge> G[V+1]; int s;bool flag; while(T--){ scanf("%d%d%d",&s,&E.to,&E.cost); int t=2; while(t--){// 这是个无向图 存两次 G[s].push_back(E); int tmp=s; s=E.to,E.to=tmp; } } dijkstra(1,G); printf("%d\n",d[V]); } return 0;}
方法二
Bellman-Ford算法,时间复杂度 O(|V|*|E|)
只是为了练习然后敲了下面的代码
//436K 16MS#include<stdio.h>#include<algorithm>#define MAX_E 4002#define MAX_V 1002#define INF 0x3f3f3fusing namespace std;struct edge{int from,to,cost;};edge es[MAX_E];int d[MAX_V],V,E;void bellman_ford(int s){ fill(d,d+V+1,INF); d[s]=0; while(true){ bool update=false; for(int i=0;i<E;i++){ edge e=es[i]; if(d[e.from]!=INF&&d[e.from]+e.cost<d[e.to]){ d[e.to]=d[e.from]+e.cost; update=true; } } if(!update) break; }}int main(){ while(~scanf("%d%d",&E,&V)){ edge get; for(int i=0;i<E;i++){ scanf("%d%d%d",&get.from,&get.to,&get.cost); es[2*i]=get; int tmp=get.from;get.from=get.to;get.to=tmp; es[2*i+1]=get; } E*=2; bellman_ford(1); printf("%d\n",d[V]); } return 0;}
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- PKU2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- Windows环境下使用cmd面板手动编译c/c++程序
- 字母索引表
- SPOJ Fata7y Ya Warda! 裸单调栈 1月25日
- 【t089】数列2
- 网络爬虫:淘女郎
- Til the Cows Come Home
- easyui tab 加载iframe 高度问题
- HDU1213并查集基础
- UVa401
- 清华大学 质因数的个数
- css IE6和W3C盒子定义区别
- Git可视化工具SourceTree的简单使用
- [DFT]插入Pipeline Register
- python条件循环和其他语句