Question: n + 1 points: 0 -- N. Find a path and traverse the point 1 -- N from 0 and return to 0. Each point can go through more than once to find the shortest path.
For the bare TSP problem, use Floyd to find the shortest path between each point and press DP.
The status is represented in n + 1 bits.
Attached template:
1 // first, it is not difficult to use Floyd to calculate the distance from DIS [I] [J] 2 with the intention of 2. // then enumerate all States, 11-bit binary representation of 10 cities and pizza stores, 1 indicates pass, 0 indicates no pass 3 // definition status dp (S, I) indicates in S state, optimal Value to city I 4 // next state transition equation: dp (S, I) = min {dp (s ^ (1 <i-1), K) + dis [k] [J], dp (S, I)}, S ^ (1 <i-1) indicates all states that have not reached city I, 1 <= k <= N 5 // For the status of all 1, that is, S = (1 <n)-1 indicates the status of all cities, the final answer is min {dp (S, I) + dis [I] [0]} 7 // dij [I] [J]: The maximum short circuit from I to J is 8 9 for (INT S = 0; S <= (1 <n)-1; ++ s) // enumerate all States, representing 10 for (INT I = 1; I <= N; ++ I) 11 {12 if (S & (1 <(I-1 ))) // status s has passed city I13 {14 if (S = (1 <(I-1 ))) DP [s] [I] = dis [0] [I]; // status s only goes through city I, and the optimal solution is the DIS from 0 to I, this is also the DP boundary 15 else // If s has passed through multiple cities 16 {17 DP [s] [I] = inf; 18 for (Int J = 1; j <= N; ++ J) 19 {20 if (S & (1 <(J-1) & J! = I) // enumerate other cities not in city I 21 DP [s] [I] = min (DP [s ^ (1 <(I-1)] [J] + dis [J] [I], DP [s] [I]); 22 // In the status of city I, find a proper Intermediate Point J to make the distance shorter. like Floyd, 23} 24} 25} 26} 27 ans = DP [(1 <n) -1] [1] + dis [1] [0]; 28 for (INT I = 2; I <= N; ++ I) 29 If (DP [(1 <n)-1] [I] + dis [I] [0] <ans) 30 ans = DP [(1 <n) -1] [I] + dis [I] [0]; 31 printf ("% d/N", ANS );
Code:
1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 #define INF 1<<28; 5 #define maxn 15 6 7 int a[maxn][maxn]; 8 int dp[1<<maxn][maxn]; 9 int ans,S,n;10 11 int main()12 {13 ios::sync_with_stdio(false);14 while (cin>>n)15 {16 memset(a,0,sizeof(a));17 if (n==0) break;18 else19 {20 for (int i=0;i<=n;i++)21 for (int j=0;j<=n;j++)22 cin>>a[i][j];23 24 for (int k=0;k<=n;k++)25 for (int i=0;i<=n;i++)26 for (int j=0;j<=n;j++)27 {28 if (a[i][k]+a[k][j]<a[i][j])29 a[i][j]=a[i][k]+a[k][j];30 }31 32 for (int S=0;S<=(1<<n)-1;S++)33 for (int i=1;i<=n;i++)34 {35 if (S&(1<<(i-1)))36 {37 if (S==(1<<(i-1))) dp[S][i]=a[0][i];38 else39 {40 dp[S][i]=INF;41 for (int j=1;j<=n;j++)42 {43 if (S&(1<<(j-1))&&(j!=i))44 dp[S][i]=min(dp[S^(1<<(i-1))][j] + a[j][i],dp[S][i]);45 }46 }47 }48 }49 50 ans = dp[(1<<n)-1][1] + a[1][0];51 for(int i = 2;i <= n;i++)52 if(dp[(1<<n)-1][i] + a[i][0] < ans)53 ans = dp[(1<<n)-1][i] + a[i][0];54 55 cout<<ans<<endl;56 }57 }58 59 60 61 62 63 return 0;64 }
View code
Reference:
Http://blog.csdn.net/chinaczy/article/details/5890768
Getting started with poj 3311 TSP