Some R × C 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,
RowSum(i) = ∑
C
j=1
Aij
CumulativeRowSum(i) = ∑
i
k=1
∑
C
j=1
Akj
ColumnSum(i) = ∑
R
j=1
Aji
CumulativeColumnSum(i) = ∑
i
k=1
∑
R
j=1
Ajk
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
2
3 4
10 31 58
10 20 37 58
3 4
10 31 58
10 20 37 58
Sample Output
Matrix 1
1 6 1 2
1 2 2 16
8 2 14 3
Matrix 2
1 1 1 7
1 1 7 12
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,
RowSum(i) = ∑
C
j=1
Aij
CumulativeRowSum(i) = ∑
i
k=1
∑
C
j=1
Akj
ColumnSum(i) = ∑
R
j=1
Aji
CumulativeColumnSum(i) = ∑
i
k=1
∑
R
j=1
Ajk
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
2
3 4
10 31 58
10 20 37 58
3 4
10 31 58
10 20 37 58
Sample Output
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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
最大流~
先计算每个横纵坐标上的和,然后建立源点0,汇点n+m+1,从原点向所有x坐标连边,容量为l[i]-m,从所有y坐标向汇点连边,容量为r[i]-n,从每个横坐标向纵坐标连边,容量为19,最后求出最大流,加1即可~
这题是special judge,样例没有什么参考价值,自己算一下就好了~
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;
int t,n,m,r[21],l[21],cnt,totnumans,a[51][51],ans[51][51],tot,dis[51],now;
bool bfs()
{
memset(dis,0,sizeof(dis));
queue<int> q;q.push(0);dis[0]=1;
while(!q.empty())
{
int k=q.front();q.pop();
for(int i=1;i<=tot;i++)
if(a[k][i]>0 && !dis[i])
{
dis[i]=dis[k]+1;q.push(i);
if(i==tot) return 1;
}
}
return 0;
}
int findd(int u,int v)
{
if(u==tot) return v;
int kkz;
for(int i=0;i<=tot;i++)
if(a[u][i]>0 && dis[i]==dis[u]+1 && (kkz=findd(i,min(v,a[u][i]))))
{
a[u][i]-=kkz;ans[u][i]+=kkz;
a[i][u]+=kkz;ans[i][u]-=kkz;
return kkz;
}
return 0;
}
int main()
{
scanf("%d",&t);
while(t--)
{
memset(a,0,sizeof(a));
memset(ans,0,sizeof(ans));
scanf("%d%d",&n,&m);tot=n+m+1;
for(int i=1;i<=n;i++)
{
scanf("%d",&r[i]);now=r[i]-r[i-1];a[0][i]+=now-m;
}
for(int i=1;i<=m;i++)
{
scanf("%d",&l[i]);now=l[i]-l[i-1];a[i+n][tot]+=now-n;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) a[i][j+n]+=19;
while(bfs()) while(findd(0,999999999));
printf("Matrix %d\n",++totnumans);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++) printf("%d ",ans[i][j+n]+1);printf("\n");
}
if(t) printf("\n");
}
return 0;
}