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); // �ǂ�����Ă������镪�̃R�X�g
return cost - radixSort(label); // ���Ƃ� max (�� LCP^2) �������Z
}
int radixSort(const vector<string>& v, int i=0, bool single=true, bool hasBothZeroOne=true)
{
// �����I�ɁA0�����ڂ��������̂ǂ�������J���ɂ܂Ƃ߂��������R�x�^�[
// 0�����ڂ��������ł�1�����ڂ��������̂ǂ�������J���Ɂc�Ƃ����ċA
map< char, vector<string> > classify;
for(int k=0; k<v.size(); ++k)
classify[v[k].c_str()[i]].push_back( v[k] );
// N �̃u���b�N�ɕ����ꂽ��A������ڂł�LCP�� i �Ȃ̂ŁA���̕��� �� LCP^2 �� i*i*(N-1)�B
int score = i*i * (classify.size()-1);
// ���Ƃ̓u���b�N���ƂɍċA�I�� LCP^2 �̘a�����߂đ����Ă���
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));
}
// �������Alabel[0] �� label[1] �������ʂ�ɂȂ镪����ڂ́i�ŏ����番����Ă�̂Łj�����Ȃ�
if( hasBothZeroOne && v[0].c_str()[i]!=v[1].c_str()[i] ) score -= i*i;
// �t�ɁA�S�̂�2�ȏ�̃u���b�N�ɕ������Ƃ��ɂ� 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