POJ-3311 Hie with the Pie

POJ - 3311

裸状压,刚开始太天真没求最短路,脑子抽了。。。

 

 1 #include<bits/stdc++.h>
 2 #define fi first
 3 #define se second
 4 #define mk make_pair
 5 #define pii pair<int,int>
 6 #define read(x) scanf("%d",&x)
 7 #define sread(x) scanf("%s",x)
 8 #define dread(x) scanf("%lf",&x)
 9 #define lread(x) scanf("%lld",&x)
10 using namespace std;
11 
12 typedef long long ll;
13 const int N=13;
14 const int inf=0x3f3f3f3f;
15 const ll INF=0x3f3f3f3f3f3f3f3f;
16 const int mod=1e9+7;
17 
18 int n,dp[N][1<<N],cost[N][N];
19 int main()
20 {
21     while(read(n)!=EOF && n)
22     {
23         memset(dp,inf,sizeof(dp));
24         for(int i=0;i<=n;i++)
25             for(int j=0;j<=n;j++)
26                 read(cost[i][j]);
27         for(int k=0;k<=n;k++)
28             for(int i=0;i<=n;i++)
29                 for(int j=0;j<=n;j++)
30                     cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);
31         dp[0][0]=0;
32         int up=1<<(n+1);
33         for(int s=0;s<up;s++)
34         {
35             for(int i=0;i<=n;i++)
36             {
37                 if(dp[i][s]==inf)
38                     continue;
39                 for(int k=0;k<=n;k++)
40                 {
41                     if(s&(1<<k))
42                         continue;
43                     dp[k][s|(1<<k)]=min(dp[k][s|(1<<k)],dp[i][s]+cost[i][k]);
44                 }
45             }
46         }
47         printf("%d\n",dp[0][up-1]);
48     }
49     return 0;
50 }
51 /*
52 */

 

posted @ 2018-03-07 15:54  NotNight  阅读(105)  评论(0编辑  收藏  举报