Artifact Content
Not logged in

Artifact f35e47b64486be8c4e52d251a1453c14fb5e4e61


#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 <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

static const int NV = 128;
typedef int           flow;
typedef int           vert;
typedef vert          edge;
typedef vector<edge>  edges;
typedef vector<edges> graph;
typedef flow          flow_graph[NV][NV];

flow dinic_dfs( graph& G, flow_graph F, vert v, vert D,
                int LV[], flow flow_in, int blocked[] )
{
	flow flow_out = 0;
	for(int i=0; i!=G[v].size(); ++i) {
		int u = G[v][i];
		if( LV[v]+1==LV[u] && F[v][u] ) {
			flow f = min(flow_in-flow_out, F[v][u]);
			if( u==D || !blocked[u] && (f=dinic_dfs(G,F,u,D,LV,f,blocked)) ) {
				F[v][u]  -= f;
				F[u][v]  += f;
				flow_out += f;
				if( flow_in == flow_out ) return flow_out;
			}
		}
	}
	blocked[v] = (flow_out==0);
	return flow_out;
}

flow maxFlow( graph& G, flow_graph F, vert S, vert D )
{
	for( flow total=0 ;; ) {
		int LV[NV] = {0};
		vector<int> Q(1, S);
		for(int lv=1; !Q.empty(); ++lv) {
			vector<int> Q2;
			for(int i=0; i!=Q.size(); ++i) {
				edges& ne = G[Q[i]];
				for(int j=0; j!=ne.size(); ++j)
					if( F[Q[i]][ne[j]] && !LV[ne[j]] && ne[j]!=S )
						LV[ne[j]]=lv, Q2.push_back(ne[j]);
			}
			Q.swap(Q2);
		}

		if( !LV[D] )
			return total;

		int blocked[NV] = {};
		total += dinic_dfs( G, F, S, D, LV, 0x7fffffff, blocked );
	}
}

flow_graph F, XXF;

class ConnectingAirports {
public:
	vector <string> getSchedule(vector <int> capacityA, vector <int> capacityB) 
	{
		int NA = capacityA.size();
		int NB = capacityB.size();
		int CAsum = accumulate(capacityA.begin(), capacityA.end(), 0);
		int CBsum = accumulate(capacityB.begin(), capacityB.end(), 0);
		if( CAsum != CBsum )
			return vector<string>();

		graph G(2+NA+NB);
		memset(XXF, 0, sizeof XXF);
		for(int i=0; i<capacityA.size(); ++i) {
			G[0].push_back( 2+i );
			G[2+i].push_back( 0 );
			XXF[0][2+i] = capacityA[i];
		}
		for(int i=0; i<capacityB.size(); ++i) {
			G[2+NA+i].push_back( 1 );
			G[1].push_back( 2+NA+i );
			XXF[2+NA+i][1] = capacityB[i];
		}
		for(int i=0; i<capacityA.size(); ++i)
		for(int j=0; j<capacityB.size(); ++j) {
			G[2+i].push_back( 2+NA+j );
			G[2+NA+j].push_back( 2+i );
			XXF[2+i][2+NA+j] = 1;
		}

		memcpy(F, XXF, sizeof F);
		flow mf = maxFlow(G,F,0,1);
		if( mf != CAsum )
			return vector<string>();

		// find lex-best
		int rest = CAsum;
		vector<string> ans(NA, string(NB, '0'));
		for(int i=0; i<capacityA.size(); ++i)
		for(int j=0; j<capacityB.size(); ++j)
		{
			if( rest==0 )
				return ans;

			memcpy(F, XXF, sizeof F);
			bool toUse = true;

			F[2+i][2+NA+j] --;
			flow mf = maxFlow(G,F,0,1);
			toUse = (mf < rest);

			ans[i][j] = toUse ? '1' : '0';
			if( toUse ) {
				XXF[0][2+i] --;
				XXF[2+NA+j][1] --;
				rest--;
			}
			XXF[2+i][2+NA+j] --;
		}
		return ans;
	}
};

// 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> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
int verify_case(const vector <string> &Expected, const vector <string> &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } return 0;}

template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
	int capacityA_[] = {1,2,3};
	  vector <int> capacityA(capacityA_, capacityA_+sizeof(capacityA_)/sizeof(*capacityA_)); 
	int capacityU_[] = {3,1,2};
	  vector <int> capacityU(capacityU_, capacityU_+sizeof(capacityU_)/sizeof(*capacityU_)); 
	string RetVal_[] = {"100", "101", "111" };
	  vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 
	return verify_case(RetVal, ConnectingAirports().getSchedule(capacityA, capacityU)); }
int Test_(Case_<1>) {
	int capacityA_[] = {3,2,1,1};
	  vector <int> capacityA(capacityA_, capacityA_+sizeof(capacityA_)/sizeof(*capacityA_)); 
	int capacityU_[] = {1,3,1,2};
	  vector <int> capacityU(capacityU_, capacityU_+sizeof(capacityU_)/sizeof(*capacityU_)); 
	string RetVal_[] = {"0111", "0101", "0100", "1000" };
	  vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 
	return verify_case(RetVal, ConnectingAirports().getSchedule(capacityA, capacityU)); }
int Test_(Case_<2>) {
	int capacityA_[] = {1,2,3,4};
	  vector <int> capacityA(capacityA_, capacityA_+sizeof(capacityA_)/sizeof(*capacityA_)); 
	int capacityU_[] = {5,6,7,8};
	  vector <int> capacityU(capacityU_, capacityU_+sizeof(capacityU_)/sizeof(*capacityU_)); 
	vector <string> RetVal; 
	return verify_case(RetVal, ConnectingAirports().getSchedule(capacityA, capacityU)); }
int Test_(Case_<3>) {
	int capacityA_[] = {47,47};
	  vector <int> capacityA(capacityA_, capacityA_+sizeof(capacityA_)/sizeof(*capacityA_)); 
	int capacityU_[] = {47,40,7};
	  vector <int> capacityU(capacityU_, capacityU_+sizeof(capacityU_)/sizeof(*capacityU_)); 
	vector <string> RetVal; 
	return verify_case(RetVal, ConnectingAirports().getSchedule(capacityA, capacityU)); }
int Test_(Case_<4>) {
	int capacityA_[] = {5,5};
	  vector <int> capacityA(capacityA_, capacityA_+sizeof(capacityA_)/sizeof(*capacityA_)); 
	int capacityU_[] = {1,1,2,1,1,1,1,1,1};
	  vector <int> capacityU(capacityU_, capacityU_+sizeof(capacityU_)/sizeof(*capacityU_)); 
	string RetVal_[] = {"001001111", "111110000" };
	  vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 
	return verify_case(RetVal, ConnectingAirports().getSchedule(capacityA, capacityU)); }
int Test_(Case_<5>) {
	int capacityA_[] = {0,0,0,0};
	  vector <int> capacityA(capacityA_, capacityA_+sizeof(capacityA_)/sizeof(*capacityA_)); 
	int capacityU_[] = {0,0,0,0,0,0};
	  vector <int> capacityU(capacityU_, capacityU_+sizeof(capacityU_)/sizeof(*capacityU_)); 
	string RetVal_[] = {"000000", "000000", "000000", "000000" };
	  vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 
	return verify_case(RetVal, ConnectingAirports().getSchedule(capacityA, capacityU)); }
int Test_(Case_<6>) {
	int capacityA_[] = {50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,};
	  vector <int> capacityA(capacityA_, capacityA_+sizeof(capacityA_)/sizeof(*capacityA_)); 
	int capacityU_[] = {50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,};
	  vector <int> capacityU(capacityU_, capacityU_+sizeof(capacityU_)/sizeof(*capacityU_)); 
	string RetVal_[] = {"000000", "000000", "000000", "000000" };
	  vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 
	return verify_case(RetVal, ConnectingAirports().getSchedule(capacityA, capacityU)); }

template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<>      void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE