1458: Soldiers occupy time limit:10 Sec Memory limit:64 MB
submit:782 solved:456
[Submit] [Status] [Discuss] Description
There is a board of M * N, and some lattices are obstacles. Now you have to choose some of the squares to put some soldiers in, a lattice can be placed up to a soldier, the barrier cannot be placed soldiers. We call these soldiers to occupy the entire chessboard. When the line I was satisfied at least the Li soldiers were placed, and the first column J had at least CJ soldiers. Your task now is to require a minimum number of soldiers to occupy the entire board.
Input
The first line, two numbers m, N, K, respectively, represents the number of rows, columns, and obstacles of the board. The second line has an M number indicating li. The third line has n number representing CI. Next there are k lines, two x per line, and Y for (X, y) This lattice is a barrier.
Output
Output a number indicating the minimum number of soldiers to use. No matter how many soldiers are placed, there is no way to capture the entire chessboard, output "jiong!" (without quotation marks)
Sample Input4 4 4
1 1 1 1
0 1 0 3
1 4
2 2
3 3
4 3
Sample Output4
Data range
M, N <=, 0 <= K <= m * N
Hintsourcesolution
Admire Huang Seniors ....
Transform the topic, use the fewest, convert to delete the most, then the maximum flow can be solved
For a row or a column, if the number of places that can be placed is less than the number that must be put, then direct jiong!
So for the source to each row, the capacity is the number of squares that can be placed-the number of required squares
For each column to the connecting edge, the capacity is the same as
Connect to a column from a row that can be placed, with a capacity of 1
Code
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>using namespacestd;intRead () {intx=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();} while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;}#defineMAXM 20000#defineMAXN 500intN,m,k;intl[ the],c[ the];intzt[ the][ the];inthang[ the],lie[ the],ans,tot,s,t;structedgenode{intNext,to,cap;} EDGE[MAXM];intHead[maxn],cnt=1;voidAddintUintVintW) {CNT++;edge[cnt].to=v;edge[cnt].next=head[u];head[u]=cnt;edge[cnt].cap=W;}voidInsertintUintVintW) {Add (u,v,w); Add (V,u,0);}//intQ[MAXN],DIS[MAXN],CUR[MAXN];BOOLBFs () { for(intI=s; i<=t; i++) dis[i]=-1; intHe=0, Ta=1; q[0]=s; dis[s]=0; while(he<ta) { intnow=q[he++]; for(intI=head[now]; I I=edge[i].next)if(Edge[i].cap && dis[edge[i].to]==-1) {dis[edge[i].to]=dis[now]+1; Q[ta++]=edge[i].to; } } returndis[t]!=-1;}intDfsintLocintLow ) { if(loc==t)returnLow ; intW,used=0; for(intI=cur[loc]; I I=edge[i].next)if(Edge[i].cap && dis[edge[i].to]==dis[loc]+1) {W=dfs (Edge[i].to,min (low-used,edge[i].cap)); Edge[i].cap-=w; edge[i^1].cap+=W; Used+=w;if(EDGE[I].CAP) cur[loc]=i; if(Used==low)returnLow ; } if(!used) dis[loc]=-1; returnused;}#defineINF 0x7fffffffintDinic () {inttmp=0; while(BFS ()) { for(intI=s; i<=t; i++) cur[i]=Head[i]; TMP+=DFS (S,inf); } returntmp;}voidMake () {S=0, t=n+m+1; for(intI=1; i<=m; i++) Insert (S,i,n-l[i]-Hang[i]); for(intI=1; i<=n; i++) Insert (i+m,t,m-c[i]-lie[t]); for(intI=1; i<=m; i++) for(intj=1; j<=n; J + +) if(!zt[i][j]) insert (i,j+m,1); }intMain () {m=read (), N=read (), k=read (); for(intI=1; i<=m; i++) l[i]=read (); for(intI=1; i<=n; i++) c[i]=read (); Tot=n*m-K; for(intX,y,i=1; i<=k; i++) x=read (), Y=read (), hang[x]++,lie[y]++,zt[x][y]=1; for(intI=1; i<=m; i++) if(Hang[i]>n-l[i]) {Puts ("jiong!");return 0;} for(intI=1; i<=n; i++) if(Lie[i]>m-c[i]) {Puts ("jiong!");return 0;} Make (); Ans=Dinic (); printf ("%d\n", tot-ans); return 0;}
That's great. At first I didn't think ... See this grid diagram, just want black and white dyeing qaq
"BZOJ-1458" soldiers occupy maximum flow