MENU

POJ2387 Til the Cows Come Home(最短路径)

2018 年 04 月 22 日 • 阅读: 1150 • 图论阅读设置

Til the Cows Come Home

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 67136Accepted: 22570

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


链接

http://poj.org/problem?id=2387

题意

裸的最短路,测试一下板子。

题解

数据范围小,只有1000,,可以使用Dijkstra或者SPFA。需要注意的是,会出现重边,所以Dijkstra在输入的时候需要处理一下。此外,输入的时候点数和边数的顺序是反着的。

代码

  • Dijkstra
StatusAccepted
Time32ms
Memory4628kB
Length127
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 1010;
const int inf = 0x3f3f3f3f;
int G[maxn][maxn];
bool vis[maxn];
int dist[maxn];
int n, m;

void Dijkstra(int s)
{
    for (int i = 1; i <= n; ++i)
    {
        dist[i] = inf;
        vis[i] = 0;
    }
    dist[s] = 0;
    for (int i = 1; i <= n; ++i)
    {
        int Min = inf, k = -1;
        for (int j = 1; j <= n; ++j)
        {
            if (dist[j] < Min && !vis[j])
            {
                Min = dist[j];
                k = j;
            }
        }
        if (k == -1)
            break;
        vis[k] = true;
        for (int j = 1; j <= n; ++j)
        {
            if (G[k][j] + Min < dist[j] && !vis[j])
                dist[j] = Min + G[k][j];
        }
    }
}

int main()
{
    while (scanf("%d%d", &m, &n) != EOF)
    {
        int u, v, w;
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= n; ++j)
                G[i][j] = ((i == j) ? 0 : inf);
        for (int i = 0; i < m; ++i)
        {
            scanf("%d%d%d", &u, &v, &w);
            G[u][v] = G[v][u] = min(w, G[u][v]); // 处理重边
        }
        Dijkstra(1);
        printf("%d\n", dist[n]);
    }
    return 0;
}
  • SPFA
StatusAccepted
Time47ms
Memory764kB
Length1623
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1010;
int n, m;
bool vis[maxn];
int dist[maxn];
int cnt[maxn];

struct Edge
{
    int v, w;
    Edge(int _v, int _w)
    {
        v = _v;
        w = _w;
    }
};

vector <Edge> edge[maxn];

void addedge(int u, int v, int w)
{
    edge[u].push_back(Edge(v, w));
}

bool spfa(int s)
{
    for (int i = 1; i <= n; ++i)
        dist[i] = inf;
    memset(vis, 0, sizeof(vis));
    memset(cnt, 0, sizeof(cnt));
    dist[s] = 0;
    queue <int> q;
    while (!q.empty())
        q.pop();
    q.push(s);
    vis[s] = true;
    cnt[s] = 1;
    while (!q.empty())
    {
        int u = q.front();
        vis[u] = false;
        q.pop();
        for (int i = 0; i < edge[u].size(); ++i)
        {
            int v = edge[u][i].v;
            if (dist[v] > dist[u] + edge[u][i].w)
            {
                dist[v] = dist[u] + edge[u][i].w;
                if (!vis[v])
                {
                    vis[v] = true;
                    q.push(v);
                    if (++cnt[v] > n)
                        return false;
                }
            }
        }
    }
    return true;
}

int main()
{
    while (scanf("%d%d", &m, &n) != EOF)
    {
        int u, v, w;
        for (int i = 0; i < m; ++i)
        {
            scanf("%d%d%d", &u, &v, &w);
            addedge(u, v, w);
            addedge(v, u, w);
        }
        if (spfa(1))
            printf("%d\n", dist[n]);
    }
    return 0;
}

就测试一下板子,没想到也回WA好几发。

The end.
2018-04-22 星期天
最后编辑于: 2018 年 05 月 06 日