POJ 2387 Til the Cows Come Home (最短路 dijkstra)

Til the Cows Come Home

题目链接:

http://acm.hust.edu.cn/vjudge/contest/66569#problem/A

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 Input

5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100

题意:

n个点m条边的无向图,求1到n的最短路径.

题解:

裸的最短路题;
以下用朴素dijkstra和优先队列优化的dijkstra两种方法分别实现;
注意:
采用邻接数组来存储图时,必须判断重边(朴素法);

代码:

朴素dijkstra方法:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define mid(a,b) ((a+b)>>1)
#define LL long long
#define maxn 1010
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

int n,m;
int value[maxn][maxn];
int dis[maxn];
int pre[maxn];
bool vis[maxn];

void dijkstra(int s) {
    memset(vis, 0, sizeof(vis));
    memset(pre, -1, sizeof(pre));
    for(int i=1; i<=n; i++) dis[i] = inf;
    dis[s] = 0;

    for(int i=1; i<=n; i++) {
        int p, mindis = inf;
        for(int j=1; j<=n; j++) {
            if(!vis[j] && dis[j]<mindis)
                mindis = dis[p=j];
        }
        vis[p] = 1;
        for(int j=1; j<=n; j++) {
            //if(dis[p]+value[p][j] < dis[j]) dis[j] = dis[p] + value[p][j];
            if(dis[j] > dis[p]+value[p][j]) {
                dis[j] = dis[p] + value[p][j];
                pre[j] = p;
            }
        }
    }
}

int main(int argc, char const *argv[])
{
    //IN;

    while(scanf("%d %d", &m,&n) != EOF)
    {
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                value[i][j] = inf;
        while(m--){
            int u,v,w; cin>>u>>v>>w;
            if(w < value[u][v]) value[u][v] = value[v][u] = w;
        }

        dijkstra(1);

        printf("%d
", dis[n]);
    }

    return 0;
}

优先队列优化的dijkstra方法:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define mid(a,b) ((a+b)>>1)
#define LL long long
#define maxn 5010
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

int n, m;
typedef pair<int,int> pii;
priority_queue<pii,vector<pii>,greater<pii> > q;
bool vis[maxn];
int edges, u[maxn], v[maxn], w[maxn];
int first[maxn], next[maxn];
int dis[maxn];
int pre[maxn];

void add_edge(int s, int t, int val) {
    u[edges] = s; v[edges] = t; w[edges] = val;
    next[edges] = first[s];
    first[s] = edges++;
}

void dijkstra(int s) {
    memset(pre, -1, sizeof(pre));
    memset(vis, 0, sizeof(vis));
    for(int i=1; i<=n; i++) dis[i]=inf; dis[s] = 0;
    while(!q.empty()) q.pop();
    q.push(make_pair(dis[s], s));

    while(!q.empty()) {
        pii cur = q.top(); q.pop();
        int p = cur.second;
        if(vis[p]) continue; vis[p] = 1;
        for(int e=first[p]; e!=-1; e=next[e]) if(dis[v[e]] > dis[p]+w[e]){
            dis[v[e]] = dis[p] + w[e];
            q.push(make_pair(dis[v[e]], v[e]));
            pre[v[e]] = p;
        }
    }
}

int main(int argc, char const *argv[])
{
    //IN;

    while(scanf("%d %d", &m,&n) != EOF)
    {
        edges = 1;
        memset(first, -1, sizeof(first));

        for(int i=1; i<=m; i++){
            int u,v,w;  scanf("%d %d %d", &u,&v,&w);
            add_edge(u, v, w);
            add_edge(v, u, w);
        }

        dijkstra(1);

        printf("%d
", dis[n]);
//        int cur = n;
//        while(1) {
//            printf("%d ", cur);
//            if(cur == 1) break;
//            cur = pre[cur];
//        }
    }

    return 0;
}

原文地址:https://www.cnblogs.com/Sunshine-tcf/p/5693654.html