POJ 2387 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.

解題報告:

1:下面代碼1是Dijkstra的模板。該算法只適用於邊是非負數的情況。g:地圖。dis:代表開始點到每個點的最短距離。visit:標記數組。

2:下面代碼2是用優先隊列優化Dijkstra的模板。該算法只適用於邊是非負數的情況。g:地圖。dis:代表開始點到每個點的最短距離。visit:標記數組。point結構體中的dis是最短距離,id爲某個點的編號。Q爲優先隊列,dis小的排在前面。

3:下面代碼3是SPFA的模板。該算法(除負環)都適用。g:地圖。dis:代表開始點到每個點的最短距離。visit:標記數組。cnt:記錄每個點入隊的次數,大於n次入隊說明出現了負環。point結構體中的dis是最短距離,id爲某個點的編號。Q爲隊列。

代碼1:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long ll;
const ll N = 1000+10;
const ll INF = 0x3f3f3f3f;
ll g[N][N], dis[N], visit[N];
void init(ll n, ll m){
    memset(visit, 0, sizeof(visit));memset(g, INF, sizeof(g));
    for(ll i=0; i<n; ++i)g[i][i] = 0;
    for(ll i=0; i<m; ++i){
        ll u, v, s;
        scanf("%lld%lld%lld", &u, &v, &s);
        g[--u][--v] = g[v][u] = min(g[u][v], s);
    }
}
void Dijkstra(ll n, ll m, ll u){
    for(ll i=0; i<n; ++i)dis[i] = g[u][i];
    visit[u] = 1;
    for(ll i=0; i<n; ++i){
        ll minn = INF, idx;
        for(ll j=0; j<n; ++j)
            if(!visit[j] && dis[j] < minn)minn = dis[j], idx = j;
        visit[idx] = 1;
        for(ll j=0; j<n; ++j)dis[j] = min(dis[j], g[idx][j]+dis[idx]);
    }
}
int main(){
    ll n, m;
    while(~scanf("%lld%lld", &m, &n)){
        init(n, m);
        Dijkstra(n, m, 0);
        printf("%lld\n", dis[n-1]);
    }
    return 0;
}

代碼2:

#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const ll N = 1000+10;
const ll INF = 0x3f3f3f3f;
ll g[N][N], visit[N], dis[N];
struct point{
    ll dis, id;
    bool operator < (point x)const {
        return dis > x.dis;
    }
}p1, p2;
priority_queue<point> Q;
void init(ll n, ll m){
    memset(g, INF, sizeof(g)), memset(visit, 0, sizeof(visit));
    for(ll i=0; i<n; ++i)g[i][i] = 0, dis[i] = INF;
    while(!Q.empty())Q.pop();
    for(ll i=0; i<m; ++i){
        ll u, v, s;
        scanf("%lld%lld%lld", &u, &v, &s);
        g[--u][--v] = g[v][u] = min(g[u][v], s);
    }
}
void Dijkstra(ll s, ll n){
    p1.id = s, p1.dis = 0, dis[0] = 0;
    Q.push(p1);
    while(!Q.empty()){
        p1 = Q.top();
        Q.pop();
        if(visit[p1.id])continue;
        visit[p1.id] = 1;
        for(ll i=0; i<n; ++i){
            if(!visit[i] && dis[p1.id]+g[p1.id][i] < dis[i]){
                dis[i] = p2.dis = dis[p1.id]+g[p1.id][i];
                p2.id = i, Q.push(p2);
            }
        }
    }
}
int main(){
    ll n, m;
    while(~scanf("%lld%lld", &m, &n)){
        init(n, m);
        Dijkstra(0, n);
        printf("%lld\n", dis[n-1]);
    }
    return 0;
}

代碼3:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
typedef long long ll;
const ll N = 1000+10;
const ll INF = 0x3f3f3f3f;
ll g[N][N], cnt[N], dis[N];
bool visit[N];
struct point{
    ll id, dis;
}p1, p2;
queue<point> Q;
void init(ll n, ll m){
    for(ll i=0; i<n; ++i)
        for(ll j=0; j<n; ++j)g[i][j] = INF;
    for(ll i=0; i<n; ++i)dis[i] = INF, cnt[i] = 0, visit[i] = 0, g[i][i] = 0;
    while(!Q.empty())Q.pop();
    for(ll i=0; i<m; ++i){
        ll u, v, s;
        scanf("%lld%lld%lld", &u, &v, &s);
        g[--u][--v] = g[v][u] = min(g[u][v], s);
    }
}
bool SPFA(ll s, ll n){
    p1.id = s, p1.dis = 0, dis[s] = 0, cnt[s] = 1;
    Q.push(p1);
    while(!Q.empty()){
        p1 = Q.front();
        Q.pop(), visit[p1.id] = 0;
        for(ll i=0; i<n; ++i){
            if(dis[p1.id]+g[p1.id][i] >= dis[i])continue;
            p2.dis = dis[i] = dis[p1.id]+g[p1.id][i];
            if(visit[i])continue;
            visit[i] = 1, p2.id = i, cnt[i]++;
            if(cnt[i] > n)return 0;
            Q.push(p2);
        }
    }
    return 1;
}
int main(){
    ll n, m;
    while(~scanf("%lld%lld", &m, &n)){
        init(n, m);
        if(SPFA(0, n))printf("%lld\n", dis[n-1]);
    }
    return 0;
}

 

發佈了211 篇原創文章 · 獲贊 9 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章