2.4.4 Bessie Come Home

Bessie Come Home
Kolstad & Burch

It's dinner time, and the cows are out in their separate pastures. Farmer John rings the bell so they will start walking to the barn. Your job is to figure out which one cow gets to the barn first (the supplied test data will always have exactly one fastest cow).

Between milkings, each cow is located in her own pasture, though some pastures have no cows in them. Each pasture is connected by a path to one or more other pastures (potentially including itself). Sometimes, two (potentially self-same) pastures are connected by more than one path. One or more of the pastures has a path to the barn. Thus, all cows have a path to the barn and they always know the shortest path. Of course, cows can go either direction on a path and they all walk at the same speed.

The pastures are labeled `a'..`z' and `A'..`Y'. One cow is in each pasture labeled with a capital letter. No cow is in a pasture labeled with a lower case letter. The barn's label is `Z'; no cows are in the barn, though.

PROGRAM NAME: comehome

INPUT FORMAT

Line 1: Integer P (1 <= P <= 10000) the number of paths that interconnect the pastures (and the barn)
Line 2..P+1: Space separated, two letters and an integer: the names of the interconnected pastures/barn and the distance between them (1 <= distance <= 1000)

SAMPLE INPUT (file comehome.in)

5
A d 6
B d 3
C e 9
d Z 8
e Z 3

OUTPUT FORMAT

A single line containing two items: the capital letter name of the pasture of the cow that arrives first back at the barn, the length of the path followed by that cow.

SAMPLE OUTPUT (file comehome.out)

B 11
/*
ID: makeeca1
LANG: C++
TASK: comehome
*/
 
#include <algorithm>
#include <climits>
#include <cstdio>
#include <cctype>
#include <queue>
using namespace std;
bool flag[52] = {};
int a[52][52], dist[52];
 
int main()
{
    int p;
    freopen("comehome.in", "r", stdin);
    freopen("comehome.out", "w", stdout);
    for (int i = 0; i < 52; ++i)
        fill_n(a[i], 52, INT_MAX/2);
    for (scanf("%d", &p); p; --p)
    {
        char u, v;
        int w;
        scanf("%*c%c %c%d", &u, &v, &w);
        u = islower(u) ? u-'a' : u-'A'+26;
        v = islower(v) ? v-'a' : v-'A'+26;
        if (w < a[u][v])
            a[u][v] = w, a[v][u] = w;
    }
    copy(a[51], a[51]+52, dist);
    for(;;)
    {
        int min_dist = INT_MAX, u;
        for (int i = 0; i < 51; ++i)
            if (!flag[i] && dist[i] < min_dist)
                min_dist = dist[i], u = i;
        if (u >= 26) return printf("%c %d\n", u-26+'A', min_dist), 0;
        flag[u] = true;
        for (int i = 0; i < 51; ++i)
            if (!flag[i] && dist[u]+a[u][i] < dist[i])
                dist[i] = dist[u]+a[u][i];
    }
}

 

转载于:https://www.cnblogs.com/makeecat/p/3274638.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值