Question: hdoj 3001 travelling
Question: The standard traveling salesman adds a sentence, and each vertex can take up to two times.
Analysis: The state transition equation is exactly the same, but it only requires a three-digit system, because each point has three states: 0, 1, 2.
Define the State: DP [st] [I]: the minimum cost of the current I point when the State is St
Transition equation: DP [now] [J] = min (DP [now] [J], DP [st] [I] + MP [I] [J ]); now is the status that St can transfer at a time.
Pay attention to initialization.
Analysis:
#include <cstdio>#include <algorithm>#include <cstring>#include <string>#include <iostream>#include <vector>using namespace std;const int inf = 0x3f3f3f3f;const int N = 12;int mp[N][N];int n,m;int bit[N],v[N];int dp[60000][N];void isit(){ bit[0]=1; for(int i=1;i<N;i++) bit[i]=bit[i-1]*3;}void solve(int st){ memset(v,0,sizeof(v)); int tmp=0; while(st) { v[tmp++]=(st%3); st/=3; }}int count(){ int ans=0; for(int i=0;i<n;i++) ans+=(v[i]*bit[i]); return ans;}int main(){ isit(); while(~scanf("%d%d",&n,&m)) { memset(mp,inf,sizeof(mp)); for(int i=0;i<m;i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); x--,y--; mp[x][y]=mp[y][x]=min(z,mp[x][y]); } memset(dp,inf,sizeof(dp)); for(int i=0;i<n;i++) { dp[bit[i]][i]=0; } int ans=inf; for(int st=0;st<bit[n];st++) { int ok=1; solve(st); for(int i=0;i<n;i++) { if(v[i]==0) ok=0; if(dp[st][i]==inf) continue; for(int j=0;j<n;j++) { if(v[j]==2 || i==j) continue; if(mp[i][j]==inf) continue; v[j]++; int no=count(); dp[no][j]=min(dp[no][j],dp[st][i]+mp[i][j]); v[j]--; } } if(ok) for(int i=0;i<n;i++) ans=min(ans,dp[st][i]); } if(ans==inf) ans=-1; printf("%d\n",ans); } return 0;}
Hdoj 3001 travelling [3 hexadecimal + traveling salesman]