The most simple and short-circuit problem, the key of which is that some farms do not have cattle. These farms can go, but they cannot be included in the final answer. After all, what is needed in the question is the shortest path of cattle. Take the 'z' point as the source point and perform a naked single-source shortest path to obtain the answer. In the code, I use a hash function to normalize all the punctuation under the letter to the range.
/*ID:sevenst4LANG:C++PROG:comehome*/#include<stdio.h>#define INF 0x0FFFFFFFusing namespace std;int map[53][53];int f( char a ){ if( a>='A' && a<='Z' ) return a-'A'+1; else return a-'a'+1+26;}void dij(){ bool vis[53]; for( int i=0;i<53;i++ ) vis[i]=false; int p=f('Z'); vis[p]=true; for( int i=1;i<53;i++ ) { int min=INF; int index=-1; for( int j=1;j<53;j++ ) if( min>map[p][j] && vis[j]==false ) min=map[p][j],index=j; if( index==-1 )return ; vis[index]=true; for( int j=1;j<53;j++ ) { if( map[p][j]>map[p][index]+map[index][j] ) map[p][j]=map[p][index]+map[index][j]; } } return ;}int main(){ freopen( "comehome.in","r",stdin ); freopen( "comehome.out","w",stdout ); int n; for( int i=0;i<53;i++ ) for( int j=0;j<53;j++ ) map[i][j]=INF; scanf( "%d",&n ); for( int i=1;i<=n;i++ ) { char a,b,c;int len; scanf( "%c",&c ); scanf( "%c %c %d",&a,&b,&len ); if( map[f(a)][f(b)]>len ) map[f(a)][f(b)]=map[f(b)][f(a)]=len;}dij();int min=INF;int index;for( int i=1;i<26;i++ ) if( min>map[f('Z')][i] ) { min=map[f('Z')][i]; index=i; } printf( "%c %d\n",char(index+'A'-1),min ); return 0;}
I haven't written any code for a long time... Dij's forgotten...