天天看點

Til the Cows Come HomeTil the Cows Come Home (最短路問題)

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 1002
using 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+,INF);
    d[s]=;
    que.push(P(,s));

    while(!que.empty()){
        P p=que.top();que.pop();
        int v=p.second;
        if(d[v]<p.first) continue;
        for(int i=;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+];
        int s;bool flag;
        while(T--){
            scanf("%d%d%d",&s,&E.to,&E.cost);
            int t=;
            while(t--){// 這是個無向圖 存兩次 
                G[s].push_back(E);
                int tmp=s;
                s=E.to,E.to=tmp;
            }
        }
        dijkstra(,G);
        printf("%d\n",d[V]);
    }
    return ;
}
           

方法二

Bellman-Ford算法,時間複雜度 O(|V|*|E|)

隻是為了練習然後敲了下面的代碼

//436K  16MS
#include<stdio.h>
#include<algorithm>
#define MAX_E 4002
#define MAX_V 1002
#define INF 0x3f3f3f
using 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+,INF);
    d[s]=;
    while(true){
        bool update=false;
        for(int i=;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=;i<E;i++){
            scanf("%d%d%d",&get.from,&get.to,&get.cost);
            es[*i]=get;
            int tmp=get.from;get.from=get.to;get.to=tmp;
            es[*i+]=get;
        }
        E*=;
        bellman_ford();
        printf("%d\n",d[V]); 
    }
    return ;
}