Input: An integer n, n * n Then there rectangle, representing the distance between the two cities to spend, spend between each two cities no more than 10,000.
Output: an integer representing the No. 1 tourist attractions all over the city and back to the minimum cost No. 1 of the city.
Sample input:
4
0 1 1 1
1 0 2 1
5 5 0 6
1 1 3 0
Sample output:
8
Thinking:
1. End search all cities, which represents a depth-first search, coupled with the return from the end to take the No. 1 city that is a full cost fees. So every search deep end (End search all cities) represents a cost results. Record minimum cost to all deep in search of.
2 marks each process has reached deep search in the city, the city has not yet reached to continue the search, the search after the end of a deep, clear the mark. Deep search for the next, resulting in different costs result.
For example: 1,2,3,4 four cities.
Search options:
1->2->3->4、1->2->4->3、1->3>2->4、1->3->4->2、1->4->2->3、1->4->3->2 ......
. 1 #include <the iostream> 2 #include <cstdio> . 3 the using namespace STD; . 4 int n-; . 5 int ANS = 100000000 ; . 6 int G [ 100 ] [ 100 ]; . 7 BOOL VIS [ 100 ] = { 0 }; . 8 void the DFS ( int U, int cnt, int sum) { 9 // optimality pruning: in a deep search process cost> current completed all the required sum from the cost of ANS, 10 // therefore certainly not a minimum sum , not in the search to the lower, the function returns continue to look for another solution. 11 IF (SUM> ANS) { 12 is return ; 13 is } 14 IF (CNT == n-) { 15 ANS = min (ANS, SUM + G [U] [ . 1 ]); 16 } . 17 VIS [U] = to true ; / / marker present layer recursion searched 18 is for ( int I = . 1 ; I <= n-; I ++ ) { . 19 IF (! {VIS [I]) 20 is DFS (I, CNT + . 1 , SUM + G [U] [I] ); 21 is } 22 is } 23 is VIS [U] =to false ; // clear the flag after the completion of a deep search 24 } 25 int main () 26 is { 27 CIN >> n-; 28 for ( int I = . 1 ; I <= n-; I ++ ) { 29 for ( int J = . 1 ; J <= n-; J ++ ) { 30 CIN >> G [I] [J]; 31 is } 32 } 33 is DFS ( . 1 , . 1 , 0 ); 34 is COUT ANS << << endl; 35 return 0; 36 }