Advertisement
akashtadwai

bitmask_dp

Aug 31st, 2021
1,078
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define pb push_back
  4. #define int long long
  5. #define all(x) x.begin(), x.end()
  6. #define ar array
  7. #define IOS                         \
  8.   std::ios::sync_with_stdio(false); \
  9.   cin.tie(NULL);
  10. const int inf = 1e12;
  11. int mx;
  12. int n;
  13. vector<pair<int,string>>arr;
  14. vector<vector<int>>dp;
  15. int dfs(int idx, int msk){
  16.     if(idx==n){
  17.         if( __builtin_popcount(msk) == mx ) return  0;
  18.         return inf;
  19.     }
  20.     if(dp[idx][msk]!=-1)
  21.         return dp[idx][msk];
  22.         int new_msk= msk;
  23.         string str = arr[idx].second;
  24.         for(int j=0;j<10;j++) {
  25.             int num = (str[j]-'0');
  26.             if(num==1)
  27.                 new_msk|=(1<<(10-j));
  28.         }
  29. dp[idx][msk] = min({arr[idx].first+dfs(idx+1,new_msk),dfs(idx+1,msk)});
  30.     return dp[idx][msk];
  31. }
  32. void init()
  33. {
  34. set<int>distinct;
  35. cin>>n;
  36. arr.resize(n);
  37. dp=vector<vector<int>>(n+1,vector<int>(( (1<<10)+1),-1));
  38.  for(int i=0;i<n;i++) {
  39.      cin>>arr[i].first>>arr[i].second;
  40.      for(int j=0;j<10;j++){
  41.          if(arr[i].second[j] == '1') distinct.insert(j);
  42.      }
  43.  }
  44.  mx = distinct.size();
  45.  cout<<dfs(0,0)<<endl;
  46. }
  47. int32_t main()
  48. {
  49.   IOS;
  50.   init();
  51.   return 0;
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement