019 school recruit Zhenti exercise graduation trip problem (headline)

Graduation trip problem

Title Description
Xiao Ming is currently doing a graduation trip planning. Intended departure from Beijing, respectively, to a number of cities, and then returned to Beijing, between each city are riding high-speed rail, and to each city only once. Due to limited funds, hoping to arrange some of the expenses of the province on the road as much as possible by reasonable route. Given a set of train between the city and the price of each pair of cities, each city found only visited once and return to the starting point of a minimum fare expenses.

Input Description :
City number n (1 <n≤20, including Beijing)
ticket price between cities m n matrix of n rows and columns [n] [n]

Output Description :
The minimum fare s spending

python language've made the cattle off-line question seems to have problems, overtime has been done to prove safety Offer subject before, there are a happen.

This platform-level language to kill quite deceptive.

Looking for a C ++ code:

 1 #include<iostream>
 2 #include<vector>
 3 #include<unordered_map>
 4 using namespace std;
 5  
 6 int getAns(vector<vector<int>> &nums){
 7      
 8     int M = 0x7ffffff;
 9     int n = nums.size();
10     vector<vector<int>> dp(1<<n, vector<int>(n,M));
11     dp[1][0] = 0;
12     for(int i=1; i<n; i++) dp[1<<i][i] = M;
13     for(int i=1; i<(1<<n); i++){
14         for(int j=0; j<n; j++){
15             if(dp[i][j]!=M){
16                 for(int k=0; k<n; k++){
17                     if((i&(1<<k))==0){
18                         dp[i|(1<<k)][k] = min(dp[i|(1<<k)][k], dp[i][j]+nums[j][k]);
19                     }
20                 }
21             }
22         }
23     }
24     int ans = M;
25     for(int i=1; i<n; i++){
26         ans = min(ans, dp[(1<<n)-1][i]+nums[i][0]);
27     }
28     return ans;
29 }
30 int main(){
31     int n;
32     while(cin>>n){
33         vector<vector<int>> edges(n,vector<int>(n,0));
34         int x;
35         for(int i=0; i<n; i++){
36             for(int j=0; j<n; j++){
37                 cin>>edges[i][j];
38             }
39         }
40         cout<<getAns(edges)<<endl;
41     }
42     return 0;
43 }

 

Guess you like

Origin www.cnblogs.com/asenyang/p/11222400.html