POJ 3311 Hie with the Pie (pressure DP), poj3311
Status compression DP
Dp [I] [j] indicates the minimum time to reach the j City when the I state (whether the city passes through in binary format) is reached.
Transition equation dp [I] [j] = min (dp [I] [k] + d [k] [j], dp [I] [j])
D [k] [j] is the shortest distance from k city to j City. flody should be used first.
#include<bits.stdc++.h>using namespace std;int d[20][20],dp[1<<11][20];int n,m;void flody(){for(int k=0;k<=n;k++)for(int i=0;i<=n;i++)for(int j=0;j<=n;j++){if(d[i][k]+d[k][j]<d[i][j])d[i][j]=d[i][k]+d[k][j];}}void DP(){int ans=INT_MAX;for(int i=0;i<(1<<n);i++)for(int j=1;j<=n;j++) { if(i==(1<<(j-1)))dp[i][j]=d[0][j];elseif(i&(1<<(j-1))){dp[i][j]=INT_MAX;for(int k=1;k<=n;k++)if(k!=j&&(i&(1<<(k-1))))dp[i][j]=min(dp[i^(1<<(j-1))][k]+d[k][j],dp[i][j]);} } for(int i=1;i<=n;i++) ans=min(ans,dp[(1<<n)-1][i]+d[i][0]); printf("%d\n",ans);}int main(){while(scanf("%d",&n)==1&&n){for(int i=0;i<=n;i++)for(int j=0;j<=n;j++)scanf("%d",&d[i][j]); flody(); DP();}return 0;}