This is a problem of violence can be had, in fact, is the focus of understanding the problem, the input process is a bit cumbersome.
1. The meaning of problems
Give a given undirected graph to rearrange the order of the nodes, such that the maximum distance between adjacent nodes in a minimal arrangement
2.Solution
Significant burst search, the challenge is how to model, in fact, as long as there have been an array of characters into the record up, and then the search time in order to arrange the letters of each node
Block explained:
(1) Input
// First, we can put the ":" character preceding presented as u, then in: back out to do character V @ Coder-CJH void the init () { Mind = 1E9; TOT = 0 ; Memset (V, 0 , the sizeof (V)); Memset (ID, 0 , the sizeof (ID)); Memset (CAN, 0 , the sizeof (CAN)); // Sets of not clear, two lines of tears burst zero for ( int I = 0 ; I <s.size (); I ++ ) IF (the isalpha (S [I])) IF ! (ID [S [I] - ' A ' ]) ID [S [I] - ' A '] = . 1 , TR [TOT ++] = S [I] - ' A ' ; // if there is no record encountered on int LS = S [ 0 ] - ' A ' ; // the first character must would be letters for ( int I = . 1 ; I <s.size (); I ++ ) IF (S [I] == ' : ' ) { // If ":", then is V ++ I; the while (! I <s.size () && S [I] = ' ; ' ) V [LS] [S [I] - ' A ' ] = V [S [I] - ' A ' ] [LS] =. 1 , I ++; // been read; } the else IF (S [I] =! ' ; ' ) LS = S [I] - ' A ' ; // once to find the u }
(2) Search
// Coder-CJH BOOL cmp ( int A [], int ANS []) { // because little counsels, the handwriting cmp function for ( int I = . 1 ; I <= TOT; I ++ ) IF (A [I]! = ANS [I]) return A [I]> ANS [I]; return to false ; } void DFS ( int STEP, int MD) { IF (MD> Mind) return ; // optimal pruning IF (STEP = TOT + = . 1 ) { IF (MD == Mind && CMP (A, ANS)) return; mind=md,memcpy(ans,a,sizeof(a)); return; } for(int i=1;i<=tot;i++) if(!can[tr[i]]){ can[tr[i]]=1;//打上标记 a[step]=tr[i]; int tmp=0; for(int j=1;j<step;j++) if(v[tr[i]][a[j]])tmp=max(tmp,step-j);//计算出目前的带宽 dfs(step+1,max(tmp,md)); can[tr[i]]=0; } }