UVa11082 Matrix decompressing (minimum cost maximum flow)

Source: Internet
Author: User

The topic probably has a n*m matrix, the prefixes of all the numbers in each row are known, and the prefix of all the numbers in each column, and the number of matrices is within the range of 1 to 20, one possible condition of the matrix is obtained.

The weakening version of POJ2396. The key to building the map is:

    • Consider rows and columns as points, and each unit as an edge

This building is very ingenious.

Each unit has a nether limit. I don't want to write the maximum flow below the bounds.

Thought, and found that the minimum cost of the maximum flow can be solved: by amplifying the cost of the edge, so that the edge becomes a must be passed by the edge.

In simple terms, the sides of each unit are split into two, a capacity of 1 costs-1, the other a capacity of 19 costs 0. This runs MCMF, obviously in order to minimize the cost, all costs-1 of the edge will inevitably pass, so that the value of each unit is at least 1.

(In fact, there is a simpler way to do this is to subtract 1 from all cells.) )

1#include <cstdio>2#include <cstring>3#include <queue>4#include <algorithm>5 using namespacestd;6 #defineMAXN 447 #defineMAXM 88*888 #defineINF (1&LT;&LT;30)9 Ten structedge{ One     intV,cap,cost,next; A }EDGE[MAXM]; - intVS,VT,NV,NE,HEAD[MAXN]; - voidAddedge (intUintVintCapintCost ) { theEdge[ne].v=v; Edge[ne].cap=cap; edge[ne].cost=Cost ; -Edge[ne].next=head[u]; head[u]=ne++; -Edge[ne].v=u; edge[ne].cap=0; edge[ne].cost=-Cost ; -EDGE[NE].NEXT=HEAD[V]; head[v]=ne++; + } -  + intD[MAXN],PRE[MAXN]; A BOOLINQUE[MAXN]; at BOOLSPFA () { -      for(intI=0; i<nv; ++i) { -D[i]=inf; inque[i]=0; -     } -d[vs]=0; inque[vs]=1; -queue<int>que; in Que.push (VS); -      while(!Que.empty ()) { to         intu=Que.front (); Que.pop (); +          for(intI=head[u]; i!=-1; I=Edge[i].next) { -             intv=edge[i].v; the             if(Edge[i].cap && d[v]>d[u]+edge[i].cost) { *d[v]=d[u]+Edge[i].cost; $pre[v]=i;Panax Notoginseng                 if(!Inque[v]) { -inque[v]=1; the Que.push (v); +                 } A             } the         } +inque[u]=0; -     } $     returnd[vt]!=INF; $ } - intMxflow; - intMCMF () { themxflow=0; -     intres=0;Wuyi      while(SPFA ()) { the         intflow=inf,cost=0; -          for(intU=VT; U!=vs; u=edge[pre[u]^1].v) { Wuflow=min (flow,edge[pre[u]].cap); -         } Aboutmxflow+=flow; $          for(intU=VT; U!=vs; u=edge[pre[u]^1].v) { -edge[pre[u]].cap-=flow; -edge[pre[u]^1].cap+=flow; -cost+=Edge[pre[u]].cost; A         } +res+=cost*flow; the     } -     returnRes; $ } the  the introw[ A],col[ A],ans[ A][ A]; the intMain () { the     intt,n,m; -scanf"%d",&t); in      for(intCse=1; cse<=t; ++CSE) { thescanf"%d%d",&n,&m); thevs=0; vt=n+m+1; nv=vt+1; Ne=0; Aboutmemset (head,-1,sizeof(head)); the          for(intI=1; i<=n; ++i) { thescanf"%d", row+i); theAddedge (vs,i,row[i]-row[i-1],0); +         } -          for(intI=1; i<=m; ++i) { thescanf"%d", col+i);BayiAddedge (i+n,vt,col[i]-col[i-1],0); the         } the          for(intI=1; i<=n; ++i) { -              for(intj=1; j<=m; ++j) { -Addedge (I,j+n,1,-1); theAddedge (I,j+n, +,0); the             } the         } the MCMF (); -memset (ans,0,sizeof(ans)); the          for(intu=1; u<=n; ++u) { the              for(intI=head[u]; i!=-1; I=Edge[i].next) { the                 if(i&1)Continue;94                 intv=edge[i].v-N; theans[u][v]+=edge[i^1].cap; the             } the         }98         if(cse!=1) Putchar ('\ n'); Aboutprintf"Matrix%d\n", CSE); -          for(intI=1; i<=n; ++i) {101              for(intj=1; j<=m; ++j) {102printf"%d", Ans[i][j]);103             }104Putchar ('\ n'); the         }106     }107     return 0;108}

UVa11082 Matrix decompressing (minimum cost maximum flow)

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.