https://www.bnuoj.com/v3/problem_show.php?pid=19500 (题目链接)

题意

  给出一个R行C列的正整数矩阵,设前${A_i}$项为其前i行所有元素之和,${B_i}$项为其前i列所有元素之和,已知R,C,A,B,找出一个满足条件的矩阵。其中每个元素都是1~20的正整数。

Solution

  看到这类矩阵形的题目,首先就要考虑构造二分图,左集代表行,右集代表列,其中每一条边代表一个点。

  这样构完图后,我们发现可以使用有上下界的网络流来解决这个问题。添加源点向左集连一条边上界为${A_i}$,下界也为${A_i}$;右集上的每个点向额外添加的汇点连一条上界为${B_i}$,下界为${B_i}$的边;左集右集之间每两个点连一条上界为20,下界1的边。

  码了一半,发现其实并不需要上下界,直接将每个元素-1,下界就成了0,因为行之和与列之和值相等的,所以可行流即为最大流。

细节

  邻接表的下标从1开始。

代码

// bnuoj19500
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<queue>
#define LL long long
#define inf 2147483640
#define MOD 10000
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=1010;
struct edge {int to,next,w;}e[maxn<<1];
int head[maxn],d[maxn],r[maxn],c[maxn],a[maxn][maxn];
int n,m,cnt=1,ans; void link(int u,int v,int w) {
e[++cnt]=(edge){v,head[u],w};head[u]=cnt;
e[++cnt]=(edge){u,head[v],0};head[v]=cnt;
}
void Init() {
memset(head,0,sizeof(head));
cnt=1;ans=0;
}
bool bfs() {
memset(d,-1,sizeof(d));
queue<int> q;q.push(1);d[1]=0;
while (!q.empty()) {
int x=q.front();q.pop();
for (int i=head[x];i;i=e[i].next) if (e[i].w && d[e[i].to]<0) {
d[e[i].to]=d[x]+1;
q.push(e[i].to);
}
}
return d[n+m+2]>0;
}
int dfs(int x,int f) {
if (x==n+m+2 || f==0) return f;
int w,used=0;
for (int i=head[x];i;i=e[i].next) if (e[i].w && d[e[i].to]==d[x]+1) {
w=dfs(e[i].to,min(e[i].w,f-used));
used+=w;
e[i].w-=w;e[i^1].w+=w;
if (used==f) return used;
}
if (!used) d[x]=-1;
return used;
}
void Dinic() {
while (bfs()) ans+=dfs(1,inf);
}
int main() {
int T;scanf("%d",&T);
for (int Case=1;Case<=T;Case++) {
Init();
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++) scanf("%d",&r[i]);
for (int i=1;i<=m;i++) scanf("%d",&c[i]);
for (int i=n;i;i--) r[i]-=r[i-1];
for (int i=m;i;i--) c[i]-=c[i-1];
for (int i=1;i<=n;i++) link(1,i+1,r[i]-m);
for (int j=1;j<=m;j++) link(j+n+1,n+m+2,c[j]-n);
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++) link(i+1,j+n+1,19);
Dinic();
printf("Matrix %d\n",Case);
for (int i=1;i<=n;i++)
for (int j=head[i+1];j;j=e[j].next)
if (e[j].to>i) a[i][e[j].to-n-1]=20-e[j].w;
for (int i=1;i<=n;i++) {
for (int j=1;j<=m;j++) printf("%d ",a[i][j]);
puts("");
}
if (Case<T) puts("");
}
return 0;
}

  

【BNUOJ19500】 Matrix Decompressing的更多相关文章

  1. 【UVA11082】Matrix Decompressing(有上下界的网络流)

    题意:给出一个矩阵前i列所有元素的和,和前j行所有元素的和,求这个矩阵解压以后的原型.(答案不唯一) n,m<=20,1<=a[i,j]<=20 思路:这道题把边上的流量作为原先矩阵 ...

  2. 【BZOJ4128】Matrix BSGS+hash

    [BZOJ4128]Matrix Description 给定矩阵A,B和模数p,求最小的x满足 A^x = B (mod p) Input 第一行两个整数n和p,表示矩阵的阶和模数,接下来一个n * ...

  3. 【UVA11019】Matrix Matcher

    Description Given an N × M matrix, your task is to find the number of occurences of an X × Y pattern ...

  4. 【RS】Matrix Factorization Techniques for Recommender Systems - 推荐系统的矩阵分解技术

    [论文标题]Matrix Factorization Techniques for Recommender Systems(2009,Published by the IEEE Computer So ...

  5. 【poj2155】Matrix(二维树状数组区间更新+单点查询)

    Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...

  6. 【uva 11082】Matrix Decompressing(图论--网络流最大流 Dinic+拆点二分图匹配)

    题意:有一个N行M列的正整数矩阵,输入N个前1~N行所有元素之和,以及M个前1~M列所有元素之和.要求找一个满足这些条件,并且矩阵中的元素都是1~20之间的正整数的矩阵.输入保证有解,而且1≤N,M≤ ...

  7. 【数学】Matrix Multiplication

                                 Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total S ...

  8. 【题解】Matrix BZOJ 4128 矩阵求逆 离散对数 大步小步算法

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4128 大水题一道 使用大步小步算法,把数字的运算换成矩阵的运算就好了 矩阵求逆?这么基础的线 ...

  9. 题解【POJ2155】Matrix

    Description Given an \(N \times N\) matrix \(A\), whose elements are either \(0\) or \(1\). \(A[i, j ...

随机推荐

  1. spring mvc总结1

    1,spring下载 spring更改了官方网站后,找了很长时间没有找到相关的jar包下载路径,然后在网上终于找到相关的路径了 有个树形结构可供选择:http://repo.spring.io/rel ...

  2. CodeSmith 使用说明

    〇.            前言 最近两天自己写了个简单的ORM框架,非常的Easy,但是没有相应的代码生成工具,于是就很杯具了! 于是乎,花费了一天的时间学习并写了一个CodeSmith可以使用的模 ...

  3. 浅析jQuery删除节点的三个方法

    jQuery提供了三种删除节点的方法,即remove(),detach()和empty().测试所用HTML代码:[html] view plaincopy<p title="选择你最 ...

  4. Vs2013中通过Nuget添加不同版本jQuery

    vs2013中如果直接更新jQuery则是2X的版本,为了兼容IE浏览器的,一般都是希望使用jQuery1.x版本的jQuery,则可在调出Nuget的控制台,在控制台输入(此例子是下载jQuery ...

  5. 让mysql支持emoji表情

    一.问题及原因 APP产品想对Emoji进行支持,但发现mysql数据库无法写入表情.原因是我们的mysql数据库默认用的是utf8编码,utf8编码存储时用的是三个字节,但Emoji表情是4个字节, ...

  6. ASP.NET Web API 实现客户端Basic(基本)认证 之简单实现

    优点是逻辑简单明了.设置简单. 缺点显而易见,即使是BASE64后也是可见的明文,很容易被破解.非法利用,使用HTTPS是一个解决方案. 还有就是HTTP是无状态的,同一客户端每次都需要验证. 实现: ...

  7. python环境搭建-Pycharm 调整字体大小

  8. 1110Nested Loop Join算法

    转自 http://blog.csdn.net/tonyxf121/article/details/7796657 join的实现原理 join的实现是采用Nested Loop Join算法,就是通 ...

  9. 1010每次备份我的MySQL数据库

    转自http://bbs.csdn.net/topics/320136534 在windows平台下面 /*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/* ...

  10. PHP中生成json信息的方法

    <?php //php中生成json信息 //json_encode(数组/对象) $color = array('red','blue','green'); //[索引数组] echo jso ...