Title Description 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 m,m (depressed ing) may exist in the input data, such as
M A A M M Z
Enter a description input Description
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).
outputs description output Description
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.
sample input to sample
5
A d 6
B d 3
C e 9
D Z 8
E Z 3
sample output Sample outputs
B 11
puzzle: A bare shortest, is read into too disgusting ╭ (╯^╰) ╮
The code is as follows :
#include <queue> #include <cstdio> #include <cstring> #include <iostream> using namespace std;
const int maxn=10100;
int first[maxn],nxt[maxn<<1],d[maxn];
int N,minn=0x7fffffff,tot;
BOOL USED[MAXN]; struct Edge {int from,to,cost;}
es[maxn<<1];
void Init () {memset (first,-1,sizeof (first));
Memset (d,63,sizeof (d));
tot=0;
} void Build (int f,int t,int d) {es[++tot]= (edge) {f,t,d};
NXT[TOT]=FIRST[F];
First[f]=tot;
} queue<int> Q;
void SPFA (int s) {d[s]=0;
Q.push (s);
Used[s]=1; while (!
Q.empty ()) {int U=q.front ();
Q.pop ();
used[u]=0;
for (int i=first[u];i!=-1;i=nxt[i]) {int v=es[i].to;
if (d[v]>d[u]+es[i].cost) {d[v]=d[u]+es[i].cost;
if (!used[v]) {Q.push (v);
Used[v]=1; }}}}} int main () {CharNum
scanf ("%d", &n);
Init ();
for (int i=1;i<=n;i++) {char c1,c2;
int ff,tt,dd;
cin>>c1>>c2>>dd;
if (c1<=90) ff=c1-64;
else ff=c1-70;
if (c2<=90) tt=c2-64;
Else tt=c2-70;//the read-in character into the corresponding integer build (FF,TT,DD);
Build (TT,FF,DD); } SPFA (26);//Run from the end of the shortest path for (int i=1;i<=25;i++) if (d[i]<minn&&i!=26) {num=
I
minn=d[i];//Find Minimum} cout<< (char) (num+64) << "" <<minn<<endl;
return 0; }