【POJ3311】Hie with the Pie(状压DP,最短路)

题意:

思路:状压DP入门题

 1 #include<cstdio>
 2 #include<cstdlib> 
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<iostream>
 6 #include<vector>
 7 #include<map>
 8 #include<set>
 9 #define oo 1000000000
10 #define N (1<<12)-1
11 using namespace std;
12 int dp[N][12];
13 int f[12][12];
14 int n;
15 
16 int main()
17 {
18     //freopen("poj3311.in","r",stdin);
19     //freopen("poj3311.out","w",stdout);
20     while(scanf("%d",&n)==1&&n)
21     {
22         n++;
23         for(int i=1;i<=n;i++)
24          for(int j=1;j<=n;j++) scanf("%d",&f[i][j]);
25         int MAX=(1<<n)-1; 
26         for(int i=1;i<=MAX;i++)
27          for(int j=1;j<=n;j++) dp[i][j]=oo; 
28         for(int i=1;i<=n;i++)
29          for(int j=1;j<=n;j++)
30           for(int k=1;k<=n;k++)
31            if((i!=j)&&(i!=k)&&(j!=k)) f[j][k]=min(f[j][k],f[j][i]+f[i][k]);
32         dp[1][1]=0;
33         for(int sta=1;sta<=MAX;sta++)
34          for(int i=1;i<=n;i++) 
35           for(int j=1;j<=n;j++)
36            if(!(sta&(1<<(j-1)))) dp[sta|(1<<(j-1))][j]=min(dp[sta|(1<<(j-1))][j],dp[sta][i]+f[i][j]); 
37         int ans=oo;
38         for(int i=1;i<=n;i++) ans=min(ans,dp[MAX][i]+f[i][1]);
39         printf("%d\n",ans);
40 
41     }
42     return 0;
43 }
44  

猜你喜欢

转载自www.cnblogs.com/myx12345/p/9288948.html