POJ 2387 - Til the Cows Come Home

A - Til the Cows Come Home
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  POJ 2387
Appoint description:  System Crawler  (2016-01-29)

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.


四种算法练手:

//Dijkstra
#include 
#include 
#include 
#include 
using namespace std;

#define N 1005
#define T 2005
#define INF 0x3f3f3f3f
int t,n;
int vis[N];
int g[N][N];
int d[N];

int dijkstra(int u){
    memset(vis,0,sizeof(vis));
    memset(d,0x3f,sizeof(d));
    d[u]=0;

    for (int i=0;id[k]+g[k][j])
                d[j]=d[k]+g[k][j];
    }

    return d[1];
}

int main(){
    while (~scanf("%d%d",&t,&n)){
        memset(g,0x3f,sizeof(g));
        for (int i=0;id)
                g[u][v]=g[v][u]=d;
        }

        printf("%d\n",dijkstra(n));
    }

    return 0;
}


//priority_queue dijkstra
#include 
#include 
#include 
#include 
#include 
using namespace std;

#define N 1005
#define T 2005
#define INF 0x3f3f3f3f
int t,n;
int g[N][N];
int d[N];

struct dnode{
    int u,d;
    dnode(){}
    dnode(int uu,int dd):u(uu),d(dd){}
    bool operator < (const dnode &a) const{
        return d==a.d?u>a.u:d>a.d;
    }
};

int dijkstra(int u){
    priority_queue q;
    memset(d,0x3f,sizeof(d));
    d[u]=0;
    q.push(dnode(u,d[u]));

    while (!q.empty()){
        dnode cur=q.top(); q.pop();

        for (int i=1;i<=n;i++){
            if (d[i]>cur.d+g[cur.u][i]){
                d[i]=cur.d+g[cur.u][i];
                q.push(dnode(i,d[i]));
            }
        }
    }

    return d[1];
}

int main(){
    while (~scanf("%d%d",&t,&n)){
        memset(g,0x3f,sizeof(g));
        for (int i=0;id)
                g[u][v]=g[v][u]=d;
        }

        printf("%d\n",dijkstra(n));
    }

    return 0;
}


//bellman_ford
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

#define N 1005
#define T 2005
#define INF 0x3f3f3f3f

int g[N][N];

struct Edge{
    int u,v,d;
    Edge(int uu,int vv,int dd):u(uu),v(vv),d(dd){}
};
vector edge;

int t,n;
int u,v,l;
int d[N];

bool ford(int u){
    memset(d,0x3f,sizeof(d));
    d[u]=0;

    for (int i=1;id[u]+l){
                d[v]=d[u]+l;
                flag=true;
            }
        }
        if (!flag)
            return true;
    }

    for (int j=1;jd[edge[j].u]+edge[j].d)
            return false;

    return true;
}

int main(){
    while (~scanf("%d%d",&t,&n)){
        memset(g,0x3f,sizeof(g));
        for (int i=0;il)
                g[u][v]=g[v][u]=l;
        }

        for (int i=1;i<=n;i++)
            for (int j=1;j<=n;j++)
                if (g[i][j]!=INF)
                    edge.push_back(Edge(i,j,g[i][j]));

        if (ford(n)){
            printf("%d\n",d[1]);
        }
    }

    return 0;
}


//SPFA
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

#define N 1005
#define T 2005
#define INF 0x3f3f3f3f

int g[N][N];
int t,n;
int u,v,l;
int d[N],vis[N];
int cnt[N];

int SPFA(int u){
    queue q;
    memset(d,INF,sizeof(d));
    memset(vis,0,sizeof(vis));
    q.push(u); vis[u]=1; d[u]=0;

    while (!q.empty()){
        u=q.front(); q.pop();
        vis[u]=0;
        for (int v=1;v<=n;v++){
            if (d[u]+g[u][v] < d[v]){
                d[v]=d[u]+g[u][v];
                if (!vis[v]){
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }

    return 1;
}

int main(){
    while (~scanf("%d%d",&t,&n)){
        memset(g,0x3f,sizeof(g));
        for (int i=0;il)
                g[u][v]=g[v][u]=l;
        }

        if (SPFA(n)){
            printf("%d\n",d[1]);
        }
    }

    return 0;
}


你可能感兴趣的:(POJ,最短路)