Artifact Content
Not logged in

Artifact 93c53f4c26bd697ea27adc653935594b79ae99d2


#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;

static const int NV = 512;
typedef int           flow;
typedef int           vert;
typedef vert          edge;
typedef vector<edge>  edges;
typedef vector<edges> graph;
typedef flow          flow_graph[NV][NV];
static const flow INF = 0x3fffffff;

flow dinic_dfs( graph& G, flow_graph F, vert v, vert D,
                int LV[], flow flow_in, int blocked[] )
{
	flow flow_out = 0;
	for(int i=0; i!=G[v].size(); ++i) {
		int u = G[v][i];
		if( LV[v]+1==LV[u] && F[v][u] ) {
			flow f = min(flow_in-flow_out, F[v][u]);
			if( u==D || !blocked[u] && (f=dinic_dfs(G,F,u,D,LV,f,blocked)) ) {
				F[v][u]  -= f;
				F[u][v]  += f;
				flow_out += f;
				if( flow_in == flow_out ) return flow_out;
			}
		}
	}
	blocked[v] = (flow_out==0);
	return flow_out;
}

flow maxFlow( graph& G, flow_graph F, vert S, vert D )
{
	for( flow total=0 ;; ) {
		int LV[NV] = {0};
		vector<int> Q(1, S);
		for(int lv=1; !Q.empty(); ++lv) {
			vector<int> Q2;
			for(int i=0; i!=Q.size(); ++i) {
				edges& ne = G[Q[i]];
				for(int j=0; j!=ne.size(); ++j)
					if( F[Q[i]][ne[j]] && !LV[ne[j]] && ne[j]!=S )
						LV[ne[j]]=lv, Q2.push_back(ne[j]);
			}
			Q.swap(Q2);
		}

		if( !LV[D] )
			return total;

		int blocked[NV] = {};
		total += dinic_dfs( G, F, S, D, LV, 0x7fffffff, blocked );
	}
}

class GreenWarfare { public:
	int minimumEnergyCost(vector <int> canonX, vector <int> canonY, vector <int> baseX, vector <int> baseY, vector <int> plantX, vector <int> plantY, int energySupplyRadius) 
	{
		int B = baseX.size();
		int P = plantX.size();

		graph      G(P+P+B+B+2);
		flow_graph F = {};

		for(int x=0; x<P; ++x) {
			G[P+P+B+B].push_back(x);
			G[x].push_back(P+P+B+B);
			F[P+P+B+B][x] = INF;
		}
		for(int x=0; x<P; ++x) {
			G[x].push_back(P+x);
			G[P+x].push_back(x);
			F[x][P+x] = breakEnergy(canonX, canonY, plantX[x], plantY[x]);
		}
		for(int x=0; x<P; ++x)
			for(int y=0; y<B; ++y)
				if( dist(plantX[x],plantY[x],baseX[y],baseY[y]) <= energySupplyRadius*energySupplyRadius ) {
					G[P+x].push_back(P+P+y);
					G[P+P+y].push_back(P+x);
					F[P+x][P+P+y] = INF;
				}
		for(int x=0; x<B; ++x) {
			G[P+P+x].push_back(P+P+B+x);
			G[P+P+B+x].push_back(P+P+x);
			F[P+P+x][P+P+B+x] = breakEnergy(canonX, canonY, baseX[x], baseY[x]);
		}
		for(int x=0; x<B; ++x) {
			G[P+P+B+x].push_back(P+P+B+B+1);
			G[P+P+B+B+1].push_back(P+P+B+x);
			F[P+P+B+x][P+P+B+B+1] = INF;
		}

		return maxFlow(G, F, P+P+B+B, P+P+B+B+1);
	}

	int dist(int x, int y, int X, int Y)
	{
		return (X-x)*(X-x) + (Y-y)*(Y-y);
	}

	int breakEnergy(const vector<int>& cx, const vector<int>& cy, int x, int y)
	{
		int e = 0x7fffffff;
		for(int i=0; i<cx.size(); ++i)
			e = min(e, dist(x,y,cx[i],cy[i]));
		return e;
	}
};

// 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(_, GreenWarfare().minimumEnergyCost(canonX, canonY, baseX, baseY, plantX, plantY, energySupplyRadius));}
int main(){

CASE(0)
	int canonX_[] = { 0 };
	  vector <int> canonX(canonX_, canonX_+sizeof(canonX_)/sizeof(*canonX_)); 
	int canonY_[] = { 0 };
	  vector <int> canonY(canonY_, canonY_+sizeof(canonY_)/sizeof(*canonY_)); 
	int baseX_[] = {1,2,3};
	  vector <int> baseX(baseX_, baseX_+sizeof(baseX_)/sizeof(*baseX_)); 
	int baseY_[] = {0,0,0};
	  vector <int> baseY(baseY_, baseY_+sizeof(baseY_)/sizeof(*baseY_)); 
	int plantX_[] = {3};
	  vector <int> plantX(plantX_, plantX_+sizeof(plantX_)/sizeof(*plantX_)); 
	int plantY_[] = {3};
	  vector <int> plantY(plantY_, plantY_+sizeof(plantY_)/sizeof(*plantY_)); 
	int energySupplyRadius = 4; 
	int _ = 14; 
END
CASE(1)
	int canonX_[] = { 0 };
	  vector <int> canonX(canonX_, canonX_+sizeof(canonX_)/sizeof(*canonX_)); 
	int canonY_[] = { 0 };
	  vector <int> canonY(canonY_, canonY_+sizeof(canonY_)/sizeof(*canonY_)); 
	int baseX_[] = {1,2,3};
	  vector <int> baseX(baseX_, baseX_+sizeof(baseX_)/sizeof(*baseX_)); 
	int baseY_[] = {0,0,0};
	  vector <int> baseY(baseY_, baseY_+sizeof(baseY_)/sizeof(*baseY_)); 
	int plantX_[] = {2};
	  vector <int> plantX(plantX_, plantX_+sizeof(plantX_)/sizeof(*plantX_)); 
	int plantY_[] = {2};
	  vector <int> plantY(plantY_, plantY_+sizeof(plantY_)/sizeof(*plantY_)); 
	int energySupplyRadius = 4; 
	int _ = 8; 
END
CASE(2)
	int canonX_[] = {3,6};
	  vector <int> canonX(canonX_, canonX_+sizeof(canonX_)/sizeof(*canonX_)); 
	int canonY_[] = {3,6};
	  vector <int> canonY(canonY_, canonY_+sizeof(canonY_)/sizeof(*canonY_)); 
	int baseX_[] = {1,2,3,4,5};
	  vector <int> baseX(baseX_, baseX_+sizeof(baseX_)/sizeof(*baseX_)); 
	int baseY_[] = {5,4,2,3,1};
	  vector <int> baseY(baseY_, baseY_+sizeof(baseY_)/sizeof(*baseY_)); 
	int plantX_[] = {1,2,5};
	  vector <int> plantX(plantX_, plantX_+sizeof(plantX_)/sizeof(*plantX_)); 
	int plantY_[] = {1,2,5};
	  vector <int> plantY(plantY_, plantY_+sizeof(plantY_)/sizeof(*plantY_)); 
	int energySupplyRadius = 5; 
	int _ = 12; 
END
CASE(3)
	int canonX_[] = {0};
	  vector <int> canonX(canonX_, canonX_+sizeof(canonX_)/sizeof(*canonX_)); 
	int canonY_[] = {0};
	  vector <int> canonY(canonY_, canonY_+sizeof(canonY_)/sizeof(*canonY_)); 
	int baseX_[] = {-10,-10,10};
	  vector <int> baseX(baseX_, baseX_+sizeof(baseX_)/sizeof(*baseX_)); 
	int baseY_[] = {10,-10,0};
	  vector <int> baseY(baseY_, baseY_+sizeof(baseY_)/sizeof(*baseY_)); 
	int plantX_[] = {10,10,-10};
	  vector <int> plantX(plantX_, plantX_+sizeof(plantX_)/sizeof(*plantX_)); 
	int plantY_[] = {10,-10,0};
	  vector <int> plantY(plantY_, plantY_+sizeof(plantY_)/sizeof(*plantY_)); 
	int energySupplyRadius = 10; 
	int _ = 200; 
END
CASE(4)
	int canonX_[] = {0};
	  vector <int> canonX(canonX_, canonX_+sizeof(canonX_)/sizeof(*canonX_)); 
	int canonY_[] = {0};
	  vector <int> canonY(canonY_, canonY_+sizeof(canonY_)/sizeof(*canonY_)); 
	int baseX_[] = {3};
	  vector <int> baseX(baseX_, baseX_+sizeof(baseX_)/sizeof(*baseX_)); 
	int baseY_[] = {3};
	  vector <int> baseY(baseY_, baseY_+sizeof(baseY_)/sizeof(*baseY_)); 
	int plantX_[] = {1,2,3};
	  vector <int> plantX(plantX_, plantX_+sizeof(plantX_)/sizeof(*plantX_)); 
	int plantY_[] = {0,0,0};
	  vector <int> plantY(plantY_, plantY_+sizeof(plantY_)/sizeof(*plantY_)); 
	int energySupplyRadius = 4; 
	int _ = 14; 
END
/*
CASE(5)
	int canonX_[] = ;
	  vector <int> canonX(canonX_, canonX_+sizeof(canonX_)/sizeof(*canonX_)); 
	int canonY_[] = ;
	  vector <int> canonY(canonY_, canonY_+sizeof(canonY_)/sizeof(*canonY_)); 
	int baseX_[] = ;
	  vector <int> baseX(baseX_, baseX_+sizeof(baseX_)/sizeof(*baseX_)); 
	int baseY_[] = ;
	  vector <int> baseY(baseY_, baseY_+sizeof(baseY_)/sizeof(*baseY_)); 
	int plantX_[] = ;
	  vector <int> plantX(plantX_, plantX_+sizeof(plantX_)/sizeof(*plantX_)); 
	int plantY_[] = ;
	  vector <int> plantY(plantY_, plantY_+sizeof(plantY_)/sizeof(*plantY_)); 
	int energySupplyRadius = ; 
	int _ = ; 
END
CASE(6)
	int canonX_[] = ;
	  vector <int> canonX(canonX_, canonX_+sizeof(canonX_)/sizeof(*canonX_)); 
	int canonY_[] = ;
	  vector <int> canonY(canonY_, canonY_+sizeof(canonY_)/sizeof(*canonY_)); 
	int baseX_[] = ;
	  vector <int> baseX(baseX_, baseX_+sizeof(baseX_)/sizeof(*baseX_)); 
	int baseY_[] = ;
	  vector <int> baseY(baseY_, baseY_+sizeof(baseY_)/sizeof(*baseY_)); 
	int plantX_[] = ;
	  vector <int> plantX(plantX_, plantX_+sizeof(plantX_)/sizeof(*plantX_)); 
	int plantY_[] = ;
	  vector <int> plantY(plantY_, plantY_+sizeof(plantY_)/sizeof(*plantY_)); 
	int energySupplyRadius = ; 
	int _ = ; 
END
*/
}
// END CUT HERE