Til the Cows Come Home(最短路)

原题链接
Til the Cows Come Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 45313 Accepted: 15372
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

USACO 2004 November
这个题简直就是一个最短路问题的裸题,问的问题实在是太经典了
下面是用最基本的dijkstra算法来实现的代码,他的复杂度是O(|v|^2)

#include <algorithm>
#include <iostream>
#include <utility>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
using namespace std;

typedef long long ll;
const int MOD = int(1e9) + 7;
//int MOD = 99990001;
const int INF = 0x3f3f3f3f;
const ll INFF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos(-1.0); //M_PI;
const int fx[] = {-1, 1, 0, 0};
const int fy[] = {0, 0, -1, 1};
const int maxn=1000 + 5;

int r,n;
int cost[maxn][maxn],d[maxn];
bool used[maxn];

void dijkstra(int s){
        fill(d,d+n,INF);
        fill(used,used+n,false);
        d[s]=0;
        while(true){
                int v=-1;
                for(int i=0;i<n;i++)
                //从剩下的点中寻找出最短距离的点
                        if(!used[i] && (v==-1 || d[i]<d[v])) v=i;
                if(v==-1) break;//如果所有的点都不可以选那么就退出
                used[v]=true;
                for(int i=0;i<n;i++)//更新所有点的最短距离
                        d[i]=min(d[i],d[v]+cost[v][i]);
        }
        return;
}
int main(){
        cin >> r >> n;
        //注意点之间的花费如果题中没有那么就是INF
        for(int i=0;i<maxn;i++)
                for(int j=0;j<maxn;j++)
                        cost[i][j]=INF;
        while(r--){
                int x,y,val;
                scanf("%d%d%d",&x,&y,&val);
                x--;y--;
                if(cost[x][y]>val){//这个题的坑点就是可能会有重边
                        cost[x][y]=val;
                        cost[y][x]=val;
                }
        }
        dijkstra(0);
        cout<<d[n-1]<<endl;
        return 0;
}

下面是用优先队列优化过的dijkstra算法,他的复杂度是O(|E|log|V| )

//http://poj.org/problem?id=2387
#include <algorithm>
#include <iostream>
#include <utility>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
using namespace std;

typedef long long ll;
const int MOD = int(1e9) + 7;
//int MOD = 99990001;
const int INF = 0x3f3f3f3f;
const ll INFF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos(-1.0); //M_PI;
const int fx[] = {-1, 1, 0, 0};
const int fy[] = {0, 0, -1, 1};
const int maxn=1000 + 5;
const int maxr=2000 + 5;

typedef pair<int,int> p;
struct edge{int to,cost;};
int n,r;
vector<edge> E[maxn];
int d[maxn];

void dijkstra(){
        fill(d,d+n,INF);
        d[0]=0;
        priority_queue< p,vector<p>,greater<p> > que;
        que.push(p(0,0));
        while(!que.empty()){
                p now=que.top();que.pop();
                int dist=now.first,v=now.second;
                if(dist > d[v]) continue;
                int len=E[v].size();
                for(int i=0;i<len;i++){
                        edge t=E[v][i];
                        int newdist=t.cost + d[v];
                        if(newdist < d[t.to]){
                                d[t.to]=newdist;
                                que.push(p(d[t.to],t.to));
                        }
                }
        }
        return;
}
int main(){
        cin >> r >> n;
        while(r--){
                int x,y,val;
                scanf("%d%d%d",&x,&y,&val);
                x--;y--;
                E[x].push_back( (edge){y,val} );
                E[y].push_back( (edge){x,val} );
        }
        dijkstra();
        cout << d[n-1] << endl;
        return 0;
}

下面是Bellman_ford算法,他的复杂度是O(|E||V|),他的优势在于可以计算有负边存在的情况,但是前两种算法却不可以

#include <algorithm>
#include <iostream>
#include <utility>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
using namespace std;

typedef long long ll;
const int MOD = int(1e9) + 7;
//int MOD = 99990001;
const int INF = 0x3f3f3f3f;
const ll INFF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos(-1.0); //M_PI;
const int fx[] = {-1, 1, 0, 0};
const int fy[] = {0, 0, -1, 1};
const int maxn=1000 + 5;
const int maxr=2000 + 5;
struct edge{int from,to,cost;};
int r,n;
edge E[maxr];
int d[maxn];
void Bellman_ford(int s){
        fill(d,d+n,INF);
        d[s]=0;
        while(true){
                bool update=false;
                for(int i=0;i<r;i++){
                        edge t=E[i];
                        if(d[t.from]!=INF && d[t.to] > d[t.from] + t.cost){
                                d[t.to]=d[t.from] + t.cost;
                                update=true;
                        }
                        //这是无向图情况下的添加的更新,如果是有向图就不需要这一步
                        if(d[t.to]!=INF && d[t.from] > d[t.to] + t.cost){
                                d[t.from]=d[t.to] + t.cost;
                                update=true;
                        }
                }
                if(!update) break;
        }
        return;
}
int main(){
        cin >> r >> n;
        for(int i=0;i<r;i++){
                int x,y,val;
                scanf("%d%d%d",&x,&y,&val);
                x--;y--;
                E[i]=(edge){x,y,val};
        }
        Bellman_ford(0);
        cout << d[n-1] << endl;
        return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

门豪杰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值