Artifact Content
Not logged in

Artifact a16249ed416e3f883181dd9497a807dc2b6b7b68


#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 VacationTours { public:
	int getIncome(vector <string> c, vector <string> d, int fee) 
	{
		int N = c.size();
		vector< vector<int> > G(N, vector<int>(N));
		for(int i=0; i<N; ++i)
			for(int j=0; j<N; ++j)
				G[i][j] = dec(c[i][j])*64 + dec(d[i][j]);

		int basefee = fee * (N-1);
		for(int i=1; i<N; ++i)
			basefee -= G[0][i] + G[i][0];

		typedef pair< int, pair<int,int> > edge;
		set< edge, greater<edge> > es;
		vector< vector<int> > P(N, vector<int>(N));
		for(int i=1; i<N; ++i)
			for(int j=1; j<N; ++j) if(i!=j) {
				P[i][j] = -fee - G[i][j] + G[i][0] + G[0][j];
				es.insert( edge(P[i][j], make_pair(i,j)) );
			}

		int gain = 0;

		vector<bool> out(N,true), in(N,true);
		vector<int> uf(N), sz(N);
		for(int v=0; v<N; ++v) uf[v]=v, sz[v]=1;

		for(set< edge, greater<edge> >::iterator it=es.begin(); it!=es.end(); ++it)
		{
			int c = it->first;
			int a = it->second.first;
			int b = it->second.second;
			if( c <= 0 )
				break;
			if( !out[a] || !in[b] )
				continue;
			// repr
			int ra=a; while(uf[ra]!=ra) ra=uf[ra];
			int rb=b; while(uf[rb]!=rb) rb=uf[rb];
			if( ra != rb ) {
				gain += c;
				// union
				if( sz[ra] > sz[rb] ) swap(ra,rb);
				uf[ra] = rb;
				sz[rb] += sz[ra];
				out[a] = false;
				in[b] = false;
			}
		}

		return max(0, basefee+gain);
	}

	int dec(char c)
	{
		if('A'<=c && c<='Z') return c-'A';
		if('a'<=c && c<='z') return c-'a'+26;
		if('0'<=c && c<='9') return c-'0'+52;
		if(c=='+')return 62;
		return 63;
	}
};

// 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(_, VacationTours().getIncome(c, d, fee));}
int main(){

CASE(0)
	string c_[] = {"AAA",
 "AAA",
 "AAA"};
	  vector <string> c(c_, c_+sizeof(c_)/sizeof(*c_)); 
	string d_[] = {"ABJ",
 "JAB",
 "BJA"};
	  vector <string> d(d_, d_+sizeof(d_)/sizeof(*d_)); 
	int fee = 15; 
	int _ = 12; 
END
CASE(1)
	string c_[] = {"AAAA",
 "AAAA",
 "AAAA",
 "AAAA"};
	  vector <string> c(c_, c_+sizeof(c_)/sizeof(*c_)); 
	string d_[] = {"AAAA",
 "AAAA",
 "AAAA",
 "AAAA"};
	  vector <string> d(d_, d_+sizeof(d_)/sizeof(*d_)); 
	int fee = 100; 
	int _ = 300; 
END
CASE(2)
	string c_[] = {"A//",
 "/A/",
 "//A"};
	  vector <string> c(c_, c_+sizeof(c_)/sizeof(*c_)); 
	string d_[] = {"A//",
 "/A/",
 "//A"};
	  vector <string> d(d_, d_+sizeof(d_)/sizeof(*d_)); 
	int fee = 1000; 
	int _ = 0; 
END
CASE(3)
	string c_[] = {"AAA////",
 "/AA/A//",
 "//AA/A/",
 "A//AA//",
 "///AAA/",
 "///A/AA",
 "AA////A"};
	  vector <string> c(c_, c_+sizeof(c_)/sizeof(*c_)); 
	string d_[] = {"AKo////",
 "/AU/X//",
 "//AZ/o/",
 "j//AK//",
 "///XAo/",
 "///y/AK",
 "KP////A"};
	  vector <string> d(d_, d_+sizeof(d_)/sizeof(*d_)); 
	int fee = 1000; 
	int _ = 1809; 
END
	/*
CASE(4)
	string c_[] = ;
	  vector <string> c(c_, c_+sizeof(c_)/sizeof(*c_)); 
	string d_[] = ;
	  vector <string> d(d_, d_+sizeof(d_)/sizeof(*d_)); 
	int fee = ; 
	int _ = ; 
END
CASE(5)
	string c_[] = ;
	  vector <string> c(c_, c_+sizeof(c_)/sizeof(*c_)); 
	string d_[] = ;
	  vector <string> d(d_, d_+sizeof(d_)/sizeof(*d_)); 
	int fee = ; 
	int _ = ; 
END
*/
}
// END CUT HERE