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 YetAnotherHamiltonianPath { public: 4fd800b3a8 2011-02-23 kinaba: int leastCost(vector <string> label) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int cost = 0; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<label.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: cost += label[i].size()*label[i].size() * (i<=1 ? 1 : 2); // どうやってもかかる分のコスト 4fd800b3a8 2011-02-23 kinaba: return cost - radixSort(label); // あとは max (Σ LCP^2) を引き算 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int radixSort(const vector<string>& v, int i=0, bool single=true, bool hasBothZeroOne=true) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: // 原則的に、0文字目が同じものどうしを一カ所にまとめた方が当然ベター 4fd800b3a8 2011-02-23 kinaba: // 0文字目が同じ中では1文字目が同じものどうしを一カ所に…という再帰 4fd800b3a8 2011-02-23 kinaba: map< char, vector<string> > classify; 4fd800b3a8 2011-02-23 kinaba: for(int k=0; k<v.size(); ++k) 4fd800b3a8 2011-02-23 kinaba: classify[v[k].c_str()[i]].push_back( v[k] ); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // N 個のブロックに分かれたら、分かれ目ではLCPが i なので、その分の Σ LCP^2 は i*i*(N-1)。 4fd800b3a8 2011-02-23 kinaba: int score = i*i * (classify.size()-1); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // あとはブロックごとに再帰的に LCP^2 の和を求めて足していく 4fd800b3a8 2011-02-23 kinaba: for(map<char, vector<string> >::iterator it=classify.begin(); it!=classify.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: bool sgl = single && classify.size()==1; 4fd800b3a8 2011-02-23 kinaba: bool hzo = hasBothZeroOne && it->first==v[0].c_str()[i] && it->first==v[1].c_str()[i]; 4fd800b3a8 2011-02-23 kinaba: if( it->first ) 4fd800b3a8 2011-02-23 kinaba: score += radixSort( it->second, i+1, sgl, hzo ); 4fd800b3a8 2011-02-23 kinaba: else 4fd800b3a8 2011-02-23 kinaba: score += i*i * (it->second.size()-1 + (sgl?1:0) - (hzo?1:0)); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // ただし、label[0] と label[1] が生き別れになる分かれ目は(最初から分かれてるので)数えない 4fd800b3a8 2011-02-23 kinaba: if( hasBothZeroOne && v[0].c_str()[i]!=v[1].c_str()[i] ) score -= i*i; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // 逆に、全体が2つ以上のブロックに分かれるときには label[0] と label[1] の間に挟まるので ΣLCP^2 がその分多い 4fd800b3a8 2011-02-23 kinaba: if( single && classify.size()>1 ) score += i*i; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // 以上 4fd800b3a8 2011-02-23 kinaba: return score; 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(_, YetAnotherHamiltonianPath().leastCost(label));} 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 label_[] = {"home", "school", "pub"} ; 4fd800b3a8 2011-02-23 kinaba: vector <string> label(label_, label_+sizeof(label_)/sizeof(*label_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 70; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: string label_[] = {"school", "home", "pub", "stadium"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> label(label_, label_+sizeof(label_)/sizeof(*label_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 167; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: string label_[] = {"abcd","aecgh","abef","aecd"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> label(label_, label_+sizeof(label_)/sizeof(*label_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 91; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: string label_[] = {"canada", "cyprus", "croatia", "colombia", "chile", "china", "cameroon"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> label(label_, label_+sizeof(label_)/sizeof(*label_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 509; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: string label_[] = {"aaa", "aab", "b"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> label(label_, label_+sizeof(label_)/sizeof(*label_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 9+1 + 1+9; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: string label_[] = {"caaa", "caab", "cb"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> label(label_, label_+sizeof(label_)/sizeof(*label_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 16+4 + 4+16 - 1 - 1; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: /* 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: string label_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <string> label(label_, label_+sizeof(label_)/sizeof(*label_)); 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