Advertisement
istinishat

subset mask dp

Mar 31st, 2018
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //http://lightoj.com/volume_showproblem.php?problem=1264
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. #define si(a) scanf("%d",&a)
  7. #define f first
  8. #define s second
  9. #define mp(a,b) make_pair(a,b)
  10. #define INF 0x3f3f3f3f
  11.  
  12. int precal[20004],dp[20004],vis[20004];
  13.  
  14. int solve(int msk)
  15. {
  16.     if(!msk)return 0;
  17.     int &ret=dp[msk];
  18.     if(vis[msk])return ret;
  19.     vis[msk]=1;
  20.     ret=INF;
  21.     for(int sub=msk;sub;sub=(sub-1)&msk)
  22.         ret=min(ret,precal[sub]+solve(msk^sub));
  23.     return ret;
  24. }
  25.  
  26. int cost[20][20];
  27.  
  28. int main()
  29. {
  30.     int t;
  31.     si(t);
  32.     for(int ca=1;ca<=t;ca++){
  33.         int n,i,j;
  34.         si(n);
  35.         int tot=1<<n;
  36.         for(i=0;i<n;i++)for(j=0;j<n;j++)si(cost[i][j]);
  37.         for(int msk=0;msk<tot;msk++){
  38.             precal[msk]=0;
  39.             for(i=0;i<n;i++){
  40.                 if(msk&(1<<i))
  41.                 for(j=0;j<n;j++){
  42.                     if(msk&(1<<j))
  43.                         precal[msk]+=cost[i][j];
  44.                 }
  45.             }
  46.         }
  47.         memset(vis,0,sizeof(vis));
  48.         printf("Case %d: %d\n",ca,solve(tot-1));
  49.     }
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement