Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <string.h>
- using namespace std;
- #include <ext/pb_ds/assoc_container.hpp> //required
- #include <ext/pb_ds/tree_policy.hpp> //required
- using namespace __gnu_pbds; //required
- template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
- #if DEBUG && !ONLINE_JUDGE
- #include "header.h"
- #else
- #define debug(args...)
- #endif
- typedef vector<int> vi;
- typedef pair<int,int> ii;
- typedef vector<ii> vii;
- typedef pair<int,ii> iii;
- typedef vector<pair<ii,int> > viii;
- typedef vector<vi> vvi;
- typedef vector<vii> vvii;
- typedef map<int,int> mii;
- typedef map<char,int> mci;
- typedef long long lli;
- typedef long double ld;
- typedef vector<lli> vlli;
- #define pb push_back
- #define trav(a,x) for(auto& a : x)
- #define all(x) x.begin(),x.end()
- #define SZ(x) (int)(x).size()
- #define IT iterator
- #define se second
- #define lb lower_bound
- #define ub upper_bound
- #define mp(a,b) make_pair(a,b)
- #define init(v,n) (v).clear();(v).resize(n)
- #define initi(v,n,i) (v).clear();(v).resize(n,i)
- #define rep(i,a,b) for(auto i=(a);i<b;i++)
- #define INF (int)1e9
- #define EPS 1e-9;
- #define PI 3.14159265359
- #define swap(a,b) { auto c = a; a=b; b=c; }
- #define EULER 2.7182818284
- #define MOD 1000000007
- void preprocess(void) {
- return;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- cout.precision(20);
- freopen("input.txt","r",stdin);
- freopen("output.txt","w",stdout);
- preprocess();
- int t; cin>>t;
- rep(ttt,1,t+1) {
- int n,m,p;
- cin>>n>>m>>p;
- vector<string> st(n), forb(m);
- rep(i,0,n) cin>>st[i];
- rep(i,0,m) cin>>forb[i];
- map<string, int> mf;
- rep(i,0,m) mf[forb[i]]=1;
- int ch[100][2];
- memset(ch,0,sizeof ch);
- rep(i,0,n) {
- rep(j,0,p) {
- ch[j][st[i][j]-'0']++;
- }
- }
- vector<ii> costp;
- string ans;
- vi cc;
- int initcost=0;
- rep(i,0,p) {
- if(ch[i][0]<=ch[i][1]) {
- initcost+=ch[i][0];
- ans+='1';
- }
- else {
- initcost+=ch[i][1];
- ans+='0';
- }
- cc.pb(abs(ch[i][0]-ch[i][1]));
- }
- rep(i,0,p) {
- costp.pb(mp(cc[i],-i));
- }
- rep(i,0,p) {
- rep(j,i+1,p) {
- costp.pb(mp(cc[i]+cc[j],i*100+j));
- }
- }
- int finans=MOD;
- map<string, int> dist;
- priority_queue<pair<int, pair<string,int> > > pqq;
- pqq.push(mp(0,mp(ans,0)));
- while(!pqq.empty()) {
- auto ddd = -pqq.top().first;
- auto u = pqq.top().se;
- if(mf[u.first]!=1) {
- finans = initcost+ddd;
- break;
- }
- pqq.pop();
- for(int i = u.se;i<p;i++) {
- int cd = cc[i];
- string hh = u.first; hh[i]=(u.first[i]=='1'?'0':'1');
- pqq.push(mp(-(ddd+cd),mp(hh,i+1)));
- }
- }
- cout<<"Case #"<<ttt<<": "<<finans<<endl;
- }
- }
Add Comment
Please, Sign In to add comment