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.
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
题意:对于一个R行C列的正整数矩阵,(1<=R,C<=20),设a[i]为前i行所有元素之和,b[i]为前i列所有元素之和,已知道R,C,和数组a,b,找一个满足条件的矩阵,矩阵中的元素必须是1~20之间的正整数,输入保证有解。
这道题建图就花了一个钟头。
开始的思路:
根据a,b数组求出每一行的元素之和a,每一列的元素之和b
建一个源点s=0,汇点t=R+C+1
然后每一行看成一个顶点1~R,每一列看成一个顶点R+1~R+C
矩阵中每一个位置看成是一条边,比如2行3列的点,就是连接第2行和第3列的点的边。
然后从s到每一行建一条边,容量为a[i],
从每一列到t建一条边,容量为b[i]
然后每一行的点向每一列的点建边,容量为20(暂且说是20,继续看下去,20是错的,19才是对的)
这样的图的意义:
整个矩阵的和代表总的流量
从s出发,分别流向R行(所以有a[1]+...+a[R]=总流量)
每一行的流量会分给该行的C个元素,每一个列的顶点会从R行获得总的流量,就是该列的流量,最后流到t的总流量还是s出发的流量。
所以行的顶点和列的顶点的R*C条边流过的流量就代表矩阵中R*C个位置的值。
因为题目要求矩阵的位置的值在1~20之间,
所以R*C条边的流量应该在1~20之间,所以R*C条边的最大流量即容量就是20.
但是这样是不对的,如果一条边没有流量经过,则流量为0,代表对应的点的值为0,但是题目要求>=1
那么我们就要保证这R*C条边都有流量经过,并且最大流量<=20
怎么保证呢?
由于a[i]=C个值的和,则a[i]>=C
那我们可以先把C的流量分给这C条边,这样就保证了每条边的流量至少为1
剩余的流量a[i]-C再根据最大流找增广路的过程进行分配。
那么每一条边的容量为20,已经有1的流量经过了,现在的容量是19了。
所以在建R*C条边的时候,前面说是20,是不对的,应该是19
最后矩阵每一位置的值+=1
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<vector> 5 #include<queue> 6 7 using namespace std; 8 9 const int maxn=23; 10 const int inf=0x3f3f3f3f; 11 12 int R,C; 13 int a[maxn]; 14 int b[maxn]; 15 int aa[maxn]; 16 int bb[maxn]; 17 int s,t; 18 int level[50]; 19 int id[50]; 20 int cost[maxn][maxn]; 21 22 int ans[maxn][maxn]; 23 24 struct Edge 25 { 26 int to,cap,rev; 27 }; 28 29 vector<Edge>edge[500]; 30 31 void addedge(int from,int to,int cap) 32 { 33 edge[from].push_back((Edge){to,cap,edge[to].size()}); 34 edge[to].push_back((Edge){from,0,edge[from].size()-1}); 35 } 36 37 void bfs() 38 { 39 memset(level,-1,sizeof(level)); 40 queue<int>que; 41 while(!que.empty()) 42 que.pop(); 43 que.push(s); 44 level[s]=0; 45 while(!que.empty()) 46 { 47 int u=que.front(); 48 que.pop(); 49 for(int i=0;i<edge[u].size();i++) 50 { 51 Edge &e=edge[u][i]; 52 if(e.cap>0&&level[e.to]<0) 53 { 54 level[e.to]=level[u]+1; 55 que.push(e.to); 56 } 57 } 58 } 59 } 60 61 int dfs(int u,int f) 62 { 63 if(u==t) 64 return f; 65 for(int &i=id[u];i<edge[u].size();i++) 66 { 67 Edge &e=edge[u][i]; 68 if(e.cap>0&&level[e.to]>level[u]) 69 { 70 int d=dfs(e.to,min(f,e.cap)); 71 if(d>0) 72 { 73 e.cap-=d; 74 edge[e.to][e.rev].cap+=d; 75 return d; 76 } 77 } 78 } 79 return 0; 80 } 81 82 void solve() 83 { 84 while(true) 85 { 86 bfs(); 87 if(level[t]<0) 88 break; 89 int flow; 90 memset(id,0,sizeof(id)); 91 while(flow=dfs(s,inf)>0) 92 { 93 ; 94 } 95 } 96 97 for(int i=1;i<=R;i++) 98 { 99 for(int j=1;j<=C;j++) 100 { 101 ans[i][j]=19-edge[i][cost[i][j]].cap+1; 102 } 103 } 104 return ; 105 } 106 107 108 int main() 109 { 110 int cas=1; 111 int test; 112 scanf("%d",&test); 113 while(test--) 114 { 115 if(cas>1) 116 printf("\n"); 117 printf("Matrix %d\n",cas++); 118 119 scanf("%d%d",&R,&C); 120 for(int i=1;i<=R;i++) 121 scanf("%d",&aa[i]); 122 for(int i=1;i<=C;i++) 123 scanf("%d",&bb[i]); 124 aa[0]=bb[0]=0; 125 for(int i=1;i<=R;i++) 126 { 127 a[i]=aa[i]-aa[i-1]; 128 } 129 for(int i=1;i<=C;i++) 130 { 131 b[i]=bb[i]-bb[i-1]; 132 } 133 134 s=0; 135 t=C+R+1; 136 137 for(int i=0;i<500;i++) 138 edge[i].clear(); 139 140 for(int i=1;i<=R;i++) 141 { 142 addedge(s,i,a[i]-C); 143 } 144 for(int i=1;i<=C;i++) 145 { 146 addedge(i+R,t,b[i]-R); 147 } 148 149 for(int i=1;i<=R;i++) 150 { 151 for(int j=1;j<=C;j++) 152 { 153 cost[i][j]=edge[i].size(); 154 addedge(i,j+R,19); 155 } 156 } 157 158 solve(); 159 160 for(int i=1;i<=R;i++) 161 { 162 for(int j=1;j<C;j++) 163 { 164 printf("%d ",ans[i][j]); 165 } 166 printf("%d\n",ans[i][C]); 167 } 168 } 169 return 0; 170 }