Gossip, Bessie is one of the favorite teachers in the University. She is a cute english teacher. Haha ~~
My idea for this question is BFS + greedy, which is a typical Dijkstra algorithm application. There may be multiple paths between the other two farms, so we need to pre-process the input. Because there are only 52 nodes in total and only 2704 possibilities can be calculated by means of permutation and combination, it is useless to input many edges.
The last step is to pay attention to the efficiency. The accessed nodes may find a new path to bring them closer to the Z node. Therefore, we need to make two comparisons, namely, the following code, because of this, I once again wa, alas...
if(path_len[i]+graph[i][j] < path_len[j]) path_len[j] = path_len[i]+graph[i][j];else if(path_len[j]+graph[i][j] < path_len[i]) path_len[i] = path_len[j]+graph[i][j];
/* ID: fairyroadtask: comehomelang: C ++ */# include <fstream> # include <deque> # include <climits> using namespace STD; ifstream fin ("comehome. in "); ofstream fout (" comehome. out "); inline int charcast (char ch) {If (CH-'A'> 25) return ch-'A'; else return ch-'A' + 26 ;} int N; int graph [52] [52]; int path_len [52]; // path_len [I] indicates the distance from the letter I represents to Z int main () {int I, j, weight, IA, IB; Fin> N; char a, B; for (I = 0; I <n; ++ I ){ Fin> A> B> weight; IA = charcast (A), IB = charcast (B ); if (graph [Ia] [IB] = 0 | weight <graph [Ia] [IB]) {graph [Ia] [IB] = weight; graph [IB] [Ia] = weight ;}for (I = 0; I <51; ++ I) path_len [I] = int_max; path_len [51] = 0; deque <int> q; q. push_back (51); While (! Q. Empty () {I = Q. Front (); For (j = 0; j <52; ++ J) {If (graph [I] [J]! = 0) // graph [I] [J] is equal to 0, indicating that the road (I, j) has been accessed or the road cannot be accessed. {q. push_back (j); If (path_len [I] + graph [I] [J] <path_len [J]) path_len [J] = path_len [I] + graph [I] [J]; else if (path_len [J] + graph [I] [J] <path_len [I]) path_len [I] = path_len [J] + graph [I] [J]; graph [I] [J] = 0; graph [J] [I] = 0 ;}} q. pop_front ();} int res = path_len [26], Index = 26; for (I = 27; I <51; ++ I) if (RES> path_len [I]) {res = path_len [I]; Index = I;} fout <(char) (index-26 + 'A') <''<res <Endl; return 0 ;}