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.
Input and Output Sample input example # #:
5 a D 6B d 3C e 9d z 8e z 3
Sample # # of output:
B 11
Description
Translation from Nocow
Usaco 2.4
Code
1#include <algorithm>2#include <iostream>3#include <cstring>4#include <cstdio>5#include <vector>6#include <queue>7#include <map>8 #defineMAXN 300059 #defineINF 0x3f3f3f3fTen using namespacestd; One Amap<Char,int>m; -vector<int>G[MAXN],C[MAXN]; - intM,DIS[MAXN],VIS[MAXN]; the - structcc{intnum,d;}; -CC Make (intNumintd) {cc A;a.num=num;a.d=d;returnA;} - structcmp{BOOL operator() (cc A,CC b) {returnA.d>B.D;}}; + intTransCharA) {returnm[a];} - + voidInit_ () { A intj=1; for(CharI='a'; i<='Z'; i++,j++) {m[i]=J;} atj= -; for(CharI='A'; i<='Z'; i++,j++) {m[i]=J;} -scanf"%d",&M); - - for(intI=1; i<=m;i++){ - Charb;intW//format input??? -Cin>>a>>b>>W; in G[trans (a)].push_back (trans (b)); C[trans (a)].push_back (w); - G[trans (b)].push_back (trans (a)); C[trans (b)].push_back (w); to } + } - the * voidDijkstra () { $Priority_queue<cc,vector<cc>,cmp>Q;Panax Notoginsengmemset (DIS,0x3f,sizeof(DIS)); - intS=trans ('Z'); thedis[s]=0; Q.push (make (s),0)); + while(!Q.empty ()) { A intx=q.top (). Num;q.pop (); the if(Vis[x])Continue; vis[x]=1; + //puts ("@"); - for(intI=0; I<g[x].size (); i++){ $ intto=G[x][i]; $ if(dis[x]+c[x][i]<Dis[to]) { -dis[to]=dis[x]+C[x][i]; - Q.push (Make (to,dis[to])); the } - }Wuyi } the } - Wu voidWork () { - Dijkstra (); About CharAns_num;intans_step=INF; $ - for(CharI='A';i<'Z'; i++) - if(Dis[trans (i)]<ans_step) -ans_num=i,ans_step=Dis[trans (i)]; A +cout<<ans_num<<" "<<ans_step<<Endl; the } - $ intMain () { the //freopen ("01.in", "R", stdin);//freopen ("01.out", "w", stdout); the the Init_ (); the Work (); - inFclose (stdin); fclose (stdout);return 0; the}
Review the magical combination of the shortest road +map
Rokua P1529 Bessie Come Home Label:dijkstra shortest way && wtf