Bessie Come Home(最短路 + Floyd)

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

 

     题意:

     给出 P(1 ~ 10000)条路,后给出这 P 条无向边的两端和距离(1 ~ 1000), 大写字母表示的是有牛的位置,小写字母表示该处没有牛但是有个转角点,终点是Z,求哪头牛最快到达终点,输出这头牛的编号和距离。

 

     思路:

     最短路 + Floyd。开 60 大小的数组,代表 A ~ Z 和 a ~ z 全部,同时用数组 cow 标记哪头牛是存在的,最后比较得出最小值即可。

 

     AC:

/*
TASK:comehome
LANG:C++
ID:sum-g1
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define INF 99999999
using namespace std;

int w[60][60];
bool cow[30];

void init() {
    for(int i = 0; i < 60; ++i)
            for(int j = 0; j < 60; ++j)
                    w[i][j] = INF;

    for(int i = 0; i <= 26; ++i)  cow[i] = false;
}

void floyd() {
    for(int k = 0; k < 60; ++k)
            for(int i = 0; i < 60; ++i)
                    for(int j = 0; j < 60; ++j)
                            if(w[i][k] < INF &&
                               w[k][j] < INF &&
                               w[i][j] > w[i][k] + w[k][j])
                               w[i][j] = w[i][k] + w[k][j];
}

int main() {
    freopen("comehome.in","r",stdin);
    freopen("comehome.out","w",stdout);
    int n;
    init();

    scanf("%d",&n);
    getchar();
    while(n--) {
        char a,b;
        int dis;
        scanf("%c %c%d",&a,&b,&dis);
        getchar();

        if(a >= 'A' && a <= 'Z') cow[a - 'A'] = true;
        if(b >= 'A' && b <= 'Z') cow[b - 'A'] = true;

        w[a - 'A'][b - 'A'] = min(w[a - 'A'][b - 'A'],dis);
        w[b - 'A'][a - 'A'] = min(w[b - 'A'][a - 'A'],dis);
    }

    floyd();

    int min_step = INF;
    char min_cow;

    for(int i = 0; i < 25; ++i)
            if(cow[i] && w[i][25] < min_step) {
                    min_step = w[i][25];
                    min_cow = 'A' + i;
            }

    printf("%c %d\n",min_cow,min_step);
    return 0;
}

 

 

你可能感兴趣的:(floyd)