Artifact Content
Not logged in

Artifact d61bab3b8e785a85f828d1c1655415d7770aa452


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

int INF = 10000000;

class TwinTowns
{
public:
	vector <int> optimalTwinTowns(vector <int> x, vector <int> y, int maxPartners, int minDistance) 
	{
		int N = x.size();
		int cost[10][10];
		for(int i=0; i<N; ++i)
		for(int j=0; j<N; ++j)
		{
			int d = abs(x[i]-x[j]) + abs(y[i]-y[j]);
			cost[i][j] = (d < minDistance ? INF : d);
		}

		vector<int> v(N, maxPartners);
		pair<int,int> r = rec(0, N, v, cost);
		vector<int> ans(2);
		ans[0] = -r.first;
		ans[1] = r.second;
		return ans;
	}

	map< vector<int>,pair<int,int> > memo;
	pair<int,int> rec(int i, int N, vector<int>& v, int cost[10][10])
	{
		if( i==N )
			return make_pair(0, 0);
		vector<int> key(v.begin()+i, v.end());
		if( memo.count(key) )
			return memo[key];

		// choose at most v[i] js s.t. cost[i][j]!=INF && v[j]!=0
		pair<int,int> best = rec(i+1, N, v, cost);
		if( v[i] >= 1 )
		{
			for(int j1= i+1; j1<N; ++j1) if(cost[i][j1]!=INF && v[j1])
			{
				v[j1]--;
				pair<int,int> a = rec(i+1, N, v, cost);
				a.first  -= 1;
				a.second += cost[i][j1];
				best = min(best, a);
				v[j1]++;
			}
		}
		if( v[i] >= 2 )
		{
			for(int j1= i+1; j1<N; ++j1) if(cost[i][j1]!=INF && v[j1])
			for(int j2=j1+1; j2<N; ++j2) if(cost[i][j2]!=INF && v[j2])
			{
				v[j1]--;
				v[j2]--;
				pair<int,int> a = rec(i+1, N, v, cost);
				a.first  -= 2;
				a.second += cost[i][j1] + cost[i][j2];
				best = min(best, a);
				v[j1]++;
				v[j2]++;
			}
		}
		if( v[i] >= 3 )
		{
			for(int j1= i+1; j1<N; ++j1) if(cost[i][j1]!=INF && v[j1])
			for(int j2=j1+1; j2<N; ++j2) if(cost[i][j2]!=INF && v[j2])
			for(int j3=j2+1; j3<N; ++j3) if(cost[i][j3]!=INF && v[j3])
			{
				v[j1]--;
				v[j2]--;
				v[j3]--;
				pair<int,int> a = rec(i+1, N, v, cost);
				a.first  -= 3;
				a.second += cost[i][j1] + cost[i][j2] + cost[i][j3];
				best = min(best, a);
				v[j1]++;
				v[j2]++;
				v[j3]++;
			}
		}

		return memo[key] = best;
	}
};

// 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> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
int verify_case(const vector <int> &Expected, const vector <int> &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } return 0;}

template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
	int x_[] = {0,0,10};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {0,10,4};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	int maxPartners = 1; 
	int minDistance = 1; 
	int RetVal_[] = {1, 10 };
	  vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 
	return verify_case(RetVal, TwinTowns().optimalTwinTowns(x, y, maxPartners, minDistance)); }
int Test_(Case_<1>) {
	int x_[] = {0,0,10};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {0,10,4};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	int maxPartners = 1; 
	int minDistance = 11; 
	int RetVal_[] = {1, 14 };
	  vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 
	return verify_case(RetVal, TwinTowns().optimalTwinTowns(x, y, maxPartners, minDistance)); }
int Test_(Case_<2>) {
	int x_[] = {0,10,0,10};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {0,0,20,20};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	int maxPartners = 1; 
	int minDistance = 1; 
	int RetVal_[] = {2, 20 };
	  vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 
	return verify_case(RetVal, TwinTowns().optimalTwinTowns(x, y, maxPartners, minDistance)); }
int Test_(Case_<3>) {
	int x_[] = {0,10,0,10};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {0,0,20,20};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	int maxPartners = 2; 
	int minDistance = 10; 
	int RetVal_[] = {4, 60 };
	  vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 
	return verify_case(RetVal, TwinTowns().optimalTwinTowns(x, y, maxPartners, minDistance)); }
int Test_(Case_<4>) {
	int x_[] = {0,0,0,0,0,0,0,0,0};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {1,2,3,4,5,6,7,8,9};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	int maxPartners = 3; 
	int minDistance = 6; 
	int RetVal_[] = {6, 40 };
	  vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 
	return verify_case(RetVal, TwinTowns().optimalTwinTowns(x, y, maxPartners, minDistance)); }

template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<>      void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE