Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=4888
Test instructions: Give a matrix of rows and each column and, ask whether the matrix can be restored, if the solution is unique, if the only output of the matrix.
Idea: Set a source point and a meeting point, each row and the source point plus edge, the weight of the row and, each column and the sink point dot, the weight of the column and.
Each row and column plus edges, the weight is K, runs the maximum flow, if the full stream (from the source point is equal to the incoming sink point) proves that the matrix can be restored.
To determine whether there are multiple sections, DFS search, to see if there is a ring in the residual network, no ring is the only solution.
AC Code:
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intoo=1e9;Const intmm=4e5+5;Const intmn=1e5+5;intNode,src,dest,edge;intver[mm],flow[mm],next[mm];intHEAD[MN],WORK[MN],DIS[MN],Q[MN];intMin (intAintb) { returnA<b?a:b;}voidPrepareint_node,int_SRC,int_dest) {Node=_node,src=_src,dest=_dest; for(intI=0; i<node; ++i) head[i]=-1; Edge=0;}voidAddedge (intUintVintc) {Ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++; Ver[edge]=u,flow[edge]=0, next[edge]=head[v],head[v]=edge++;}BOOLDinic_bfs () {intI,u,v,l,r=0; for(i=0; i<node; ++i) dis[i]=-1; Dis[q[r++]=src]=0; for(l=0; l<r; ++l) for(I=head[u=q[l]]; i>=0; I=Next[i])if(flow[i]&&dis[v=ver[i]]<0) {Dis[q[r++]=v]=dis[u]+1; if(v==dest)return 1; } return 0;}intDinic_dfs (intUintexp) { if(u==dest)returnexp; for(int&i=work[u],v,tmp; i>=0; I=Next[i])if(flow[i]&&dis[v=ver[i]]==dis[u]+1&& (Tmp=dinic_dfs (V,min (exp,flow[i))) >0) {Flow[i]-=tmp; Flow[i^1]+=tmp; returntmp; } return 0;}intDinic_flow () {inti,ret=0, Delta; while(Dinic_bfs ()) { for(i=0; i<node; ++i) work[i]=Head[i]; while(Delta=dinic_dfs (Src,oo)) ret+=Delta; } returnret;}intn,m,k;intvit[500005];intmp[505][505];intDfsintUintp) { if(Vit[u])return 1; Vit[u]=1; for(intI=head[u]; i!=-1; I=Next[i]) { intv=Ver[i]; if(V!=p && v!=0&& v!= (n+m+1) &&Flow[i])if(Dfs (V,U))return 1; } Vit[u]=0; return 0;}intMain () { while(SCANF ("%d%d%d", &n,&m,&k) = =3) {Prepare (n+m+2,0, n+m+1); intsum1=0, sum2=0; intx; for(intI=1; i<=n; i++) {scanf ("%d",&x); Sum1+=x; Addedge (0, i,x); for(intj=1; j<=m; J + +) Addedge (i,j+n,k); } for(intI=1; i<=m; i++) {scanf ("%d",&x); Addedge (i+n,n+m+1, x); Sum2+=x; } intans=Dinic_flow (); if(Ans==sum1 && ans==sum2) { intflag=0; memset (Vit,0,sizeof(Vit)); for(intI=1; i<=n; i++) {memset (Vit,0,sizeof(Vit)); if(Dfs (i,-1) ) {flag=1; Break; } } if(flag) puts ("Not Unique"); Else{puts ("Unique"); Memset (MP,0,sizeof(MP)); for(intI=1; i<=n; i++) { for(intJ=head[i]; j!=-1; j=Next[j]) { intv=Ver[j]; if(V>n && v<=n+m) mp[i][v-n]=k-Flow[j]; } } for(intI=1; i<=n; i++) { for(intj=1; j<=m; J + +) { if(j!=1) printf (" "); printf ("%d", Mp[i][j]); } puts (""); } } } ElsePuts"Impossible"); } return 0;}
hdu4888 Redraw Beautiful drawings (maximum flow)