The main idea: given a 1 to 20 of the integer matrix of each row and each column of the and, constructs the matrix. Output any one of the construction schemes.
Title analysis: Treat each row as a point x and treat each column as a point y. For each lattice in the matrix, it corresponds to a two-yuan relationship <x,y>, and from X to a arc to Y, the capacity is set to 19. Increase the source point S and the sink point T, for each x, even a direction arc from S to X, the capacity is set to the corresponding sum of the row minus the number of columns, for each y, a direction arc from Y to T, the capacity is set to the corresponding sum of the column minus the number of rows. For this to find the maximum flow, the algorithm terminates, if each arc starting from S and each arc to reach T is saturated, then each XI to YJ traffic is the corresponding lattice value minus one; otherwise, no solution. There must be a solution to this problem. Obviously, this modeling is correct.
The code is as follows:
# include<iostream># include<cstdio># include<cmath># include<string># include<vector ># include<list># include<set># include<map># include<queue># include<cstring># Include<algorithm>using namespace std;# define LL long long# define REP (i,s,n) for (int i=s;i<n;++i) # define CL (A, b) memset (A,b,sizeof (a)) # define CLL (A,b,n) Fill (a,a+n,b) const double inf=1e30;const int Inf=1<<30;const int n= 1000;struct edge{int fr,to,cap,flow; Edge (int _fr,int _to,int _cap,int _flow): fr (_fr), to (_to), Cap (_CAP), Flow (_flow) {}};vector<edge>edge;int id[50] [50],r,c,a[25],b[25],f[50],p[50];vector<int>g[50];void init () {a[0]=b[0]=0; REP (i,0,r+c+2) g[i].clear (); Edge.clear ();} void Addedge (int fr,int to,int cap) {Edge.push_back (Edge (fr,to,cap,0)); Edge.push_back (Edge (to,fr,0,0)); int m=edge.size (); Id[fr][to]=m-2; G[fr].push_back (m-2); G[to].push_back (m-1);} void Maxflow (int s,int t) {while (1) {queue<int>q; CL (f,0); F[s]=inf; Q.push (s); while (!q.empty ()) {int U=q.front (); Q.pop (); REP (I,0,g[u].size ()) {Edge &e=edge[G[u][i]]; if (!f[e.to]&&e.cap>e.flow) {p[e.to]=g[u][i]; F[e.to]=min (F[u],e.cap-e.flow); Q.push (e.to); }} if (F[t]) break; } if (!f[t]) break; for (int u=t;u!=s;u=edge[p[u]].fr) {edge[p[u]].flow+=f[t]; EDGE[P[U]^1].FLOW-=F[T]; }}}int Main () {int t,cas=0; scanf ("%d", &t); while (t--) {scanf ("%d%d", &r,&c); Init (); REP (i,1,r+1) scanf ("%d", a+i); REP (i,1,c+1) scanf ("%d", b+i); REP (i,1,r+1) Addedge (0,I,A[I]-A[I-1]-C); Rep (i,1,r+1) Rep (j,r+1,r+c+1) Addedge (i,j,19); REP (i,r+1,r+c+1) Addedge (i,r+c+1,b[i-r]-b[i-r-1]-r); Maxflow (0,r+c+1); printf ("Matrix%d\n", ++cas); Rep (i,1,r+1) Rep (j,1,c+1) printf ("%d%c", Edge[id[i][j+r]].flow+1, (j==c)? ' \ n ': '); if (T) printf ("\ n"); } return 0;}
UVA-11082 Matrix decompressing (Network flow modeling)