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: class VacationTours { public: 4fd800b3a8 2011-02-23 kinaba: int getIncome(vector <string> c, vector <string> d, int fee) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int N = c.size(); 4fd800b3a8 2011-02-23 kinaba: vector< vector<int> > G(N, vector<int>(N)); 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] = dec(c[i][j])*64 + dec(d[i][j]); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int basefee = fee * (N-1); 4fd800b3a8 2011-02-23 kinaba: for(int i=1; i<N; ++i) 4fd800b3a8 2011-02-23 kinaba: basefee -= G[0][i] + G[i][0]; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: typedef pair< int, pair<int,int> > edge; 4fd800b3a8 2011-02-23 kinaba: set< edge, greater<edge> > es; 4fd800b3a8 2011-02-23 kinaba: vector< vector<int> > P(N, vector<int>(N)); 4fd800b3a8 2011-02-23 kinaba: for(int i=1; i<N; ++i) 4fd800b3a8 2011-02-23 kinaba: for(int j=1; j<N; ++j) if(i!=j) { 4fd800b3a8 2011-02-23 kinaba: P[i][j] = -fee - G[i][j] + G[i][0] + G[0][j]; 4fd800b3a8 2011-02-23 kinaba: es.insert( edge(P[i][j], make_pair(i,j)) ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int gain = 0; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<bool> out(N,true), in(N,true); 4fd800b3a8 2011-02-23 kinaba: vector<int> uf(N), sz(N); 4fd800b3a8 2011-02-23 kinaba: for(int v=0; v<N; ++v) uf[v]=v, sz[v]=1; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(set< edge, greater<edge> >::iterator it=es.begin(); it!=es.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int c = it->first; 4fd800b3a8 2011-02-23 kinaba: int a = it->second.first; 4fd800b3a8 2011-02-23 kinaba: int b = it->second.second; 4fd800b3a8 2011-02-23 kinaba: if( c <= 0 ) 4fd800b3a8 2011-02-23 kinaba: break; 4fd800b3a8 2011-02-23 kinaba: if( !out[a] || !in[b] ) 4fd800b3a8 2011-02-23 kinaba: continue; 4fd800b3a8 2011-02-23 kinaba: // repr 4fd800b3a8 2011-02-23 kinaba: int ra=a; while(uf[ra]!=ra) ra=uf[ra]; 4fd800b3a8 2011-02-23 kinaba: int rb=b; while(uf[rb]!=rb) rb=uf[rb]; 4fd800b3a8 2011-02-23 kinaba: if( ra != rb ) { 4fd800b3a8 2011-02-23 kinaba: gain += c; 4fd800b3a8 2011-02-23 kinaba: // union 4fd800b3a8 2011-02-23 kinaba: if( sz[ra] > sz[rb] ) swap(ra,rb); 4fd800b3a8 2011-02-23 kinaba: uf[ra] = rb; 4fd800b3a8 2011-02-23 kinaba: sz[rb] += sz[ra]; 4fd800b3a8 2011-02-23 kinaba: out[a] = false; 4fd800b3a8 2011-02-23 kinaba: in[b] = false; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: return max(0, basefee+gain); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int dec(char c) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if('A'<=c && c<='Z') return c-'A'; 4fd800b3a8 2011-02-23 kinaba: if('a'<=c && c<='z') return c-'a'+26; 4fd800b3a8 2011-02-23 kinaba: if('0'<=c && c<='9') return c-'0'+52; 4fd800b3a8 2011-02-23 kinaba: if(c=='+')return 62; 4fd800b3a8 2011-02-23 kinaba: return 63; 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(_, VacationTours().getIncome(c, d, fee));} 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 c_[] = {"AAA", 4fd800b3a8 2011-02-23 kinaba: "AAA", 4fd800b3a8 2011-02-23 kinaba: "AAA"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> c(c_, c_+sizeof(c_)/sizeof(*c_)); 4fd800b3a8 2011-02-23 kinaba: string d_[] = {"ABJ", 4fd800b3a8 2011-02-23 kinaba: "JAB", 4fd800b3a8 2011-02-23 kinaba: "BJA"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> d(d_, d_+sizeof(d_)/sizeof(*d_)); 4fd800b3a8 2011-02-23 kinaba: int fee = 15; 4fd800b3a8 2011-02-23 kinaba: int _ = 12; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: string c_[] = {"AAAA", 4fd800b3a8 2011-02-23 kinaba: "AAAA", 4fd800b3a8 2011-02-23 kinaba: "AAAA", 4fd800b3a8 2011-02-23 kinaba: "AAAA"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> c(c_, c_+sizeof(c_)/sizeof(*c_)); 4fd800b3a8 2011-02-23 kinaba: string d_[] = {"AAAA", 4fd800b3a8 2011-02-23 kinaba: "AAAA", 4fd800b3a8 2011-02-23 kinaba: "AAAA", 4fd800b3a8 2011-02-23 kinaba: "AAAA"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> d(d_, d_+sizeof(d_)/sizeof(*d_)); 4fd800b3a8 2011-02-23 kinaba: int fee = 100; 4fd800b3a8 2011-02-23 kinaba: int _ = 300; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: string c_[] = {"A//", 4fd800b3a8 2011-02-23 kinaba: "/A/", 4fd800b3a8 2011-02-23 kinaba: "//A"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> c(c_, c_+sizeof(c_)/sizeof(*c_)); 4fd800b3a8 2011-02-23 kinaba: string d_[] = {"A//", 4fd800b3a8 2011-02-23 kinaba: "/A/", 4fd800b3a8 2011-02-23 kinaba: "//A"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> d(d_, d_+sizeof(d_)/sizeof(*d_)); 4fd800b3a8 2011-02-23 kinaba: int fee = 1000; 4fd800b3a8 2011-02-23 kinaba: int _ = 0; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: string c_[] = {"AAA////", 4fd800b3a8 2011-02-23 kinaba: "/AA/A//", 4fd800b3a8 2011-02-23 kinaba: "//AA/A/", 4fd800b3a8 2011-02-23 kinaba: "A//AA//", 4fd800b3a8 2011-02-23 kinaba: "///AAA/", 4fd800b3a8 2011-02-23 kinaba: "///A/AA", 4fd800b3a8 2011-02-23 kinaba: "AA////A"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> c(c_, c_+sizeof(c_)/sizeof(*c_)); 4fd800b3a8 2011-02-23 kinaba: string d_[] = {"AKo////", 4fd800b3a8 2011-02-23 kinaba: "/AU/X//", 4fd800b3a8 2011-02-23 kinaba: "//AZ/o/", 4fd800b3a8 2011-02-23 kinaba: "j//AK//", 4fd800b3a8 2011-02-23 kinaba: "///XAo/", 4fd800b3a8 2011-02-23 kinaba: "///y/AK", 4fd800b3a8 2011-02-23 kinaba: "KP////A"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> d(d_, d_+sizeof(d_)/sizeof(*d_)); 4fd800b3a8 2011-02-23 kinaba: int fee = 1000; 4fd800b3a8 2011-02-23 kinaba: int _ = 1809; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: /* 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: string c_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <string> c(c_, c_+sizeof(c_)/sizeof(*c_)); 4fd800b3a8 2011-02-23 kinaba: string d_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <string> d(d_, d_+sizeof(d_)/sizeof(*d_)); 4fd800b3a8 2011-02-23 kinaba: int fee = ; 4fd800b3a8 2011-02-23 kinaba: int _ = ; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: string c_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <string> c(c_, c_+sizeof(c_)/sizeof(*c_)); 4fd800b3a8 2011-02-23 kinaba: string d_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <string> d(d_, d_+sizeof(d_)/sizeof(*d_)); 4fd800b3a8 2011-02-23 kinaba: int fee = ; 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