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. SQL探险

    两张表,取相同字段比较 相同则显示true  否则FALSE.

  2. WampServer下如何实现多域名配置(虚拟域名配置)

    之前在学习跨域的时候,我写过一篇叫做WampServer下使用多端口访问的文章,默认的 localhost 采用的是 80 端口,能使用多端口访问的核心是得新建一个端口,也就是新建一个 http 服务 ...

  3. Angular实现瀑布流的库angular-deckgrid

    一. 安装 bower install --save angular-deckgrid 添加代码到你的HTML 添加到你的angular模块中: angular.module('your.module ...

  4. GBPR: Group Preference Based Bayesian Personalized Ranking for One-Class Collaborative Filtering-IJACA 2013_20160421

    1.Information publication:IJACA 2013 2.What 基于BPR模型的改进:改变BPR模型中,a,用户对商品喜好偏序对之间相互独立;b,用户之间相互独立的假设 原因: ...

  5. jq mobile非ajax加载,ready执行两次

    jqm只有通过ajax加载的页面才只执行一次ready(正常情况) 页面刷新(同非ajax加载的页面)都会执行两次ready,包括pageinit和pageshow事件也是如此. 两种避免的方法是: ...

  6. model 的验证

    Ext.onReady(function(){ Ext.define('User', { extend: 'Ext.data.Model', fields: [ { name: 'name', typ ...

  7. Linux IO Scheduler(Linux IO 调度器)

    每个块设备或者块设备的分区,都对应有自身的请求队列(request_queue),而每个请求队列都可以选择一个I/O调度器来协调所递交的request.I/O调度器的基本目的是将请求按照它们对应在块设 ...

  8. [转]看懂ExtJS的API

    原文地址:http://www.cnblogs.com/youring2/archive/2013/03/05/2944004.html ExtJS的功能很强大,相应的其API也很庞大,并且看起来并不 ...

  9. [转]html js中name和id的区别和使用分析

    js中web页面元素的调用可以有两种识别方法:id和name 自己在用的过程中总结一下id和name的使用区别. 一,使用范围 除 BASE, HEAD, HTML, META, SCRIPT, ST ...

  10. git rebase 和 reset的区别

    check the command detail by input 'git command --help' rebase: reset: