#1389: Sewage Treatment time limit: 2000ms single point limit: 2000ms memory limit: 256MB description
After years of suffering, people could don't tolerate the pollution in the city any more, and started a long-lasting protest . Eventually, the municipal government made up it mind to deal with the sewage by discharged. The Government planned to deploy some Pollution-killer Utilities (Pkus) in order to clean all the sewage, and meanwhile, M Inimize the total cost.
The city can be approximately considered as a plane. There are n factories discharging sewage. The i-th one is located in (xi, Yi), and discharges SI units of sewage. The government has built m Pkus. The Pkus ' locations are also fixed, and the j-th one is located at (XJ, YJ). Two features of Pkus are essential, and you'll need to find the best choice of them:u is the upper limit of units of SE Wage that can being cleaned by one PKU every day, and C are the coverage radius of one PKU (which means, only if the distance Between the PKU and the factory does not exceed the coverage radius, sewage discharged from this factory can is cleaned by t His PKU). The Pkus share the same u and C. Because of some technical reasons, U has to be a positive integer.
This is your task. The cost of deploying these pkus can is weighed by a empirical formula:
f = uxsqrt (c)
You are need to calculate the minimum value of F, and guarantee all the sewage are treated.
input
There are no more than test cases. Each test case starts with a line containing two positive integers n and m (1 <= N, M <=), representing the numb Er of factories and Pkus. Then n lines follow, the i-th line contains three integers xi., Yi, si ( -10000 <= xi., Yi <= 10000, 1 <= si <= 1 , representing the I-th factory ' location and the quantity of sewage by it discharged. In the next following m lines, the j-th line contains two integers xj, YJ ( -10000 <= XJ, YJ <=), 10000 The j-th PKU ' s location. After the each test case, there is a empty line. After all the test cases there was a line "0 0", which indicates the ' end of the ' input, and should not to be processed. Output
For each test case, output a single line containing a number and which is the minimum value of F. This number should is rounded to an integer. Tips
Test Case 1:
When u = and c = 2, F has the minimum value of * sqrt (2) = 16.97 = 17.
Test Case 2:
When u = ten, c = sqrt (2), F has the minimum value of * sqrt (sqrt (2)) = 11.89 = 12.
The input guarantees that there is always a valid solution. You may assume that the factories and Pkus are uniformly randomly on the distributed. Sample input
3 1
. 1 0 5 2 0 3 0 1 4 0 0 4 2 0 0 4 3 0 5 3 2 3 0 2 6 1 1 2 1 0 0
Sample output
12
Topic Link:
http://hihocoder.com/problemset/problem/1389
The main effect of the topic:
N a sewage plant fac,m a sewage treatment plant PKU (n,m<=100). Each sewage plant coordinates (XI,YI), will produce the SI the sewage. Each sewage treatment plant coordinates (XI,YI) can handle the total amount of u (a sewage treatment plant can be treated together) from the range of C.
The u,c of all sewage treatment plants must be kept the same, and the minimum value of f=u*√c function can be obtained in the case of all sewage treatment. (U is a positive integer, C is Euler distance, the coordinate range is in [ -10000,10000])
Topic Idea:
"Two points + network flow + optimization"
First of all sewage treatment plant b[i].from to each sewage plant b[i].to Euler distance B[i].dis, from small to large sorting, a total of N*m edge. Set source S remit t
Enumerate the C=b[i].dis (make sure each sewage plant is connected to at least one sewage treatment plant), the smallest U for the second division
Check when the front I edge is added to the network flow diagram, the edge of the capacity of infinity. s to each sewage treatment plant with a capacity for the side of the U, each sewage plant to t even a volume for the side of SI
Then run the maximum flow from S to T to see if it equals total sewage amount sum. equals to indicate that the current u is feasible, otherwise you are not currently viable.
The light does so will T, which needs to be optimized, and the upper bound of the binary U is the smallest u (because C is growing, and F is more likely to be better only if u is reduced), and the lower bound is sum/n, which means at least the average amount of each sewage treatment plant.
Add a second to optimize the current enumeration of C and the lower bound l The calculated function F has exceeded the f of the optimal value of the break (and then the answer will not be more excellent).
"I don't know why it's over," he said. I think I didn't write it when I was playing. The result is that Daniel's findings really do. But when the game was optimized, it was not clear, and maybe T.
by coolxxx//#include <bits/stdc++.h> #include <iostream> #include <algorithm> #include < string> #include <iomanip> #include <map> #include <stack> #include <queue> #include <set > #include <bitset> #include <memory.h> #include <time.h> #include <stdio.h> #include < stdlib.h> #include <string.h>/#include <stdbool.h> #include <math.h> #define MIN (a,b) ((a) < (b )? (a):(B)) #define MAX (A,b) ((a) > (b)? ( A):(B) #define ABS (a) ((a) >0? ( A):(-(a)) #define LOWBIT (a) (a& (a)) #define SQR (a) ((a) * (a)) #define SWAP (A,B) ((a) ^= (b), (b) ^= (a), (a) ^= (b)) # Define MEM (A,b) memset (A,b,sizeof (a)) #define EPS (1E-10) #define J 10000 #define MOD 1000000007 #define MAX 0x7f7f7f7f #d Efine PI 3.14159265358979323 #pragma comment (linker, "/stack:1024000000,1024000000") #define N 204 #define M 20004 using NA
Mespace std;
typedef long Long LL;
Double Anss;
LL aans,sum;
int cas,cass;
int N,m,lll,ans;
int c,u,s,t,nn; int D[N],vd[n],last[n];
int mapp[n][n];
BOOL Mark[n]; struct point {int x,y,qq;}
Fac[n],pku[n]; struct Edge {int from,to,dis;}
B[M]; struct XXX {int from,to,next,q;}
a[m<<1];
BOOL Cmp1 (Point Aa,point BB) {if (aa.x!=bb.x) return aa.x<bb.x;
Return aa.y<bb.y;
BOOL Cmp2 (Edge Aa,edge bb) {return aa.dis<bb.dis;} void Add (int x,int y,int z) {a[++lll].to=y;
A[lll].q=z;
A[LLL].NEXT=LAST[X];
last[x]=lll;
int sap (int u,int f) {int i,v,tt,asp=0,mix=nn-1;
if (u==t) return F;
for (I=last[u];i!=0;i=a[i].next) {v=a[i].to;
if (a[i].q>0) {if (d[u]==d[v]+1) {Tt=sap (V,min (F-ASP,A[I].Q));
Asp+=tt;
A[i].q-=tt;
A[i^1].q+=tt;
if (asp==f | | d[s]==nn) return ASP;
} mix=min (Mix,d[v]);
} if (asp!=0) return ASP;
if (!--Vd[d[u]]) d[s]=nn;
else vd[d[u]=mix+1]++;
return ASP;
BOOL Check (int u) {int i,f;
MEM (d,0); mem (vd,0);
nn=t;
Vd[0]=nn;
ans=0;
while (D[S]<NN) {F=SAP (S,max);
Ans+=f;
} if (ans==sum) return 1;
return 0;} void Build (int k,int uu) {int i;
MEM (last,0); lll=1;
for (i=1;i<=m;i++) Add (S,i,uu), add (i,s,0);
for (i=1;i<=n;i++) Add (M+I,T,FAC[I].QQ), add (t,m+i,0);
for (i=1;i<=k;i++) Add (B[i].from,b[i].to+m,max), add (b[i].to+m,b[i].from,0);
int main () {#ifndef Online_judgew//Freopen ("1.txt", "R", stdin);//Freopen ("2.txt", "w", stdout);
#endif int i,j,k;
int x,y,z,l,r,mid;
Init (); For (scanf ("%d", &cass); cass;cass--)//For (scanf ("%d", &cas), cass=1;cass<=cas;cass++)//while (~SCANF ("
%s ", s)" while (~SCANF ("%d%d", &n,&m)) {sum=0;cas=0;anss=1e30;ans=0;
MEM (mark,0);
if (!n &&!m) break;
for (i=1;i<=n;i++) {scanf ("%d%d%d", &FAC[I].X,&FAC[I].Y,&FAC[I].QQ);
SUM+=FAC[I].QQ;
for (i=1;i<=m;i++) scanf ("%d%d", &pku[i].x,&pku[i].y);
Sort (FAC+1,FAC+1+N,CMP1);
Sort (PKU+1,PKU+1+M,CMP1);
for (i=1;i<=m;i++) {for (j=1;j<=n;j++) {b[++cas].from=i;
B[cas].to=j; B[CAS].DIS=SQR (fac[j].x-pku[i].x) +sqr (FAC[J].Y-PKU[I].Y);
} sort (B+1,B+1+CAS,CMP2);
s=n+m+1,t=n+m+2;
for (j=0,k=1;k<=cas;k++) {if (!mark[b[k].to]) {j++,mark[b[k].to]=1;
if (j==n) break;
}} u=sum;
for (; k<=cas;k++) {C=b[k].dis;
L=sum/n,r=u;
if (double) l*sqrt (sqrt (C)) >anss) break;
while (L<r) {mid= (l+r) >>1;
Build (K,mid);
if (check (mid)) R=mid;
else l=mid+1;
} u=min (U,r);
Anss=min (Anss,u*sqrt (sqrt (C)));
printf ("%d\n", int (anss+0.5));
return 0;
}
/*
//
//
*/