Topic Connection: http://acm.hdu.edu.cn/showproblem.php?pid=5046
Test instructions: To build no more than K airports in n cities to cover all cities, ask the minimum distance between airport cities.
Analysis: The dichotomy distance +DLX judgment, N City n column, then n row, each row city I can reach column J in the distance of two minutes is labeled 1, the problem is converted to select not more than K line to cover all columns (repeatable overlay).
Note: The maximum distance is 4*1e9, burst int, to use a long long, otherwise it will tle ...
#include <cstdio>#include<cstring>#include<string>#include<cmath>#include<iostream>#include<algorithm>#include<queue>#include<cstdlib>#include<stack>#include<vector>#include<Set>#include<map>#defineLL Long Long#defineMoD 10007#defineINF 0x3f3f3f3f#defineN 100010#defineFILL (A, B) (Memset (A,b,sizeof (a)))#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1using namespacestd;Const intMaxnode=500010;Const intmaxn=1010;Const intmaxm=510;intK;structdlx{intn,m,size; intU[maxnode],d[maxnode],r[maxnode],l[maxnode],row[maxnode],col[maxnode]; intH[MAXN],S[MAXM]; intANSD,ANS[MAXN]; voidInitint_n,int_m) {N=_n;m=_m; for(intI=0; i<=m;i++) {S[i]=0; U[i]=d[i]=i; L[i]=i-1; R[i]=i+1; } R[m]=0; l[0]=m; Size=m; for(intI=1; i<=n;i++) h[i]=-1; } voidLink (intRintc) {++s[col[++size]=c]; Row[size]=R; D[size]=D[c]; U[D[C]]=size; U[size]=C; D[C]=size; if(h[r]<0) h[r]=l[size]=r[size]=size; Else{R[size]=R[h[r]]; L[R[H[R] ]=size; L[size]=H[r]; R[H[R]]=size; } } voidRemove (intc) { for(inti = d[c];i! = C;i =D[i]) L[r[i]]= L[i], r[l[i]] =R[i]; } voidResume (intc) { for(inti = u[c];i! = C;i =U[i]) L[r[i]]=r[l[i]]=i; } BOOLVis[maxnode]; inth () {intres=0; for(intc=r[0];c!=0; C=r[c]) vis[c]=true; for(intc=r[0];c!=0; c=R[c])if(Vis[c]) {res++; VIS[C]=false; for(intI=d[c];i!=c;i=D[i]) for(intj=r[i];j!=i;j=R[j]) vis[col[j]=false; } returnRes; } BOOLDance (intD//d is recursive depth { if(D+h () >k)return false; if(r[0]==0)//Find the solution returnd<=K; //find the smallest C column in S intc=r[0];//first non-deleted column for(inti=r[0];i!=0; i=R[i])if(S[i]<s[c]) c=i; for(intI=d[c];i!=c;i=d[i])//Overwrite column C with the row where node I is located{Remove (i); for(intJ=R[I];J!=I;J=R[J]) Remove (j);//Delete node I row overrides column C if(Dance (d+1))return true; for(intJ=L[I];J!=I;J=L[J]) Resume (j);//RecoveryResume (i); } return false; }};D lx G;structnode{LL x, y;} C[MAXN]; LL Dist (node A,node b) {return(LL) (LL) ABS (a.x-b.x) + (LL) ABS (a.y-b.y));}intMain () {intt,n,cas=1; intMnx,mxx,mny,mxy; scanf ("%d",&T); while(t--) {scanf ("%d%d",&n,&k); for(intI=1; i<=n;i++) scanf ("%i64d%i64d",&c[i].x,&c[i].y); LL L=0, r=1000000000000ll,ans; while(l<=r) {LL mid= (l+r)/2; G.init (N,n); for(intI=1; i<=n;i++) for(intj=1; j<=n;j++) if(Dist (c[i],c[j]) <=mid) G.link (I,J); if(G.dance (0)) r=mid-1, ans=mid; ElseL=mid+1; } printf ("Case #%d:%i64d\n", cas++, ans); }}
View Code
hdu5046 (Repeat overlay + two points)