Artifact Content
Not logged in

Artifact 243d0d4a58986591c2c1a715d81b5db4be4270c3


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class YetAnotherHamiltonianPath { public:
	int leastCost(vector <string> label) 
	{
		int cost = 0;
		for(int i=0; i<label.size(); ++i)
			cost += label[i].size()*label[i].size() * (i<=1 ? 1 : 2); // どうやってもかかる分のコスト
		return cost - radixSort(label); // あとは max (Σ LCP^2) を引き算
	}

	int radixSort(const vector<string>& v, int i=0, bool single=true, bool hasBothZeroOne=true)
	{
		// 原則的に、0文字目が同じものどうしを一カ所にまとめた方が当然ベター
		// 0文字目が同じ中では1文字目が同じものどうしを一カ所に…という再帰
		map< char, vector<string> > classify;
		for(int k=0; k<v.size(); ++k)
			classify[v[k].c_str()[i]].push_back( v[k] );

		// N 個のブロックに分かれたら、分かれ目ではLCPが i なので、その分の Σ LCP^2 は i*i*(N-1)。
		int score = i*i * (classify.size()-1); 

		// あとはブロックごとに再帰的に LCP^2 の和を求めて足していく
		for(map<char, vector<string> >::iterator it=classify.begin(); it!=classify.end(); ++it)
		{
			bool sgl = single && classify.size()==1;
			bool hzo = hasBothZeroOne && it->first==v[0].c_str()[i] && it->first==v[1].c_str()[i];
			if( it->first )
				score += radixSort( it->second, i+1, sgl, hzo );
			else
				score += i*i * (it->second.size()-1 + (sgl?1:0) - (hzo?1:0));
		}

		// ただし、label[0] と label[1] が生き別れになる分かれ目は(最初から分かれてるので)数えない
		if( hasBothZeroOne && v[0].c_str()[i]!=v[1].c_str()[i] ) score -= i*i;

		// 逆に、全体が2つ以上のブロックに分かれるときには label[0] と label[1] の間に挟まるので ΣLCP^2 がその分多い
		if( single && classify.size()>1 ) score += i*i;

		// 以上
		return score;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, YetAnotherHamiltonianPath().leastCost(label));}
int main(){

CASE(0)
	string label_[] = {"home", "school", "pub"} ;
	  vector <string> label(label_, label_+sizeof(label_)/sizeof(*label_)); 
	int _ = 70; 
END
CASE(1)
	string label_[] = {"school", "home", "pub", "stadium"};
	  vector <string> label(label_, label_+sizeof(label_)/sizeof(*label_)); 
	int _ = 167; 
END
CASE(2)
	string label_[] = {"abcd","aecgh","abef","aecd"};
	  vector <string> label(label_, label_+sizeof(label_)/sizeof(*label_)); 
	int _ = 91; 
END
CASE(3)
	string label_[] = {"canada", "cyprus", "croatia", "colombia", "chile", "china", "cameroon"};
	  vector <string> label(label_, label_+sizeof(label_)/sizeof(*label_)); 
	int _ = 509; 
END
CASE(4)
	string label_[] = {"aaa", "aab", "b"};
	  vector <string> label(label_, label_+sizeof(label_)/sizeof(*label_)); 
	int _ = 9+1 + 1+9; 
END
CASE(4)
	string label_[] = {"caaa", "caab", "cb"};
	  vector <string> label(label_, label_+sizeof(label_)/sizeof(*label_)); 
	int _ = 16+4 + 4+16 - 1 - 1; 
END
/*
CASE(5)
	string label_[] = ;
	  vector <string> label(label_, label_+sizeof(label_)/sizeof(*label_)); 
	int _ = ; 
END
*/
}
// END CUT HERE