Problem I
Matrix Decompressing
Input: Standard Input
Output: Standard Output
Some RxC matrix of positive integers is encoded and represented by its R cumulative row sum and C column sum entries. Given, R, C and those R+C cumulative row sum and column sums, you want to decode the matrix (i.e., find all the individual R*C entries).
Here,
Or in other words, the i-th row sum is the sum of all the entries in row i. And the cumulative i-th row sum is the sum of all the row sums from row 1 to row i (inclusive).
Input
There can be multiple test cases. The first line of input contains the number of test cases, T (1 ≤T ≤ 100). Each test case contains 3 lines of input. The first line of the test case gives the size of the matrix: the number of rows, R (1 ≤ R ≤ 20) and the number of columns C (1 ≤ C ≤20). The next line contains all the R cumulative row sums, and the last line of the test case contains the C cumulative column sums. Any two successive numbers in the same line is separated by a single space.
Output
For each test case print the label of the test case in the first line. The format of this label should be “Matrix x” where x would be replaced by the serial number of the test case starting at 1. In each of the following R lines print C integers giving all the individual entries of the matrix. You can assume that there is at least one solution for each of the given encodings. To simplify the problem further, we add the constraint that each entry in the matrix must be an integer between 1 and 20. In case of multiple solutions, you can output any one of them.
Sample Input Output for Sample Input
2 3 4 10 31 58 10 20 37 58 3 4 10 31 58 10 20 37 58 | Matrix 1 1 6 1 2 1 2 2 16 8 2 14 3
Matrix 2 1 1 1 7 1 1 7 12 8 8 9 2
|
Problemsetter: Monirul Hasan
Special Thanks: Sadrul Habib Chowdhury
题意:有一个矩阵,已经知道了前i行的总和和前j列的总和,矩阵中每个位置的范围是1~20,要你找到一个符合要求的矩阵。
思路:虽然看起来是一个有下界和上届的网络流,其实不需要这么麻烦,这道题目等价于每个位置的范围是0~19,对应的行和列的总和只需要处理一下就行了,所以我们有一个二分图,行号构成一个集合,列号构成一个集合,加一个源点连向每个行,容量为该行的总和,每一个列号连向汇点,容量为该列的总和,然后每个行号连向每个列号,容量为19,然后跑一下网络流就行了。最后在对应的矩阵的位置填上对应的流量就行了,注意最后的数字要加1.
代码:
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 20+20+2;
const int inf=1e8;
struct Edge
{
int u,v;
int cap,flow;
Edge(int u,int v,int cap,int flow)
:u(u),v(v),cap(cap),flow(flow) {}
};
vector<Edge> edges;
vector<int> G[maxn];
void add(int u,int v,int cap)
{
edges.push_back(Edge(u,v,cap,0));
edges.push_back(Edge(v,u,0,0));
int m=edges.size();
G[u].push_back(m-2);
G[v].push_back(m-1);
}
struct ISAP
{
int d[maxn],p[maxn],num[maxn],cur[maxn];
int n,s,t;
void init(int n) { this->n=n; }
int Augment()
{
int x=t,a=inf;
while(x!=s)
{
Edge&e=edges[p[x]];
a=min(a,e.cap-e.flow);
x=e.u;
}
x=t;
while(x!=s)
{
edges[p[x]].flow+=a;
edges[p[x]^1].flow-=a;
x=edges[p[x]].u;
}
return a;
}
void bfs()
{
for(int i=0;i<n;++i) d[i]=inf;
queue<int> q;
d[t]=0;
q.push(t);
while(q.size())
{
int x=q.front();q.pop();
for(int i=0;i<G[x].size();++i)
{
Edge&e=edges[G[x][i]];
if(e.cap>0||d[e.v]<=d[x]+1) continue;
d[e.v]=d[x]+1;
q.push(e.v);
}
}
}
int maxflow(int s,int t)
{
this->s=s,this->t=t;
memset(num,0,sizeof(num));
memset(cur,0,sizeof(cur));
bfs();
for(int i=0;i<n;++i)
if(d[i]!=inf) ++num[d[i]];
int flow=0,x=s;
while(d[s]<n)
{
if(x==t)
{
flow+=Augment();
x=s;
}
int ok=0;
for(int i=cur[x];i<G[x].size();++i)
{
Edge&e=edges[G[x][i]];
if(e.cap>e.flow&&d[e.v]+1==d[x])
{
ok=1;
cur[x]=i;
p[e.v]=G[x][i];
x=e.v;
break;
}
}
if(!ok)
{
int m=n-1;
for(int i=0;i<G[x].size();++i)
{
Edge&e=edges[G[x][i]];
if(e.cap>e.flow) m=min(m,d[e.v]);
}
if(--num[d[x]]==0) break;
++num[d[x]=m+1];
cur[x]=0;
if(x!=s) x=edges[p[x]].u;
}
}
return flow;
}
}solver;
int n,m;
int rowsum[maxn];
int colsum[maxn];
void input()
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;++i) scanf("%d",rowsum+i+1);
for(int i=0;i<m;++i) scanf("%d",colsum+i+1);
}
void buildgraph()
{
edges.clear();
for(int i=0;i<maxn;++i) G[i].clear();
for(int i=0;i<n;++i) add(n+m,i,rowsum[i+1]-rowsum[i]-m);
for(int i=0;i<m;++i) add(n+i,n+m+1,colsum[i+1]-colsum[i]-n);
for(int i=0;i<n;++i)
for(int j=0;j<m;++j)
{
add(i,n+j,19);
}
solver.init(n+m+2);
}
int A[30][30];
void solve()
{
int ans=solver.maxflow(n+m,n+m+1);
for(int r=0;r<n;++r)
{
for(int i=0;i<G[r].size();++i)
{
Edge&e=edges[G[r][i]];
int v=e.flow;
int c=e.v-n;
if(v<0||c>=m||c<0) continue;
A[r][c]=e.flow;
}
}
for(int r=0;r<n;++r)
for(int c=0;c<m;++c)
{
if(c) printf(" ");
printf("%d",A[r][c]+1);
if(c==m-1) printf("\n");
}
}
int main()
{
int T;cin>>T;
int Cas=0;
while (T--)
{
input();
buildgraph();
++Cas;
printf("Matrix %d\n",Cas);
solve();
}
}