UVa11082 Matrix Decompressing 矩阵解压

传送


题面:一个\(n\)\(m\)列的正整数矩阵\((1\leqslant n, m \leqslant 20)\),设\(A_i\)为前\(i\)行所有元素之和,\(B_i\)为前\(i\)列所有元素之和。已知\(n,m\)和数组\(A\)\(B\),找一个满足条件的矩阵。矩阵中的元素必须是\(1\sim 20\)之间的正整数。输入保证有解。


这道题说白了就是分配问题嘛,我们该如何分配每个元素的权值,使其满足行列的条件。

那这就是一个二分图,左部点是行\(1\sim n\),右部点是列\(n+1\sim n + m + 1\).

从源点向每一行连一条容量为该行元素之和的边,从每一列向汇点连一条容量为该列元素之和的边,而对于任意行列之间,连一条容量为\(20\)的边,因为每个数最大只有\(20\).

然后跑最大流,看行列之间的边的流量是多少即可。

但道题还有个条件,即有流量下界\(1\)的限制,但因为每条边的下界都是\(1\),因此在跑网络流之前减掉即可。

陈老师的代码,直接记录了行列之间的边的编号,输出答案就非常方便,值得学习。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<queue>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
#define forE(i, x, y) for(int i = head[x], y; ~i && (y = e[i].to); i = e[i].nxt)
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 25;
const int maxe = 1e4 + 5;
In ll read()
{
	ll ans = 0;
	char ch = getchar(), las = ' ';
	while(!isdigit(ch)) las = ch, ch = getchar();
	while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
	if(las == '-') ans = -ans;
	return ans;
}
In void write(ll x)
{
	if(x < 0) x = -x, putchar('-');
	if(x >= 10) write(x / 10);
	putchar(x % 10 + '0');
}

int n, m, s, t, id[maxn][maxn];
struct Edge
{
	int nxt, to, cap, flow;
}e[maxe];
int head[maxn << 1], ecnt = -1;
In void addEdge(int x, int y, int w)
{
	e[++ecnt] = (Edge){head[x], y, w, 0};
	head[x] = ecnt;
	e[++ecnt] = (Edge){head[y], x, 0, 0};
	head[y] = ecnt;
}

int dis[maxn << 1];
In bool bfs()
{
	Mem(dis, 0), dis[s] = 1;
	queue<int> q; q.push(s);
	while(!q.empty())
	{
		int now = q.front(); q.pop();
		for(int i = head[now], v; ~i; i = e[i].nxt)
			if(e[i].cap > e[i].flow && !dis[v = e[i].to])
				dis[v] = dis[now] + 1, q.push(v);
	}
	return dis[t];
}
int cur[maxn << 1];
In int dfs(int now, int res)
{
	if(now == t || res == 0) return res;
	int flow = 0, f;
	for(int& i = cur[now], v; ~i; i = e[i].nxt)
	{
		if(dis[v = e[i].to] == dis[now] + 1 && (f = dfs(v, min(res, e[i].cap - e[i].flow))) > 0)
		{
			e[i].flow += f, e[i ^ 1].flow -= f;
			flow += f, res -= f;
			if(res == 0) break;
		}
	}
	return flow;
}
In int maxFlow()
{
	int flow = 0;
	while(bfs())
	{
		memcpy(cur, head, sizeof(head));
		flow += dfs(s, INF);
	}
	return flow;
}

int main()
{
	int T = read(), ID = 0;
	while(T--)
	{
		Mem(head, -1), ecnt = -1;
		n = read(), m = read();
		s = 0, t = n + m + 1;
		for(int i = 1, las = 0; i <= n; ++i)
		{
			int x = read();
			addEdge(s, i, x - las - m);
			las = x;
		}
		for(int i = 1, las = 0; i <= m; ++i)
		{
			int x = read();
			addEdge(i + n, t, x - las - n);
			las = x;
		}
		for(int i = 1; i <= n; ++i)
			for(int j = 1; j <= m; ++j)
			{
				addEdge(i, j + n, 19);
				id[i][j] = ecnt - 1;
			}
		maxFlow();
		printf("Matrix %d\n", ++ID);
		for(int i = 1; i <= n; ++i, enter)
			for(int j = 1; j <= m; ++j) write(e[id[i][j]].flow + 1), space;
		enter;
	}
	return 0;
}
posted @ 2021-06-18 21:36  mrclr  阅读(61)  评论(0编辑  收藏  举报