4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <functional> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: static const int NV = 128; 4fd800b3a8 2011-02-23 kinaba: typedef int flow; 4fd800b3a8 2011-02-23 kinaba: typedef int vert; 4fd800b3a8 2011-02-23 kinaba: typedef vert edge; 4fd800b3a8 2011-02-23 kinaba: typedef vector<edge> edges; 4fd800b3a8 2011-02-23 kinaba: typedef vector<edges> graph; 4fd800b3a8 2011-02-23 kinaba: typedef flow flow_graph[NV][NV]; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: flow dinic_dfs( graph& G, flow_graph F, vert v, vert D, 4fd800b3a8 2011-02-23 kinaba: int LV[], flow flow_in, int blocked[] ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: flow flow_out = 0; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i!=G[v].size(); ++i) { 4fd800b3a8 2011-02-23 kinaba: int u = G[v][i]; 4fd800b3a8 2011-02-23 kinaba: if( LV[v]+1==LV[u] && F[v][u] ) { 4fd800b3a8 2011-02-23 kinaba: flow f = min(flow_in-flow_out, F[v][u]); 4fd800b3a8 2011-02-23 kinaba: if( u==D || !blocked[u] && (f=dinic_dfs(G,F,u,D,LV,f,blocked)) ) { 4fd800b3a8 2011-02-23 kinaba: F[v][u] -= f; 4fd800b3a8 2011-02-23 kinaba: F[u][v] += f; 4fd800b3a8 2011-02-23 kinaba: flow_out += f; 4fd800b3a8 2011-02-23 kinaba: if( flow_in == flow_out ) return flow_out; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: blocked[v] = (flow_out==0); 4fd800b3a8 2011-02-23 kinaba: return flow_out; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: flow maxFlow( graph& G, flow_graph F, vert S, vert D ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: for( flow total=0 ;; ) { 4fd800b3a8 2011-02-23 kinaba: int LV[NV] = {0}; 4fd800b3a8 2011-02-23 kinaba: vector<int> Q(1, S); 4fd800b3a8 2011-02-23 kinaba: for(int lv=1; !Q.empty(); ++lv) { 4fd800b3a8 2011-02-23 kinaba: vector<int> Q2; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i!=Q.size(); ++i) { 4fd800b3a8 2011-02-23 kinaba: edges& ne = G[Q[i]]; 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j!=ne.size(); ++j) 4fd800b3a8 2011-02-23 kinaba: if( F[Q[i]][ne[j]] && !LV[ne[j]] && ne[j]!=S ) 4fd800b3a8 2011-02-23 kinaba: LV[ne[j]]=lv, Q2.push_back(ne[j]); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: Q.swap(Q2); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( !LV[D] ) 4fd800b3a8 2011-02-23 kinaba: return total; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int blocked[NV] = {}; 4fd800b3a8 2011-02-23 kinaba: total += dinic_dfs( G, F, S, D, LV, 0x7fffffff, blocked ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: flow_graph F, XXF; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class ConnectingAirports { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: vector <string> getSchedule(vector <int> capacityA, vector <int> capacityB) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int NA = capacityA.size(); 4fd800b3a8 2011-02-23 kinaba: int NB = capacityB.size(); 4fd800b3a8 2011-02-23 kinaba: int CAsum = accumulate(capacityA.begin(), capacityA.end(), 0); 4fd800b3a8 2011-02-23 kinaba: int CBsum = accumulate(capacityB.begin(), capacityB.end(), 0); 4fd800b3a8 2011-02-23 kinaba: if( CAsum != CBsum ) 4fd800b3a8 2011-02-23 kinaba: return vector<string>(); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: graph G(2+NA+NB); 4fd800b3a8 2011-02-23 kinaba: memset(XXF, 0, sizeof XXF); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<capacityA.size(); ++i) { 4fd800b3a8 2011-02-23 kinaba: G[0].push_back( 2+i ); 4fd800b3a8 2011-02-23 kinaba: G[2+i].push_back( 0 ); 4fd800b3a8 2011-02-23 kinaba: XXF[0][2+i] = capacityA[i]; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<capacityB.size(); ++i) { 4fd800b3a8 2011-02-23 kinaba: G[2+NA+i].push_back( 1 ); 4fd800b3a8 2011-02-23 kinaba: G[1].push_back( 2+NA+i ); 4fd800b3a8 2011-02-23 kinaba: XXF[2+NA+i][1] = capacityB[i]; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<capacityA.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<capacityB.size(); ++j) { 4fd800b3a8 2011-02-23 kinaba: G[2+i].push_back( 2+NA+j ); 4fd800b3a8 2011-02-23 kinaba: G[2+NA+j].push_back( 2+i ); 4fd800b3a8 2011-02-23 kinaba: XXF[2+i][2+NA+j] = 1; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: memcpy(F, XXF, sizeof F); 4fd800b3a8 2011-02-23 kinaba: flow mf = maxFlow(G,F,0,1); 4fd800b3a8 2011-02-23 kinaba: if( mf != CAsum ) 4fd800b3a8 2011-02-23 kinaba: return vector<string>(); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // find lex-best 4fd800b3a8 2011-02-23 kinaba: int rest = CAsum; 4fd800b3a8 2011-02-23 kinaba: vector<string> ans(NA, string(NB, '0')); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<capacityA.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<capacityB.size(); ++j) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( rest==0 ) 4fd800b3a8 2011-02-23 kinaba: return ans; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: memcpy(F, XXF, sizeof F); 4fd800b3a8 2011-02-23 kinaba: bool toUse = true; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: F[2+i][2+NA+j] --; 4fd800b3a8 2011-02-23 kinaba: flow mf = maxFlow(G,F,0,1); 4fd800b3a8 2011-02-23 kinaba: toUse = (mf < rest); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: ans[i][j] = toUse ? '1' : '0'; 4fd800b3a8 2011-02-23 kinaba: if( toUse ) { 4fd800b3a8 2011-02-23 kinaba: XXF[0][2+i] --; 4fd800b3a8 2011-02-23 kinaba: XXF[2+NA+j][1] --; 4fd800b3a8 2011-02-23 kinaba: rest--; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: XXF[2+i][2+NA+j] --; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return ans; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: 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(); } 4fd800b3a8 2011-02-23 kinaba: 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;} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> struct Case_ { Case_(){start_time=clock();} }; 4fd800b3a8 2011-02-23 kinaba: char Test_(...); 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<0>) { 4fd800b3a8 2011-02-23 kinaba: int capacityA_[] = {1,2,3}; 4fd800b3a8 2011-02-23 kinaba: vector <int> capacityA(capacityA_, capacityA_+sizeof(capacityA_)/sizeof(*capacityA_)); 4fd800b3a8 2011-02-23 kinaba: int capacityU_[] = {3,1,2}; 4fd800b3a8 2011-02-23 kinaba: vector <int> capacityU(capacityU_, capacityU_+sizeof(capacityU_)/sizeof(*capacityU_)); 4fd800b3a8 2011-02-23 kinaba: string RetVal_[] = {"100", "101", "111" }; 4fd800b3a8 2011-02-23 kinaba: vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ConnectingAirports().getSchedule(capacityA, capacityU)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: int capacityA_[] = {3,2,1,1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> capacityA(capacityA_, capacityA_+sizeof(capacityA_)/sizeof(*capacityA_)); 4fd800b3a8 2011-02-23 kinaba: int capacityU_[] = {1,3,1,2}; 4fd800b3a8 2011-02-23 kinaba: vector <int> capacityU(capacityU_, capacityU_+sizeof(capacityU_)/sizeof(*capacityU_)); 4fd800b3a8 2011-02-23 kinaba: string RetVal_[] = {"0111", "0101", "0100", "1000" }; 4fd800b3a8 2011-02-23 kinaba: vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ConnectingAirports().getSchedule(capacityA, capacityU)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: int capacityA_[] = {1,2,3,4}; 4fd800b3a8 2011-02-23 kinaba: vector <int> capacityA(capacityA_, capacityA_+sizeof(capacityA_)/sizeof(*capacityA_)); 4fd800b3a8 2011-02-23 kinaba: int capacityU_[] = {5,6,7,8}; 4fd800b3a8 2011-02-23 kinaba: vector <int> capacityU(capacityU_, capacityU_+sizeof(capacityU_)/sizeof(*capacityU_)); 4fd800b3a8 2011-02-23 kinaba: vector <string> RetVal; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ConnectingAirports().getSchedule(capacityA, capacityU)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: int capacityA_[] = {47,47}; 4fd800b3a8 2011-02-23 kinaba: vector <int> capacityA(capacityA_, capacityA_+sizeof(capacityA_)/sizeof(*capacityA_)); 4fd800b3a8 2011-02-23 kinaba: int capacityU_[] = {47,40,7}; 4fd800b3a8 2011-02-23 kinaba: vector <int> capacityU(capacityU_, capacityU_+sizeof(capacityU_)/sizeof(*capacityU_)); 4fd800b3a8 2011-02-23 kinaba: vector <string> RetVal; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ConnectingAirports().getSchedule(capacityA, capacityU)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: int capacityA_[] = {5,5}; 4fd800b3a8 2011-02-23 kinaba: vector <int> capacityA(capacityA_, capacityA_+sizeof(capacityA_)/sizeof(*capacityA_)); 4fd800b3a8 2011-02-23 kinaba: int capacityU_[] = {1,1,2,1,1,1,1,1,1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> capacityU(capacityU_, capacityU_+sizeof(capacityU_)/sizeof(*capacityU_)); 4fd800b3a8 2011-02-23 kinaba: string RetVal_[] = {"001001111", "111110000" }; 4fd800b3a8 2011-02-23 kinaba: vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ConnectingAirports().getSchedule(capacityA, capacityU)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<5>) { 4fd800b3a8 2011-02-23 kinaba: int capacityA_[] = {0,0,0,0}; 4fd800b3a8 2011-02-23 kinaba: vector <int> capacityA(capacityA_, capacityA_+sizeof(capacityA_)/sizeof(*capacityA_)); 4fd800b3a8 2011-02-23 kinaba: int capacityU_[] = {0,0,0,0,0,0}; 4fd800b3a8 2011-02-23 kinaba: vector <int> capacityU(capacityU_, capacityU_+sizeof(capacityU_)/sizeof(*capacityU_)); 4fd800b3a8 2011-02-23 kinaba: string RetVal_[] = {"000000", "000000", "000000", "000000" }; 4fd800b3a8 2011-02-23 kinaba: vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ConnectingAirports().getSchedule(capacityA, capacityU)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<6>) { 4fd800b3a8 2011-02-23 kinaba: 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,}; 4fd800b3a8 2011-02-23 kinaba: vector <int> capacityA(capacityA_, capacityA_+sizeof(capacityA_)/sizeof(*capacityA_)); 4fd800b3a8 2011-02-23 kinaba: 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,}; 4fd800b3a8 2011-02-23 kinaba: vector <int> capacityU(capacityU_, capacityU_+sizeof(capacityU_)/sizeof(*capacityU_)); 4fd800b3a8 2011-02-23 kinaba: string RetVal_[] = {"000000", "000000", "000000", "000000" }; 4fd800b3a8 2011-02-23 kinaba: vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, ConnectingAirports().getSchedule(capacityA, capacityU)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); } 4fd800b3a8 2011-02-23 kinaba: template<> void Run_<-1>() {} 4fd800b3a8 2011-02-23 kinaba: int main() { Run_<0>(); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: