4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <functional> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: static const int NV = 512; 4fd800b3a8 2011-02-23 kinaba: typedef int flow; 4fd800b3a8 2011-02-23 kinaba: typedef int vert; 4fd800b3a8 2011-02-23 kinaba: typedef vert edge; 4fd800b3a8 2011-02-23 kinaba: typedef vector<edge> edges; 4fd800b3a8 2011-02-23 kinaba: typedef vector<edges> graph; 4fd800b3a8 2011-02-23 kinaba: typedef flow flow_graph[NV][NV]; 4fd800b3a8 2011-02-23 kinaba: static const flow INF = 0x3fffffff; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: flow dinic_dfs( graph& G, flow_graph F, vert v, vert D, 4fd800b3a8 2011-02-23 kinaba: int LV[], flow flow_in, int blocked[] ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: flow flow_out = 0; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i!=G[v].size(); ++i) { 4fd800b3a8 2011-02-23 kinaba: int u = G[v][i]; 4fd800b3a8 2011-02-23 kinaba: if( LV[v]+1==LV[u] && F[v][u] ) { 4fd800b3a8 2011-02-23 kinaba: flow f = min(flow_in-flow_out, F[v][u]); 4fd800b3a8 2011-02-23 kinaba: if( u==D || !blocked[u] && (f=dinic_dfs(G,F,u,D,LV,f,blocked)) ) { 4fd800b3a8 2011-02-23 kinaba: F[v][u] -= f; 4fd800b3a8 2011-02-23 kinaba: F[u][v] += f; 4fd800b3a8 2011-02-23 kinaba: flow_out += f; 4fd800b3a8 2011-02-23 kinaba: if( flow_in == flow_out ) return flow_out; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: blocked[v] = (flow_out==0); 4fd800b3a8 2011-02-23 kinaba: return flow_out; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: flow maxFlow( graph& G, flow_graph F, vert S, vert D ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: for( flow total=0 ;; ) { 4fd800b3a8 2011-02-23 kinaba: int LV[NV] = {0}; 4fd800b3a8 2011-02-23 kinaba: vector<int> Q(1, S); 4fd800b3a8 2011-02-23 kinaba: for(int lv=1; !Q.empty(); ++lv) { 4fd800b3a8 2011-02-23 kinaba: vector<int> Q2; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i!=Q.size(); ++i) { 4fd800b3a8 2011-02-23 kinaba: edges& ne = G[Q[i]]; 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j!=ne.size(); ++j) 4fd800b3a8 2011-02-23 kinaba: if( F[Q[i]][ne[j]] && !LV[ne[j]] && ne[j]!=S ) 4fd800b3a8 2011-02-23 kinaba: LV[ne[j]]=lv, Q2.push_back(ne[j]); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: Q.swap(Q2); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( !LV[D] ) 4fd800b3a8 2011-02-23 kinaba: return total; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int blocked[NV] = {}; 4fd800b3a8 2011-02-23 kinaba: total += dinic_dfs( G, F, S, D, LV, 0x7fffffff, blocked ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class GreenWarfare { public: 4fd800b3a8 2011-02-23 kinaba: int minimumEnergyCost(vector <int> canonX, vector <int> canonY, vector <int> baseX, vector <int> baseY, vector <int> plantX, vector <int> plantY, int energySupplyRadius) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int B = baseX.size(); 4fd800b3a8 2011-02-23 kinaba: int P = plantX.size(); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: graph G(P+P+B+B+2); 4fd800b3a8 2011-02-23 kinaba: flow_graph F = {}; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int x=0; x<P; ++x) { 4fd800b3a8 2011-02-23 kinaba: G[P+P+B+B].push_back(x); 4fd800b3a8 2011-02-23 kinaba: G[x].push_back(P+P+B+B); 4fd800b3a8 2011-02-23 kinaba: F[P+P+B+B][x] = INF; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: for(int x=0; x<P; ++x) { 4fd800b3a8 2011-02-23 kinaba: G[x].push_back(P+x); 4fd800b3a8 2011-02-23 kinaba: G[P+x].push_back(x); 4fd800b3a8 2011-02-23 kinaba: F[x][P+x] = breakEnergy(canonX, canonY, plantX[x], plantY[x]); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: for(int x=0; x<P; ++x) 4fd800b3a8 2011-02-23 kinaba: for(int y=0; y<B; ++y) 4fd800b3a8 2011-02-23 kinaba: if( dist(plantX[x],plantY[x],baseX[y],baseY[y]) <= energySupplyRadius*energySupplyRadius ) { 4fd800b3a8 2011-02-23 kinaba: G[P+x].push_back(P+P+y); 4fd800b3a8 2011-02-23 kinaba: G[P+P+y].push_back(P+x); 4fd800b3a8 2011-02-23 kinaba: F[P+x][P+P+y] = INF; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: for(int x=0; x<B; ++x) { 4fd800b3a8 2011-02-23 kinaba: G[P+P+x].push_back(P+P+B+x); 4fd800b3a8 2011-02-23 kinaba: G[P+P+B+x].push_back(P+P+x); 4fd800b3a8 2011-02-23 kinaba: F[P+P+x][P+P+B+x] = breakEnergy(canonX, canonY, baseX[x], baseY[x]); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: for(int x=0; x<B; ++x) { 4fd800b3a8 2011-02-23 kinaba: G[P+P+B+x].push_back(P+P+B+B+1); 4fd800b3a8 2011-02-23 kinaba: G[P+P+B+B+1].push_back(P+P+B+x); 4fd800b3a8 2011-02-23 kinaba: F[P+P+B+x][P+P+B+B+1] = INF; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: return maxFlow(G, F, P+P+B+B, P+P+B+B+1); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int dist(int x, int y, int X, int Y) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: return (X-x)*(X-x) + (Y-y)*(Y-y); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int breakEnergy(const vector<int>& cx, const vector<int>& cy, int x, int y) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int e = 0x7fffffff; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<cx.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: e = min(e, dist(x,y,cx[i],cy[i])); 4fd800b3a8 2011-02-23 kinaba: return e; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time; string timer() 4fd800b3a8 2011-02-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4fd800b3a8 2011-02-23 kinaba: { os << "{ "; 4fd800b3a8 2011-02-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4fd800b3a8 2011-02-23 kinaba: void verify_case(const int& Expected, const int& Received) { 4fd800b3a8 2011-02-23 kinaba: bool ok = (Expected == Received); 4fd800b3a8 2011-02-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4fd800b3a8 2011-02-23 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4fd800b3a8 2011-02-23 kinaba: #define END verify_case(_, GreenWarfare().minimumEnergyCost(canonX, canonY, baseX, baseY, plantX, plantY, energySupplyRadius));} 4fd800b3a8 2011-02-23 kinaba: int main(){ 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CASE(0) 4fd800b3a8 2011-02-23 kinaba: int canonX_[] = { 0 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> canonX(canonX_, canonX_+sizeof(canonX_)/sizeof(*canonX_)); 4fd800b3a8 2011-02-23 kinaba: int canonY_[] = { 0 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> canonY(canonY_, canonY_+sizeof(canonY_)/sizeof(*canonY_)); 4fd800b3a8 2011-02-23 kinaba: int baseX_[] = {1,2,3}; 4fd800b3a8 2011-02-23 kinaba: vector <int> baseX(baseX_, baseX_+sizeof(baseX_)/sizeof(*baseX_)); 4fd800b3a8 2011-02-23 kinaba: int baseY_[] = {0,0,0}; 4fd800b3a8 2011-02-23 kinaba: vector <int> baseY(baseY_, baseY_+sizeof(baseY_)/sizeof(*baseY_)); 4fd800b3a8 2011-02-23 kinaba: int plantX_[] = {3}; 4fd800b3a8 2011-02-23 kinaba: vector <int> plantX(plantX_, plantX_+sizeof(plantX_)/sizeof(*plantX_)); 4fd800b3a8 2011-02-23 kinaba: int plantY_[] = {3}; 4fd800b3a8 2011-02-23 kinaba: vector <int> plantY(plantY_, plantY_+sizeof(plantY_)/sizeof(*plantY_)); 4fd800b3a8 2011-02-23 kinaba: int energySupplyRadius = 4; 4fd800b3a8 2011-02-23 kinaba: int _ = 14; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: int canonX_[] = { 0 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> canonX(canonX_, canonX_+sizeof(canonX_)/sizeof(*canonX_)); 4fd800b3a8 2011-02-23 kinaba: int canonY_[] = { 0 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> canonY(canonY_, canonY_+sizeof(canonY_)/sizeof(*canonY_)); 4fd800b3a8 2011-02-23 kinaba: int baseX_[] = {1,2,3}; 4fd800b3a8 2011-02-23 kinaba: vector <int> baseX(baseX_, baseX_+sizeof(baseX_)/sizeof(*baseX_)); 4fd800b3a8 2011-02-23 kinaba: int baseY_[] = {0,0,0}; 4fd800b3a8 2011-02-23 kinaba: vector <int> baseY(baseY_, baseY_+sizeof(baseY_)/sizeof(*baseY_)); 4fd800b3a8 2011-02-23 kinaba: int plantX_[] = {2}; 4fd800b3a8 2011-02-23 kinaba: vector <int> plantX(plantX_, plantX_+sizeof(plantX_)/sizeof(*plantX_)); 4fd800b3a8 2011-02-23 kinaba: int plantY_[] = {2}; 4fd800b3a8 2011-02-23 kinaba: vector <int> plantY(plantY_, plantY_+sizeof(plantY_)/sizeof(*plantY_)); 4fd800b3a8 2011-02-23 kinaba: int energySupplyRadius = 4; 4fd800b3a8 2011-02-23 kinaba: int _ = 8; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: int canonX_[] = {3,6}; 4fd800b3a8 2011-02-23 kinaba: vector <int> canonX(canonX_, canonX_+sizeof(canonX_)/sizeof(*canonX_)); 4fd800b3a8 2011-02-23 kinaba: int canonY_[] = {3,6}; 4fd800b3a8 2011-02-23 kinaba: vector <int> canonY(canonY_, canonY_+sizeof(canonY_)/sizeof(*canonY_)); 4fd800b3a8 2011-02-23 kinaba: int baseX_[] = {1,2,3,4,5}; 4fd800b3a8 2011-02-23 kinaba: vector <int> baseX(baseX_, baseX_+sizeof(baseX_)/sizeof(*baseX_)); 4fd800b3a8 2011-02-23 kinaba: int baseY_[] = {5,4,2,3,1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> baseY(baseY_, baseY_+sizeof(baseY_)/sizeof(*baseY_)); 4fd800b3a8 2011-02-23 kinaba: int plantX_[] = {1,2,5}; 4fd800b3a8 2011-02-23 kinaba: vector <int> plantX(plantX_, plantX_+sizeof(plantX_)/sizeof(*plantX_)); 4fd800b3a8 2011-02-23 kinaba: int plantY_[] = {1,2,5}; 4fd800b3a8 2011-02-23 kinaba: vector <int> plantY(plantY_, plantY_+sizeof(plantY_)/sizeof(*plantY_)); 4fd800b3a8 2011-02-23 kinaba: int energySupplyRadius = 5; 4fd800b3a8 2011-02-23 kinaba: int _ = 12; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: int canonX_[] = {0}; 4fd800b3a8 2011-02-23 kinaba: vector <int> canonX(canonX_, canonX_+sizeof(canonX_)/sizeof(*canonX_)); 4fd800b3a8 2011-02-23 kinaba: int canonY_[] = {0}; 4fd800b3a8 2011-02-23 kinaba: vector <int> canonY(canonY_, canonY_+sizeof(canonY_)/sizeof(*canonY_)); 4fd800b3a8 2011-02-23 kinaba: int baseX_[] = {-10,-10,10}; 4fd800b3a8 2011-02-23 kinaba: vector <int> baseX(baseX_, baseX_+sizeof(baseX_)/sizeof(*baseX_)); 4fd800b3a8 2011-02-23 kinaba: int baseY_[] = {10,-10,0}; 4fd800b3a8 2011-02-23 kinaba: vector <int> baseY(baseY_, baseY_+sizeof(baseY_)/sizeof(*baseY_)); 4fd800b3a8 2011-02-23 kinaba: int plantX_[] = {10,10,-10}; 4fd800b3a8 2011-02-23 kinaba: vector <int> plantX(plantX_, plantX_+sizeof(plantX_)/sizeof(*plantX_)); 4fd800b3a8 2011-02-23 kinaba: int plantY_[] = {10,-10,0}; 4fd800b3a8 2011-02-23 kinaba: vector <int> plantY(plantY_, plantY_+sizeof(plantY_)/sizeof(*plantY_)); 4fd800b3a8 2011-02-23 kinaba: int energySupplyRadius = 10; 4fd800b3a8 2011-02-23 kinaba: int _ = 200; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: int canonX_[] = {0}; 4fd800b3a8 2011-02-23 kinaba: vector <int> canonX(canonX_, canonX_+sizeof(canonX_)/sizeof(*canonX_)); 4fd800b3a8 2011-02-23 kinaba: int canonY_[] = {0}; 4fd800b3a8 2011-02-23 kinaba: vector <int> canonY(canonY_, canonY_+sizeof(canonY_)/sizeof(*canonY_)); 4fd800b3a8 2011-02-23 kinaba: int baseX_[] = {3}; 4fd800b3a8 2011-02-23 kinaba: vector <int> baseX(baseX_, baseX_+sizeof(baseX_)/sizeof(*baseX_)); 4fd800b3a8 2011-02-23 kinaba: int baseY_[] = {3}; 4fd800b3a8 2011-02-23 kinaba: vector <int> baseY(baseY_, baseY_+sizeof(baseY_)/sizeof(*baseY_)); 4fd800b3a8 2011-02-23 kinaba: int plantX_[] = {1,2,3}; 4fd800b3a8 2011-02-23 kinaba: vector <int> plantX(plantX_, plantX_+sizeof(plantX_)/sizeof(*plantX_)); 4fd800b3a8 2011-02-23 kinaba: int plantY_[] = {0,0,0}; 4fd800b3a8 2011-02-23 kinaba: vector <int> plantY(plantY_, plantY_+sizeof(plantY_)/sizeof(*plantY_)); 4fd800b3a8 2011-02-23 kinaba: int energySupplyRadius = 4; 4fd800b3a8 2011-02-23 kinaba: int _ = 14; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: /* 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: int canonX_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <int> canonX(canonX_, canonX_+sizeof(canonX_)/sizeof(*canonX_)); 4fd800b3a8 2011-02-23 kinaba: int canonY_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <int> canonY(canonY_, canonY_+sizeof(canonY_)/sizeof(*canonY_)); 4fd800b3a8 2011-02-23 kinaba: int baseX_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <int> baseX(baseX_, baseX_+sizeof(baseX_)/sizeof(*baseX_)); 4fd800b3a8 2011-02-23 kinaba: int baseY_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <int> baseY(baseY_, baseY_+sizeof(baseY_)/sizeof(*baseY_)); 4fd800b3a8 2011-02-23 kinaba: int plantX_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <int> plantX(plantX_, plantX_+sizeof(plantX_)/sizeof(*plantX_)); 4fd800b3a8 2011-02-23 kinaba: int plantY_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <int> plantY(plantY_, plantY_+sizeof(plantY_)/sizeof(*plantY_)); 4fd800b3a8 2011-02-23 kinaba: int energySupplyRadius = ; 4fd800b3a8 2011-02-23 kinaba: int _ = ; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(6) 4fd800b3a8 2011-02-23 kinaba: int canonX_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <int> canonX(canonX_, canonX_+sizeof(canonX_)/sizeof(*canonX_)); 4fd800b3a8 2011-02-23 kinaba: int canonY_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <int> canonY(canonY_, canonY_+sizeof(canonY_)/sizeof(*canonY_)); 4fd800b3a8 2011-02-23 kinaba: int baseX_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <int> baseX(baseX_, baseX_+sizeof(baseX_)/sizeof(*baseX_)); 4fd800b3a8 2011-02-23 kinaba: int baseY_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <int> baseY(baseY_, baseY_+sizeof(baseY_)/sizeof(*baseY_)); 4fd800b3a8 2011-02-23 kinaba: int plantX_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <int> plantX(plantX_, plantX_+sizeof(plantX_)/sizeof(*plantX_)); 4fd800b3a8 2011-02-23 kinaba: int plantY_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <int> plantY(plantY_, plantY_+sizeof(plantY_)/sizeof(*plantY_)); 4fd800b3a8 2011-02-23 kinaba: int energySupplyRadius = ; 4fd800b3a8 2011-02-23 kinaba: int _ = ; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: */ 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE