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 = 256; 4fd800b3a8 2011-02-23 kinaba: typedef int flow; 4fd800b3a8 2011-02-23 kinaba: typedef int cost; 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: typedef cost cost_graph[NV][NV]; 4fd800b3a8 2011-02-23 kinaba: static const flow FLOW_INF = 0x7FFFFFFF; 4fd800b3a8 2011-02-23 kinaba: static const cost COST_INF = 0x7FFFFFFF; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // edges(bidi), capacity, cost(s.t. C[x][y]=-C[y][x]) per 1 flow, src, dst 4fd800b3a8 2011-02-23 kinaba: pair<cost,flow> mincostFlow(graph& G, flow_graph F, cost_graph C, vert S, vert D) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int N = G.size(); 4fd800b3a8 2011-02-23 kinaba: cost total_cost = 0; 4fd800b3a8 2011-02-23 kinaba: flow total_flow = 0; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<cost> h(N, 0); // potential 4fd800b3a8 2011-02-23 kinaba: for(cost RF=FLOW_INF; RF>0; ) // residual flow 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: // Dijkstra -- find the min-cost path 4fd800b3a8 2011-02-23 kinaba: vector<cost> d(N, COST_INF); d[S] = 0; 4fd800b3a8 2011-02-23 kinaba: vector<vert> prev(N, -1); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: typedef pair<cost, pair<vert,vert> > cedge; 4fd800b3a8 2011-02-23 kinaba: priority_queue< cedge, vector<cedge>, greater<cedge> > Q; 4fd800b3a8 2011-02-23 kinaba: Q.push( cedge(0, make_pair(S,S)) ); 4fd800b3a8 2011-02-23 kinaba: while( !Q.empty() ) { 4fd800b3a8 2011-02-23 kinaba: cedge e = Q.top(); Q.pop(); 4fd800b3a8 2011-02-23 kinaba: if( prev[e.second.second] >= 0 ) 4fd800b3a8 2011-02-23 kinaba: continue; 4fd800b3a8 2011-02-23 kinaba: prev[e.second.second] = e.second.first; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vert u = e.second.second; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<G[u].size(); ++i) { 4fd800b3a8 2011-02-23 kinaba: vert v = G[u][i]; 4fd800b3a8 2011-02-23 kinaba: cost r_cost = C[u][v] + h[u] - h[v]; 4fd800b3a8 2011-02-23 kinaba: if( F[u][v] > 0 && d[v] > d[u]+r_cost ) 4fd800b3a8 2011-02-23 kinaba: Q.push( cedge(d[v]=d[u]+r_cost, make_pair(u,v)) ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( prev[D] < 0 ) 4fd800b3a8 2011-02-23 kinaba: break; // Finished 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // As much as possible 4fd800b3a8 2011-02-23 kinaba: flow f = RF; 4fd800b3a8 2011-02-23 kinaba: for(vert u=D; u!=S; u=prev[u]) 4fd800b3a8 2011-02-23 kinaba: f = min(f, F[prev[u]][u]); 4fd800b3a8 2011-02-23 kinaba: RF -= f; 4fd800b3a8 2011-02-23 kinaba: total_flow += f; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(vert u=D; u!=S; u=prev[u]) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: total_cost += f * C[prev[u]][u]; 4fd800b3a8 2011-02-23 kinaba: F[prev[u]][u] -= f; 4fd800b3a8 2011-02-23 kinaba: F[u][prev[u]] += f; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // Update the potential 4fd800b3a8 2011-02-23 kinaba: for(vert u=0; u<N; ++u) 4fd800b3a8 2011-02-23 kinaba: h[u] += d[u]; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return make_pair(total_cost, total_flow); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class MarblesRegroupingHard { public: 4fd800b3a8 2011-02-23 kinaba: int minMoves(vector <string> boxes) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: const int NB = boxes.size(); 4fd800b3a8 2011-02-23 kinaba: vector< vector<int> > bb(NB); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<boxes.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: stringstream sin(boxes[i]); 4fd800b3a8 2011-02-23 kinaba: for(int v; sin>>v; ) 4fd800b3a8 2011-02-23 kinaba: bb[i].push_back(v); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: const int NC = bb[0].size(); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: graph G(NB+NC+3); 4fd800b3a8 2011-02-23 kinaba: flow_graph F = {}; 4fd800b3a8 2011-02-23 kinaba: cost_graph C = {}; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<NB; ++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: F[0][2+i] = 1; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<NC; ++i) { 4fd800b3a8 2011-02-23 kinaba: G[2+NB+i].push_back(1); 4fd800b3a8 2011-02-23 kinaba: G[1].push_back(2+NB+i); 4fd800b3a8 2011-02-23 kinaba: F[2+NB+i][1] = 1; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: G[2+NB+NC].push_back(1); 4fd800b3a8 2011-02-23 kinaba: G[1].push_back(2+NB+NC); 4fd800b3a8 2011-02-23 kinaba: F[2+NB+NC][1] = NB-NC; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<NB; ++i) 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<NC; ++j) { 4fd800b3a8 2011-02-23 kinaba: G[2+i].push_back(2+NB+j); 4fd800b3a8 2011-02-23 kinaba: G[2+NB+j].push_back(2+i); 4fd800b3a8 2011-02-23 kinaba: F[2+i][2+NB+j] = 1; 4fd800b3a8 2011-02-23 kinaba: int c = 0; 4fd800b3a8 2011-02-23 kinaba: for(int k=0; k<NB; ++k) if(k!=i) 4fd800b3a8 2011-02-23 kinaba: c += bb[k][j]; 4fd800b3a8 2011-02-23 kinaba: C[2+i][2+NB+j] = c; 4fd800b3a8 2011-02-23 kinaba: C[2+NB+j][2+i] = -c; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<NB; ++i) { 4fd800b3a8 2011-02-23 kinaba: G[2+i].push_back(2+NB+NC); 4fd800b3a8 2011-02-23 kinaba: G[2+NB+NC].push_back(2+i); 4fd800b3a8 2011-02-23 kinaba: F[2+i][2+NB+NC] = 1; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: pair<cost,flow> ff = mincostFlow(G, F, C, 0, 1); 4fd800b3a8 2011-02-23 kinaba: return ff.first; 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() 4fd800b3a8 2011-02-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4fd800b3a8 2011-02-23 kinaba: { os << "{ "; 4fd800b3a8 2011-02-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4fd800b3a8 2011-02-23 kinaba: void verify_case(const int& Expected, const int& Received) { 4fd800b3a8 2011-02-23 kinaba: bool ok = (Expected == Received); 4fd800b3a8 2011-02-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4fd800b3a8 2011-02-23 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4fd800b3a8 2011-02-23 kinaba: #define END verify_case(_, MarblesRegroupingHard().minMoves(boxes));} 4fd800b3a8 2011-02-23 kinaba: int main(){ 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CASE(0) 4fd800b3a8 2011-02-23 kinaba: string boxes_[] = {"0"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> boxes(boxes_, boxes_+sizeof(boxes_)/sizeof(*boxes_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 0; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: string boxes_[] = {"77 97","8 0"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> boxes(boxes_, boxes_+sizeof(boxes_)/sizeof(*boxes_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 77; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: string boxes_[] = {"6 97 7","73 45 0","67 45 63"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> boxes(boxes_, boxes_+sizeof(boxes_)/sizeof(*boxes_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 170; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: string boxes_[] = {"97 94 0 99","1 72 46 45","0 10 47 75","0 92 76 20","2 25 98 22"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> boxes(boxes_, boxes_+sizeof(boxes_)/sizeof(*boxes_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 559; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: string boxes_[] = {"38 0 0 45 77 61 0 0 8 87 65 53 0 2", 4fd800b3a8 2011-02-23 kinaba: "0 97 79 37 70 0 69 35 95 11 75 96 77 29", 4fd800b3a8 2011-02-23 kinaba: "39 32 0 24 63 22 91 71 0 63 92 59 12 0", 4fd800b3a8 2011-02-23 kinaba: "34 0 0 71 88 13 60 56 0 22 29 89 81 53", 4fd800b3a8 2011-02-23 kinaba: "69 95 65 0 94 98 84 37 0 87 16 0 58 18", 4fd800b3a8 2011-02-23 kinaba: "82 0 36 88 0 54 82 40 6 0 34 44 80 2", 4fd800b3a8 2011-02-23 kinaba: "33 58 7 95 83 87 0 0 79 35 0 51 22 84", 4fd800b3a8 2011-02-23 kinaba: "7 0 30 57 33 4 0 26 44 55 67 31 88 42", 4fd800b3a8 2011-02-23 kinaba: "62 58 62 93 91 0 1 0 44 23 95 0 55 57", 4fd800b3a8 2011-02-23 kinaba: "35 8 22 26 76 0 78 54 0 46 42 0 0 64", 4fd800b3a8 2011-02-23 kinaba: "93 54 58 0 89 89 0 0 57 0 98 3 24 0", 4fd800b3a8 2011-02-23 kinaba: "9 0 0 23 82 18 0 46 0 0 94 84 19 18", 4fd800b3a8 2011-02-23 kinaba: "78 12 6 45 0 80 16 69 59 76 35 0 66 0", 4fd800b3a8 2011-02-23 kinaba: "0 68 77 13 15 0 52 72 0 0 18 65 23 0", 4fd800b3a8 2011-02-23 kinaba: "0 0 73 53 0 95 91 44 27 96 85 0 99 85", 4fd800b3a8 2011-02-23 kinaba: "93 29 4 89 27 44 27 17 21 6 0 8 3 91", 4fd800b3a8 2011-02-23 kinaba: "0 46 73 94 60 59 15 50 18 75 74 88 46 93", 4fd800b3a8 2011-02-23 kinaba: "0 0 0 94 93 26 21 83 54 62 0 89 59 77", 4fd800b3a8 2011-02-23 kinaba: "32 98 0 44 34 38 56 20 0 61 29 94 52 57", 4fd800b3a8 2011-02-23 kinaba: "52 60 0 22 0 5 38 0 50 98 12 75 0 81", 4fd800b3a8 2011-02-23 kinaba: "23 0 41 18 36 87 49 32 62 43 22 74 0 97", 4fd800b3a8 2011-02-23 kinaba: "0 0 30 79 16 73 61 0 75 51 64 13 45 0", 4fd800b3a8 2011-02-23 kinaba: "11 0 14 2 0 1 2 16 84 37 95 45 48 33", 4fd800b3a8 2011-02-23 kinaba: "10 0 0 35 0 85 28 76 99 74 76 32 52 8", 4fd800b3a8 2011-02-23 kinaba: "60 0 96 0 95 26 59 13 0 0 44 42 97 48", 4fd800b3a8 2011-02-23 kinaba: "34 7 77 25 91 85 35 78 32 99 7 29 18 15", 4fd800b3a8 2011-02-23 kinaba: "61 50 43 22 42 63 64 50 9 94 42 22 21 33", 4fd800b3a8 2011-02-23 kinaba: "58 0 41 10 16 0 27 67 83 27 14 37 98 47", 4fd800b3a8 2011-02-23 kinaba: "37 60 60 76 71 2 84 32 27 39 82 84 0 94", 4fd800b3a8 2011-02-23 kinaba: "15 98 69 82 36 66 0 97 62 39 0 65 62 67", 4fd800b3a8 2011-02-23 kinaba: "0 41 0 43 0 0 94 0 0 58 0 0 27 33", 4fd800b3a8 2011-02-23 kinaba: "53 90 71 91 85 0 32 86 40 60 11 0 99 28", 4fd800b3a8 2011-02-23 kinaba: "79 62 0 0 79 0 14 62 87 76 35 0 70 0", 4fd800b3a8 2011-02-23 kinaba: "0 40 73 48 0 63 0 0 63 5 30 18 47 51", 4fd800b3a8 2011-02-23 kinaba: "75 6 58 69 33 57 66 0 12 0 46 0 65 10"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> boxes(boxes_, boxes_+sizeof(boxes_)/sizeof(*boxes_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 18618; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: /* 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: string boxes_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <string> boxes(boxes_, boxes_+sizeof(boxes_)/sizeof(*boxes_)); 4fd800b3a8 2011-02-23 kinaba: int _ = ; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(6) 4fd800b3a8 2011-02-23 kinaba: string boxes_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <string> boxes(boxes_, boxes_+sizeof(boxes_)/sizeof(*boxes_)); 4fd800b3a8 2011-02-23 kinaba: int _ = ; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: */ 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE