Portal: Rain on your Parade
Test instructions: T starts raining after the unit time, gives you the location of n visitors (within a one-dimensional coordinate plane) and his moving speed, and then gives the position of the M-umbrella, asking how many people can get an umbrella before it rains (two people do not share an umbrella).
Analysis: The Hungarian algorithm, but to learn the HOPCROFT-CARP algorithm, the complexity of O (sqrt (V) *e), the algorithm in advance to find a good set of augmented paths, to avoid a lot of matches.
#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 100000000#defineINF 0x3f3f3f3f#defineEPS 1e-6#defineN 10010#defineFILL (A, B) (Memset (A,b,sizeof (a)))#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1#definePII pair<int,int>using namespacestd;intDx[n],dy[n],matchx[n],matchy[n];intVis[n],dis,n,m,t;vector<int>G[n];structnode{intx, Y, Z;} S[n];BOOLSearch_path () {Queue<int>que; Dis=inf; FILL (DX,-1); FILL (dy,-1); for(intI=1; i<=n;i++) { if(matchx[i]==-1) {Que.push (i); Dx[i]=0; } } while(!Que.empty ()) { intu=Que.front (); Que.pop (); if(Dx[u]>dis) Break; for(intI=0, Sz=g[u].size (); i<sz;i++) { intv=G[u][i]; if(dy[v]==-1) {Dy[v]=dx[u]+1; if(matchy[v]==-1) dis=Dy[v]; Else{Dx[matchy[v]]=dy[v]+1; Que.push (Matchy[v]); } } } } returndis!=inf;}intDfsintu) { for(intI=0, Sz=g[u].size (); i<sz;i++) { intv=G[u][i]; if(!vis[v]&&dy[v]==dx[u]+1) {Vis[v]=1; if(matchy[v]!=-1&&dy[v]==dis)Continue; if(matchy[v]==-1||DFS (Matchy[v])) {Matchy[v]=u; Matchx[u]=v; return 1; } } } return 0;}intHK () {intres=0; FILL (Matchx,-1); FILL (matchy,-1); while(Search_path ()) {FILL (Vis,0); for(intI=1; i<=n;i++) if(matchx[i]==-1&&dfs (i)) res++; } returnRes;}BOOLJudgeintAintBintXintYintz) { return(a-x) * (a-x) + (b-y) * (b-y) <= (z*t) * (z*t);}intMain () {intt,cas=1; scanf ("%d",&T); while(t--) {scanf ("%d%d",&t,&N); for(intI=1; i<=n;i++) g[i].clear (); for(intI=1; i<=n;i++) scanf ("%d%d%d",&s[i].x,&s[i].y,&s[i].z); scanf ("%d",&m); for(intI=1; i<=m;i++) { intx, y; scanf ("%d%d",&x,&y); for(intj=1; j<=n;j++) { if(judge (x,y,s[j].x,s[j].y,s[j].z)) g[j].push_back (i); } } intans=HK (); printf ("Scenario #%d:\n%d\n", cas++, ans); Puts (""); }}
View Code
hdu2389 (HK algorithm)