Classic TSP variants
Learn: 1, Floyd O (n^3) processing random two points of the shortest
2, the collection of bits, I will in the final summary of the written out. Note the state of the bits must be designed before writing the code. In the subject, the No. 0 to the nth place represents the first city, 1 is already traversed, 0 did not walk
So DP equation: dp[s][i]--At present in the city I. The state is S (s) stored by those cities that have traveled
3, the final request to form a circuit, then is min (dp[1<< (n+1) -1][i],dp[0][i])
#include <cstdio> #include <cstring> #include <algorithm> #include <string> #include < iostream> #include <cmath> #include <map> #include <queue>using namespace std; #define LS (RT) rt*2# Define RS (RT) Rt*2+1#define ll Long Long#define Rep (i,s,e) for (int. i=s;i<e;i++) #define REPE (i,s,e) for (int i=s;i<=e ; i++) #define CL (A, B) memset (A,b,sizeof (a)) #define in (s) freopen (S, "R", stdin) const int MAXN = 12;int Dis[maxn][maxn];int dp[1<<maxn][maxn];const int INF = 1e9+10;int n;void Floyd () {Rep (k,0,n+1) Rep (i,0,n+1) Rep (j,0, n+1) Dis[i][j]=min (Dis[i][k]+dis[k][j],dis[i][j]);} int main () {//in ("poj3311.txt"); int Len; while (~SCANF ("%d", &n) && N) {rep (i,0,n+1) Rep (j,0,n+1) Dp[i][j]=dis[i][j] =inf; Rep (i,0,n+1) Rep (j,0,n+1) {scanf ("%d", &len); Dis[i][j]=min (Dis[i][j],len); } Floyd ();Find out the random distance of two points int s=1<< (n+1); Rep (i,0,s) Rep (j,0,n+1) {dp[i][j]=inf; } for (int s=0;s<s;s++)//Enumerate All state Rep (i,0,n+1) {if (s& (1<< (i))) {if (s== (1<<i)) dp[s][i]=dis[0][i]; Else Rep (j,0,n+1) if (s& (1<<j) && i!=j) {Dp[s][i]=min (dp[s^ (1<<i)][j]+dis[j][i],dp[s][i]); }}} int ans=inf; for (int i=0;i<n+1;i++) ans=min (ans,dp[(S-1)][i]+dis[i][0]); printf ("%d\n", ans); } return 0;}
POJ 3311-Shape pressure DP