天天看點

[usaco]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

--------------------------------------------------------------------

本題就是一個拓撲排序,找到到根節點最近的帶權節點。

由于最多有26×2=52個節點。是以直接用關聯矩陣就可以了。

我的解法:

——————————————————————————————————————————————————————————