天天看点

POJ - 2387 Til the Cows Come Home(最短路Dijkstra模板题)

Til the Cows Come Home

Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u

Submit Status

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.

Source

USACO 2004 November

题意:给出 m 条无向边, n 个点,求编号为 n 的点到编号为 1 的点的长度。 数据巨小,每条边的长度 1~100.

定点数 2~1000,边数小于2000.

思路:模板题。

Dijkstra , Bellman,SPFA 都可以,此题有重边。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;

const int INF = 0x3f3f3f3f;
const int MAXN = 2010;
const double PI = acos(-1.0);
const double e = 2.718281828459;
const double eps = 1e-8;
int dist[MAXN];
int g[MAXN][MAXN];
int vis[MAXN], n;

void Dijkstra(int s)
{
    memset(vis, 0, sizeof(vis));
    for(int i = 1; i <= n; i++)
    {
        dist[i] = g[s][i];
    }
    dist[s] = 0;
    vis[s] = 1;
    for(int i = 1; i <= n-1; i++)
    {
        int u = s;
        int t = INF;
        for(int j = 1; j <= n; j++)
        {
            if(!vis[j] && dist[j]<t)
            {
                u = j;
                t = dist[j];
            }
        }
        if(u == s)
            return ;
        vis[u] = 1;
        for(int j = 1; j <= n; j++)
        {
            if(!vis[j] && dist[j]>dist[u]+g[u][j])
                dist[j] = dist[u]+g[u][j];
        }
    }
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int m;
    int u, v, w;
    while(cin>>m>>n)  //注意:先输入边数,再输入点数,WA了都是泪啊。
    {
        for(int i = 0; i <= n; i++)
        {
            for(int j = 0; j <= n; j++)
            {
                g[i][j] = (i==j)?0:INF;
            }
        }
        for(int i = 0; i < m; i++)
        {
            scanf("%d %d %d", &u, &v, &w);
            if(g[u][v] > w)
                g[u][v] = g[v][u] = w;
        }
        Dijkstra(1);
        printf("%d\n", dist[n]);
    }
    return 0;
}