HDU 4888 Redraw Beautiful drawings (solution of network flow seeking matrix)

Source: Internet
Author: User

The paper "Why many network flow problems always have integer solution" http://diaorui.net/archives/189;

Reference: http://www.cnblogs.com/yuiffy/p/3929369.html

Test instructions: The matrix of the n*m, giving each row and each column of the and, to determine whether such a matrix exists, if present, whether the unique, if only, the output solution;

Idea: network flow, maximum flow + judgment ring. Network flow is often used to find polynomial integer solutions.

#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intinf=0x7fffffff;BOOLfound,walked[500010];intT,nr,nc,k,sumr,sumc,ans;intr[500010],c[500010];intan[5005][5005];inthead[500100],h[500010],g[500010];//G[i] is the number of nodes of layer I, h[i] is the number of layers of I pointintd[500010],AUGC;//d record current arc, AUGC for augmented path capacityintCnt,st,ed,flow,n,m;//n points m edge, flow is the maximum streamstructnode{intV,cap,next;}; Node e[500010];voidAddintXintYintz) {E[CNT].V=y; E[cnt].cap=Z; E[cnt].next=Head[x]; HEAD[X]=cnt++; E[CNT].V=x; E[cnt].cap=0; E[cnt].next=Head[y]; Head[y]=cnt++;}BOOLDfsConst int&x,Const int&prex)//Award Ring{    intbiu=-1; WALKED[X]=true;  for(inti=head[x];i!=-1; i=E[i].next) {        if(e[i].v==Prex) {Biu=i;Continue; }        if(e[i].cap>0)        {            if(WALKED[E[I].V])return true; if(Dfs (E[I].V,X))return true; }        if(biu==-1) Head[x]=e[i].next;//There's no ring on this side of the line .        Elsee[biu].next=E[i].next; Biu=i; } Walked[x]=false; return false;}voidAugConst int&AMP;M)//Dicnic{    inti,mini,minh=n-1; intaugco=AUGC; if(m==ed) {//current point is a meeting pointFound=true; Flow+=AUGC;//Increase Traffic        return; }     for(i=d[m];i!=-1; i=e[i].next) {//find the permissible side        if(e[i].cap&&h[e[i].v]+1==H[M])//if the residue is greater than 0 and is a permissible edge        {            if(E[I].CAP&LT;AUGC) Augc=e[i].cap;//if the residue is less than the current augmented path flow, the new wide-path trafficD[m]=i;//set I as the current arc(E[I].V);//Recursive            if(h[st]>=n)return;//returns if the source point layer is greater than n            if(found) Break;//find a meeting point, jump outAugc=augco;//Restore current stream if not found        }    }    if(!found) {         for(i=head[m];i!=-1; i=E[i].next) {            if(e[i].cap&&h[e[i].v]<Minh) {Minh=H[E[I].V]; Mini=i; }} G[h[m]]--; if(!g[h[m]]) h[st]=N; H[M]=minh+1; D[M]=Mini; G[H[M]]++; }Else{//Modify the residual quantityE[I].CAP-=AUGC;//Positive Reductione[i^1].CAP+=AUGC;//Reverse Add    }}voidFarm () {inti,j,x,y,z; memset (Head,-1,sizeof(head)); CNT=0; N=nc+nr+2; St=nc+nr+1;//Source Pointed=nc+nr+2;//meeting Point     for(i=1; i<=nc;i++) Add (St,i,c[i]); //source point to line edge, capacity for the row and     for(i=1; i<=nr;i++) Add (NC+i,ed,r[i]);//column to a meeting point edge, and the capacity for the column and     for(i=1; i<=nc;i++)         for(j=1; j<=nr;j++) Add (i,j+NC,K);//row-to-column edge, capacity Kmemset (H,0,sizeof(h));//number of layers, building a hierarchical mapMemset (G,0,sizeof(g)); g[0]=N; Flow=0;  for(i=1; i<=n;i++) D[i]=head[i];//Current ARC Initialization     while(h[st]<N) {AUGC=inf;//The capacity of the initial augmented path is infinitely largeFound=false;       (ST); //from the source point to start looking for    }    if(FLOW!=SUMR) {//not up to full streamans=0; return; }     for(i=1; i<=nr;i++)    {        intk=1;  for(j=head[nc+i];j!=-1; j=E[j].next) {            if(e[j].v==ed)Continue; An[i][k++]=E[j].cap; intthenext=E[j].next;  while(thenext!=-1&&e[thenext].v==ed) thenext=E[thenext].next; }} memset (walked,false,sizeof(walked));  for(i=nr;i>=1; i--)    {        if(Dfs (i,-1))//the number of rows as the starting position of the ring, if there is a ring, 1~NR must be a bit in the ring{ans=2; return; }} ans=1;}intMain () {intI,j,cas;  while(SCANF ("%d%d%d", &nr,&nc,&k)! =EOF) {SUMR=sumc=0;  for(i=1; i<=nr;i++) {scanf ("%d",&R[i]); SUMR+=R[i]; }         for(i=1; i<=nc;i++) {scanf ("%d",&C[i]); SUMC+=C[i]; } ans=0; if(SUMR==SUMC) farm ();//and different, then no solution        if(ans==0) printf ("impossible\n"); Else if(ans!=1) {printf ("Not unique\n"); }        Else{printf ("unique\n");  for(i=1; i<=nr;i++)           {               if(nc>=1) printf ("%d", AN[I][NC]);  for(j=nc-1; j>=1; j--) {printf ("%d", An[i][j]); }printf ("\ n"); }        }    }    return 0;}

HDU 4888 Redraw Beautiful drawings (solution of network flow seeking matrix)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.