Title Description
It's dinner time, and cows are out in the scattered pastures. The farmer John rang the bell, so they began to walk towards the barn. Your job is to point out which cow will arrive first in the barn (and in the test data given, there is always one and only the fastest cow). At the time of milking (before dinner), every cow is on her own ranch, and some pastures may not have cows. Each ranch is connected by a road and one or more pastures (possibly including itself). Sometimes, more than one road is connected between two ranches (possibly the same letter). at least one pasture and barn have a road connection. As a result, all cows can eventually reach the barn, and the cows always take the shortest path. Of course, cows can move in any direction, and they move at the same speed. The ranch is marked ' a '. Z ' and ' A ' ... ' Y ', there is a cow in uppercase letters, but not in the lower case. The mark of the barn is ' Z ', note that there are no cows in the barn.
Note that ' m ' and ' m ' are not the same ranch otherwise the error above means that the input data may be present in m,m (depressed ing) (PS: Table depressed ...) Tell me the set of how to use it is not depressed ...), such as
M A A M M Z
Input/output format
Input format:
Line 1th: Integer P (1<= p<=10000), indicating the number of roads connected to the pasture (barn).
2nd.. P+1: Two letters and an integer separated by a space:
The markings of the pasture connected to the road and the length of the road (1<= length <=1000).
Output format:
A separate line consists of two items: The mark of the ranch where the cow first reached the barn, and the length of the path the cow walked through.
1#include <iostream>2 using namespacestd;3 Const intinf=1<< -;4 intp,g[ -][ -],d[ -],v[ -];5 intID (Charc) {6 if('A'<=c&&c<='Z')returnC-'A'+1;7 returnC-'a'+ -;8 }9 intMain ()Ten { One for(intI=1; i<= the; i++) for(intj=1; j<= the; j + +) g[i][j]= (i==j?)0: INF); A for(intI=1; i<= the; i++) v[i]=0; - for(intI=1; i<= the; i++) d[i]=inf; -d[ -]=0; the Charf,t; - intDist; -Cin>>P; - while(p--){ +Cin>>f>>t>>Dist; -G[id (f)][id (t)]=g[id (T)][id (f)]=min (dist,g[id (t)][id (f)]); + //There is not necessarily one path between two pastures A } at for(intI=1; i<= the; i++){ - intx,m=inf; - for(intj=1; j<= the; j + +){ - if(!v[j]&& (D[J]<=M)/*less than or equal*/) m=d[x=j]; - } -v[x]=1; in for(intj=1; j<= the; j + +){ - if(!v[j]&&g[x][j]<inf&&g[x][j]+d[x]<D[j]) tod[j]=d[x]+G[x][j]; + } - } the intx,ans=inf; * for(intI=1;i< -; i++){ $ if(D[i]<ans) ans=d[x=i];Panax Notoginseng } -cout<< (Char) (x+'A'-1) <<" "<<ans; the return 0; +}
Usaco Section 2.4 Go home Bessie Come Home