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 <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: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: typedef int cost; 4fd800b3a8 2011-02-23 kinaba: typedef int vert; 4fd800b3a8 2011-02-23 kinaba: typedef vector< vector<cost> > graph; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: cost bidi_minCut( graph G, int N) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<vert> V; 4fd800b3a8 2011-02-23 kinaba: for(int v=0; v<N; ++v) 4fd800b3a8 2011-02-23 kinaba: V.push_back(v); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: cost result = 0x7fffffff; 4fd800b3a8 2011-02-23 kinaba: for(int n=N; n>1; --n) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: // invariant: 4fd800b3a8 2011-02-23 kinaba: // the current set of vertices = {V[0] .. V[n-1]} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // order the vertices 4fd800b3a8 2011-02-23 kinaba: vector<int> vs; 4fd800b3a8 2011-02-23 kinaba: cost lastCut = 0; 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<cost> ws(n, 0); 4fd800b3a8 2011-02-23 kinaba: ws[0] = 1; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<n; ++i) { 4fd800b3a8 2011-02-23 kinaba: int j = max_element(ws.begin(), ws.end()) - ws.begin(); 4fd800b3a8 2011-02-23 kinaba: lastCut = ws[j]; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vs.push_back(j); 4fd800b3a8 2011-02-23 kinaba: ws[j] = -1; 4fd800b3a8 2011-02-23 kinaba: for(int k=0; k<n; ++k) 4fd800b3a8 2011-02-23 kinaba: if( ws[k] != -1 ) 4fd800b3a8 2011-02-23 kinaba: ws[k] += G[V[k]][V[j]]; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // update mincut 4fd800b3a8 2011-02-23 kinaba: result = min(result, lastCut); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // reduce the graph (unify vs[n-2] and vs[n-1]) 4fd800b3a8 2011-02-23 kinaba: vert v2=vs[n-2], v1=vs[n-1]; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<n; ++i) { 4fd800b3a8 2011-02-23 kinaba: G[V[v2]][V[i]] += G[V[v1]][V[i]]; 4fd800b3a8 2011-02-23 kinaba: G[V[i]][V[v2]] += G[V[i]][V[v1]]; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: V.erase( V.begin() + v1 ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return result; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class Terrorists 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: int requiredCost(vector <string> roads) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int N = roads.size(); 4fd800b3a8 2011-02-23 kinaba: graph G(N, vector<cost>(N)); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<N; ++i) 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<N; ++j) 4fd800b3a8 2011-02-23 kinaba: G[i][j] = roads[i][j]-'0'; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: return bidi_minCut(G, N); 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 int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << 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: string roads_[] = {"0911", 4fd800b3a8 2011-02-23 kinaba: "9011", 4fd800b3a8 2011-02-23 kinaba: "1109", 4fd800b3a8 2011-02-23 kinaba: "1190"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 4; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, Terrorists().requiredCost(roads)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: string roads_[] = {"0399", 4fd800b3a8 2011-02-23 kinaba: "3033", 4fd800b3a8 2011-02-23 kinaba: "9309", 4fd800b3a8 2011-02-23 kinaba: "9390"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 9; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, Terrorists().requiredCost(roads)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: string roads_[] = {"030900", 4fd800b3a8 2011-02-23 kinaba: "304120", 4fd800b3a8 2011-02-23 kinaba: "040174", 4fd800b3a8 2011-02-23 kinaba: "911021", 4fd800b3a8 2011-02-23 kinaba: "027207", 4fd800b3a8 2011-02-23 kinaba: "004170"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 8; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, Terrorists().requiredCost(roads)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: string roads_[] = {"044967263", 4fd800b3a8 2011-02-23 kinaba: "409134231", 4fd800b3a8 2011-02-23 kinaba: "490642938", 4fd800b3a8 2011-02-23 kinaba: "916036261", 4fd800b3a8 2011-02-23 kinaba: "634306024", 4fd800b3a8 2011-02-23 kinaba: "742660550", 4fd800b3a8 2011-02-23 kinaba: "229205069", 4fd800b3a8 2011-02-23 kinaba: "633625604", 4fd800b3a8 2011-02-23 kinaba: "318140940"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 27; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, Terrorists().requiredCost(roads)); } 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: