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;}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 红米2屏幕失灵怎么办 红米手机电池不耐用怎么办 红米手机没内存怎么办 红米2a卡顿怎么办 红米2a手机卡顿怎么办 红米5a内存不足怎么办 红米note1s内存不够怎么办 红米2手机没内存怎么办 红米note显示无服务怎么办 红米手机死屏了怎么办 红米4a信号不好怎么办 小米2a开不了机怎么办 红米4x屏幕失灵怎么办 红米4x外屏坏了怎么办 honor手机开不了机怎么办 honor黑屏开不了机怎么办 红米2开机停在mi怎么办 红米手机无法开机怎么办 小米手机帐号密码忘了怎么办 小米手机忘记小米账号密码怎么办 小米4账号密码忘了怎么办 小米2a触屏失灵怎么办 红米note5拍照不清晰怎么办 红米手机太卡怎么办 红米3x忘记密码怎么办 红米手机太卡了怎么办 红米手机太卡怎么办? 红米3老是死机怎么办 红米3s经常死机怎么办 红米4a进水了怎么办 魅族耳机声音小怎么办 魅蓝note6声音小怎么办 魅蓝3s锁定怎么办 红米1密码忘了怎么办 小米手环升级失败怎么办 红米4忘记密码怎么办 红米1代忘记密码怎么办 红米3s忘记密码怎么办 小米3卡槽卡住了怎么办 红米4手机信号不好怎么办 红米note3信号差怎么办