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: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL next_combination(LL p) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL lsb = p & -p; 4fd800b3a8 2011-02-23 kinaba: LL rem = p + lsb; 4fd800b3a8 2011-02-23 kinaba: LL rit = rem & ~p; 4fd800b3a8 2011-02-23 kinaba: return rem|(((rit/lsb)>>1)-1); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class ByteLand 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: int buildCastles(vector<int> road, vector<int> distance, vector<int> castle, int k) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int n = road.size(); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // Construct the Graph 4fd800b3a8 2011-02-23 kinaba: int d[50][50] = {}; 4fd800b3a8 2011-02-23 kinaba: for(int v=0; v<n; ++v) 4fd800b3a8 2011-02-23 kinaba: for(int u=v+1; u<n; ++u) 4fd800b3a8 2011-02-23 kinaba: d[v][u] = d[u][v] = 0x3fffffff; 4fd800b3a8 2011-02-23 kinaba: for(int v=0; v<n; ++v) 4fd800b3a8 2011-02-23 kinaba: d[v][road[v]] = d[road[v]][v] = min( d[v][road[v]], distance[v] ); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // Warshall-Floyd 4fd800b3a8 2011-02-23 kinaba: for(int m=0; m<n; ++m) 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: d[i][j] = min(d[i][j], d[i][m]+d[m][j]); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // Binary Search: the answer is in (L,R] 4fd800b3a8 2011-02-23 kinaba: int L=-1, R=accumulate(distance.begin(), distance.end(), 0); 4fd800b3a8 2011-02-23 kinaba: while( R-L > 1 ) 4fd800b3a8 2011-02-23 kinaba: (is_possible((L+R)/2, d, n, castle, k) ? R : L) = (L+R)/2; 4fd800b3a8 2011-02-23 kinaba: return R; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: bool is_possible( int D, int d[50][50], int n, const vector<int>& c, int k ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: // the set of cities not covered yet 4fd800b3a8 2011-02-23 kinaba: LL goal = (1LL << n) - 1; 4fd800b3a8 2011-02-23 kinaba: for(int v=0; v<n; ++v) 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<c.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: if( d[v][c[i]] <= D ) 4fd800b3a8 2011-02-23 kinaba: goal &= ~(1LL << v); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // the set of cities each city can cover 4fd800b3a8 2011-02-23 kinaba: set<LL> mask; 4fd800b3a8 2011-02-23 kinaba: for(int v=0; v<n; ++v) { 4fd800b3a8 2011-02-23 kinaba: LL m = 0; 4fd800b3a8 2011-02-23 kinaba: for(int u=0; u<n; ++u) 4fd800b3a8 2011-02-23 kinaba: if( d[v][u] <= D ) 4fd800b3a8 2011-02-23 kinaba: m |= (1LL << u); 4fd800b3a8 2011-02-23 kinaba: if( m&goal ) 4fd800b3a8 2011-02-23 kinaba: mask.insert( m&goal ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // is it possible to cover the goal by at most k elements from mask? 4fd800b3a8 2011-02-23 kinaba: return canCover(goal, mask, k); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: bool canCover( LL goal, set<LL>& mask, int k ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: // optimizer 4fd800b3a8 2011-02-23 kinaba: for(bool update=true; update; ) { 4fd800b3a8 2011-02-23 kinaba: update = false; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // if *it \subseteq *jt for some jt, then we NEVER use it 4fd800b3a8 2011-02-23 kinaba: // if *it is the only mask covering some city, we ALWAYS use it 4fd800b3a8 2011-02-23 kinaba: for(set<LL>::iterator it=mask.begin(); it!=mask.end(); ) { 4fd800b3a8 2011-02-23 kinaba: bool isSubset = false; 4fd800b3a8 2011-02-23 kinaba: LL onlyByMe = *it & goal; 4fd800b3a8 2011-02-23 kinaba: for(set<LL>::iterator jt=mask.begin(); jt!=mask.end(); ++jt) 4fd800b3a8 2011-02-23 kinaba: if( it!=jt ) { 4fd800b3a8 2011-02-23 kinaba: onlyByMe &= ~*jt; 4fd800b3a8 2011-02-23 kinaba: isSubset |= (*it & *jt & goal) == (*it & goal); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: update |= isSubset | !!onlyByMe; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( isSubset ) 4fd800b3a8 2011-02-23 kinaba: mask.erase(it++); 4fd800b3a8 2011-02-23 kinaba: else if( onlyByMe ) { 4fd800b3a8 2011-02-23 kinaba: if( --k < 0 ) 4fd800b3a8 2011-02-23 kinaba: return false; 4fd800b3a8 2011-02-23 kinaba: goal &= ~*it; 4fd800b3a8 2011-02-23 kinaba: mask.erase(it++); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: else 4fd800b3a8 2011-02-23 kinaba: ++it; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( mask.size()<=k || goal==0 ) 4fd800b3a8 2011-02-23 kinaba: return true; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // exhaustive search 4fd800b3a8 2011-02-23 kinaba: vector<LL> ms(mask.begin(), mask.end()); 4fd800b3a8 2011-02-23 kinaba: for(LL i=(1LL<<k)-1; i<(1LL<<ms.size()); i=next_combination(i)) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL gg = goal; 4fd800b3a8 2011-02-23 kinaba: for(LL j=0; (1LL<<j)<=i; ++j) 4fd800b3a8 2011-02-23 kinaba: if( i & (1<<j) ) 4fd800b3a8 2011-02-23 kinaba: gg &= ~ms[j]; 4fd800b3a8 2011-02-23 kinaba: if( gg == 0 ) 4fd800b3a8 2011-02-23 kinaba: return true; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return false; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); } 4fd800b3a8 2011-02-23 kinaba: private: 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: void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: void test_case_0() { int Arr0[] = {1,2,3,4,0}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1,1,1,1,1}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); vector <int> Arg2; int Arg3 = 1; int Arg4 = 2; verify_case(0, Arg4, buildCastles(Arg0, Arg1, Arg2, Arg3)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_1() { int Arr0[] = {1,2,0}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1,2,3}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {2}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 1; int Arg4 = 1; verify_case(1, Arg4, buildCastles(Arg0, Arg1, Arg2, Arg3)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_2() { int Arr0[] = {0,1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1,1}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {1}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 1; int Arg4 = 0; verify_case(2, Arg4, buildCastles(Arg0, Arg1, Arg2, Arg3)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_3() { int Arr0[] = {0,2,0,0,2,2,8,3,8,7}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {10,9,1,8,1,3,7,2,8,1}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {3,4,6}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 3; int Arg4 = 3; verify_case(3, Arg4, buildCastles(Arg0, Arg1, Arg2, Arg3)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_4() { int Arr0[] = {1, 0}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {5, 10}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); vector <int> Arg2; int Arg3 = 1; int Arg4 = 5; verify_case(4, Arg4, buildCastles(Arg0, Arg1, Arg2, Arg3)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: int main() { ByteLand().run_test(-1); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE