Guest User

Untitled

a guest
Aug 27th, 2018
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.16 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <string.h>
  3. using namespace std;
  4. #include <ext/pb_ds/assoc_container.hpp> //required
  5. #include <ext/pb_ds/tree_policy.hpp> //required
  6. using namespace __gnu_pbds; //required
  7. template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  8.  
  9. #if DEBUG && !ONLINE_JUDGE
  10.     #include "header.h"
  11. #else
  12.     #define debug(args...)
  13. #endif
  14.  
  15. typedef vector<int> vi;
  16. typedef pair<int,int> ii;
  17. typedef vector<ii> vii;
  18. typedef pair<int,ii> iii;
  19. typedef vector<pair<ii,int> > viii;
  20. typedef vector<vi> vvi;
  21. typedef vector<vii> vvii;
  22. typedef map<int,int> mii;
  23. typedef map<char,int> mci;
  24. typedef long long lli;
  25. typedef long double ld;
  26. typedef vector<lli> vlli;
  27.  
  28.  
  29. #define pb push_back
  30. #define trav(a,x) for(auto& a : x)
  31. #define all(x) x.begin(),x.end()
  32. #define SZ(x) (int)(x).size()
  33. #define IT iterator
  34. #define se second
  35. #define lb lower_bound
  36. #define ub upper_bound
  37. #define mp(a,b) make_pair(a,b)
  38. #define init(v,n) (v).clear();(v).resize(n)
  39. #define initi(v,n,i) (v).clear();(v).resize(n,i)
  40. #define rep(i,a,b) for(auto i=(a);i<b;i++)
  41.  
  42. #define INF (int)1e9
  43. #define EPS 1e-9;
  44. #define PI 3.14159265359
  45. #define swap(a,b) { auto c = a; a=b; b=c; }
  46. #define EULER 2.7182818284
  47. #define MOD 1000000007
  48. void preprocess(void) {
  49.     return;
  50. }
  51. int main() {
  52.     ios_base::sync_with_stdio(false);
  53.     cin.tie(NULL);
  54.     cout.precision(20);
  55.     freopen("input.txt","r",stdin);
  56.     freopen("output.txt","w",stdout);
  57.     preprocess();
  58.     int t; cin>>t;
  59.     rep(ttt,1,t+1) {
  60.         int n,m,p;
  61.         cin>>n>>m>>p;
  62.         vector<string> st(n), forb(m);
  63.         rep(i,0,n) cin>>st[i];
  64.         rep(i,0,m) cin>>forb[i];
  65.         map<string, int> mf;
  66.         rep(i,0,m) mf[forb[i]]=1;
  67.         int ch[100][2];
  68.         memset(ch,0,sizeof ch);
  69.         rep(i,0,n) {
  70.             rep(j,0,p) {
  71.                 ch[j][st[i][j]-'0']++;
  72.             }
  73.         }
  74.         vector<ii> costp;
  75.         string ans;
  76.         vi cc;
  77.         int initcost=0;
  78.         rep(i,0,p) {
  79.             if(ch[i][0]<=ch[i][1]) {
  80.                 initcost+=ch[i][0];
  81.                 ans+='1';
  82.             }
  83.             else {
  84.                 initcost+=ch[i][1];
  85.                 ans+='0';
  86.             }
  87.             cc.pb(abs(ch[i][0]-ch[i][1]));
  88.         }
  89.         rep(i,0,p) {
  90.             costp.pb(mp(cc[i],-i));
  91.         }
  92.         rep(i,0,p) {
  93.             rep(j,i+1,p) {
  94.                 costp.pb(mp(cc[i]+cc[j],i*100+j));
  95.             }
  96.         }
  97.         int finans=MOD;
  98.         map<string, int> dist;
  99.         priority_queue<pair<int, pair<string,int> > > pqq;
  100.         pqq.push(mp(0,mp(ans,0)));
  101.         while(!pqq.empty()) {
  102.             auto ddd = -pqq.top().first;
  103.             auto u = pqq.top().se;
  104.             if(mf[u.first]!=1) {
  105.                 finans = initcost+ddd;
  106.                 break;
  107.             }
  108.             pqq.pop();
  109.             for(int i = u.se;i<p;i++) {
  110.                 int cd = cc[i];
  111.                 string hh = u.first; hh[i]=(u.first[i]=='1'?'0':'1');
  112.                 pqq.push(mp(-(ddd+cd),mp(hh,i+1)));
  113.             }
  114.         }
  115.         cout<<"Case #"<<ttt<<": "<<finans<<endl;
  116.     }
  117. }
Add Comment
Please, Sign In to add comment