Artifact Content
Not logged in

Artifact 8f81dce206d33d359c913fe71bbd1efaf2c34acc


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

inline int bitcnt(int x)
{
	int c = 0;
	for(; x; x>>=1)
		c += x&1;
	return c;
}

class OrderOfOperations { public:
	int minTime(vector <string> s)
	{
		const int M = s[0].size();

		vector<int> edge;
		int all = 0;
		for(auto& si: s) {
			int x = 0;
			for(int i=0; i<M; ++i)
				x |= (si[i]=='1')<<i;
			edge.emplace_back(x);
			all |= x;
		}
		vector<int> nbits;
		for(int m=0; m<(1<<M); ++m)
			nbits.emplace_back(bitcnt(m));

		typedef int Vert, Cost;
		typedef pair<Cost,Vert> Edge;
		priority_queue<Edge, vector<Edge>, greater<Edge>> Q;
		Q.emplace(0, 0);
		vector<bool> V(1<<M);
		while(!Q.empty())
		{
			Vert v = Q.top().second;
			Cost c = Q.top().first;
			Q.pop();
			if( V[v] )
				continue;
			V[v] = true;

			if( v == all )
				return c;

			for(auto e: edge)
				if( !V[v|e] )
					Q.emplace(c+nbits[(v|e)&~v]*nbits[(v|e)&~v], v|e);
		}
		return -1;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, OrderOfOperations().minTime(s));}
int main(){

CASE(0)
	string s_[] = {
 "111",
 "001",
 "010"
};
	  vector <string> s(s_, s_+sizeof(s_)/sizeof(*s_)); 
	int _ = 3; 
END
CASE(1)
	string s_[] = {
 "11101",
 "00111",
 "10101",
 "00000",
 "11000"
};
	  vector <string> s(s_, s_+sizeof(s_)/sizeof(*s_)); 
	int _ = 9; 
END
CASE(2)
	string s_[] = {
  "11111111111111111111"
};
	  vector <string> s(s_, s_+sizeof(s_)/sizeof(*s_)); 
	int _ = 400; 
END
CASE(3)
	string s_[] = {
  "1000",
  "1100",
  "1110"
};
	  vector <string> s(s_, s_+sizeof(s_)/sizeof(*s_)); 
	int _ = 3; 
END
CASE(4)
	string s_[] = {
  "111",
  "111",
  "110",
  "100"
};
	  vector <string> s(s_, s_+sizeof(s_)/sizeof(*s_)); 
	int _ = 3; 
END
CASE(5)
	string s_[] = {"00100001010101000101","11011001110001000111","01011001010101111001","01011100110111000111","11000100001100001000","11110000101111111111","10010110010000000111","10001100001101110001","00010001100000010010","10001111001101101011","01010111000111100110","00111100010101100010","00110111000100111111","01100000110001000010","11011001011011101100","11111101100111101100","10011101010111101010","11001100000100011100","10111010000011100101","11001000000000011110","10110010101111110000","10101001011011100010","00101001111000110100","10011000010100011000","00011000001011110111","00111000101010001111","01011110110001111100","01010001100001010100","11001010000011000101","00000100010101101000","11100110101101011101","01110100101010100011","10100000100100010100","00111101010111101100","10101011110111111011","01001101010110000000","00111110001011010101","01011011010100011111","11011110011011111111","11111100000011110111","00110001010001100010","00100101011011100001","10101111001101011011","01011101100100010010","01000001010011101100","11001010100110001110","00010110001001110001","01000001111111110100","01001100101111101011","00010101001100110110"};
	  vector <string> s(s_, s_+sizeof(s_)/sizeof(*s_)); 
	int _ = -2; 
END
CASE(6)
	string s_[] = {
		"1100",
		"0011",
		"0110",
	};
	  vector <string> s(s_, s_+sizeof(s_)/sizeof(*s_)); 
	int _ = 6; 
END
}
// END CUT HERE