uva11082 Matrix decompressing

Source: Internet
Author: User

Network flow

First, the number of each column in each row is calculated.

The value of each row minus C, the value of each column minus R

Then the edge is connected between each row and each column, with a capacity of 19.

In this way, the (I,J) traffic is equivalent to (I,J) the value-1.

This avoids the awkward situation where the flow is 0 and does not correspond to the answer.

#include <cstdio>#include<algorithm>#include<cstring>using namespacestd;Const intMAXN = ++Ten;Const intMAXM =100000+Ten;Const intINF =0x3f3f3f3f;intG[maxn],v[maxm],nex[maxm],f[maxm],eid;intGap[maxn],d[maxn],vid;intn,m,s,t;intA[MAXN],B[MAXN];intRES[MAXN][MAXN];voidAddedge (intAintBintF) {V[eid]=b; F[eid]=f; Nex[eid]=g[a]; g[a]=eid++; V[eid]=a; f[eid]=0; NEX[EID]=G[B]; g[b]=eid++; }voidbuild () {memset (A,0,sizeof(a)); memset (b,0,sizeof(b)); scanf ("%d%d",&n,&m);  for(intI=1; i<=n;i++) scanf ("%d",&A[i]);  for(inti=n;i>=1; i--) a[i]-=a[i-1];  for(inti=n+1; i<=n+m;i++) scanf ("%d",&B[i]);  for(inti=n+m;i>=n+1; i--) b[i]-=b[i-1]; Memset (g,-1,sizeof(g)); Eid=0; S=n+m+1; t=n+m+2; vid=n+m+2;  for(intI=1; i<=n;i++) {Addedge (S,i,a[i]-m); }     for(inti=n+1; i<=n+m;i++) {Addedge (I,t,b[i]-N); }     for(intI=1; i<=n;i++)      for(intj=n+1; j<=n+m;j++) {Addedge (i,j, +); }}intISAP (intUintflow) {    if(u==t)returnflow; intCur=0, aug,mindist=vid;  for(intI=g[u];~i;i=nex[i])if(F[i] && d[u]==d[v[i]]+1) { in=isap (V[i],min (flow-cur,f[i])); Cur+=; F[i]-=; F[i^1]+=; if(Cur==flow | | d[s]>=vid)returncur; }        if(cur==0) {        if(!--Gap[d[u]]) {D[s]=vid; returncur; }         for(intI=g[u];~i;i=nex[i])if(F[i]) mindist=min (Mindist,d[v[i]]); ++gap[d[u]=mindist+1]; }    returncur;}voidsolve () {memset (d,0,sizeof(d)); Memset (Gap,0,sizeof(GAP)); gap[0]=vid;  while(d[s]<vid) {ISAP (s,inf); }     for(intu=1; u<=n;u++) {         for(intI=g[u];~i;i=nex[i]) res[u][v[i]]= --F[i]; }     for(intI=1; i<=n;i++) {         for(intj=n+1; j<n+m;j++) printf ("%d", Res[i][j]); printf ("%d\n", res[i][n+m]); }}intMain () {intT; scanf ("%d",&T);  for(intI=1; i<=t;i++) {        if(i>1) printf ("\ n"); printf ("Matrix%d\n", i);        Build ();    Solve (); }    return 0;}

uva11082 Matrix decompressing

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.