POJ2396 Budget
Budget
Time Limit: 3000MS
Memory Limit: 65536K
Special Judge
Description
We are supposed to make a budget proposal for this multi-site competition. The budget proposal is a matrix where the rows represent different kinds of expenses and the columns represent different sites. We had a meeting about this, some time ago where we discussed the sums over different kinds of expenses and sums over different sites. There was also some talk about special constraints: someone mentioned that Computer Center would need at least 2000K Rials for food and someone from Sharif Authorities argued they wouldn’t use more than 30000K Rials for T-shirts. Anyway, we are sure there was more; we will go and try to find some notes from that meeting.
And, by the way, no one really reads budget proposals anyway, so we’ll just have to make sure that it sums up properly and meets all constraints.
Input
The first line of the input contains an integer N, giving the number of test cases. The next line is empty, then, test cases follow: The first line of each test case contains two integers, m and n, giving the number of rows and columns (m <= 200, n <= 20). The second line contains m integers, giving the row sums of the matrix. The third line contains n integers, giving the column sums of the matrix. The fourth line contains an integer c (c < 1000) giving the number of constraints. The next c lines contain the constraints. There is an empty line after each test case.
Each constraint consists of two integers r and q, specifying some entry (or entries) in the matrix (the upper left corner is 1 1 and 0 is interpreted as “ALL”, i.e. 4 0 means all entries on the fourth row and 0 0 means the entire matrix), one element from the set {<, =, >} and one integer v, with the obvious interpretation. For instance, the constraint 1 2 > 5 means that the cell in the 1st row and 2nd column must have an entry strictly greater than 5, and the constraint 4 0 = 3 means that all elements in the fourth row should be equal to 3.
Output
For each case output a matrix of non-negative integers meeting the above constraints or the string “IMPOSSIBLE” if no legal solution exists. Put one empty line between matrices.
Sample Input
2
2 3
8 10
5 6 7
4
0 2 > 2
2 1 = 3
2 3 > 2
2 3 < 5
2 2
4 5
6 7
1
1 1 > 10
Sample Output
2 3 3
3 3 4
IMPOSSIBLE
Source
Tehran 2003 Preliminary
题目大意:给一个n*m的格子填数,有行列和单个格子的限制。求是否有可行解并输出一组可行的填法。
解:
这道题是有上下界的网络流。
建图:设置一个source和一个sink点,将sourse与行标号1~m连接,流量限制为行的数的和;将列标号1~n与sink连接,流量限制为列的数的和;依次将对应的行列标号相连,设为a行与b行相连,代表原图中(a,b)的格子,所以流量限制为格子的数的范围。
要求解这个有上下界限制的网络流的一个可行解,且是有源汇的。转化为一个没有源汇的上下界网络流处理,即连一条从sink到source的容量为inf的边。
重新构图:设置一个superSource和一个superSink,superSource负责提供每一个点的最低流量,superSink负责接收每一个点的最低流量,原来的边变为只有容量上限的边,转化为普通网络流处理,跑一遍dinic即可。
注意:用long long
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
const int MAXN = 600;
const int MAXM = 1000000;
const ll INF = 1e15;
struct Edge{
ll t, f, nxt;
};
struct MCMF{
ll src, sink, tot;
ll vis[MAXN], dis[MAXN], g[MAXN];
Edge e[1000000];
queue<int> q;
void init(){
tot = 1;
memset(g, 0, sizeof g);
}
void addEdge(ll u, ll v, ll c){
e[++tot].t = v;
e[tot].f = c;
e[tot].nxt = g[u];
g[u] = tot;
e[++tot].t = u;
e[tot].f = 0;
e[tot].nxt = g[v];
g[v] = tot;
}
void bfs(){
memset(dis, 0, sizeof dis);
q.push(src);
vis[src] = 1;
while (!q.empty()) {
int x = q.front();
q.pop();
for (int i = g[x]; i; i = e[i].nxt)
if (e[i].f && !vis[e[i].t]) {
vis[e[i].t] = 1;
dis[e[i].t] = dis[x] + 1;
q.push(e[i].t);
}
}
}
ll dfs(int x, ll delta) {
if (x == sink) return delta;
ll res = 0;
for (int i = g[x]; delta && i; i = e[i].nxt)
if (e[i].f && dis[e[i].t] == dis[x] + 1) {
int dd = dfs(e[i].t, min(e[i].f, delta));
e[i].f -= dd;
e[i^1].f += dd;
delta -= dd;
res += dd;
}
return res;
}
ll maxFlow(){
ll res = 0;
while (true) {
memset(vis, 0, sizeof vis);
bfs();
if (!vis[sink]) return res;
res += dfs(src, INF);
}
}
}MF;
ll n, m;
ll _up[MAXN][MAXN], _low[MAXN][MAXN], ans[MAXN][MAXN], row[MAXN], column[MAXN];
ll up[MAXM], low[MAXM], u[MAXM], v[MAXM], tot_in[MAXM], tot_out[MAXM];
bool lowbound_flow(ll src, ll sink, ll tot) {
MF.init();
memset(tot_in, 0, sizeof tot_in);
memset(tot_out, 0, sizeof tot_out);
memset(ans, 0, sizeof ans);
for (int i = 1; i <= tot; ++i) {
if (up[i] < low[i]) return 0;
tot_in[v[i]] += low[i];
tot_out[u[i]] += low[i];
MF.addEdge(u[i], v[i], up[i] - low[i]);
}
MF.addEdge(sink, src, INF);
int superSrc = sink + 1;
int superSink = sink + 2;
MF.src = superSrc;
MF.sink = superSink;
for (int i = 0; i <= n+m+1; ++i) {
if (tot_in[i]) MF.addEdge(superSrc, i, tot_in[i]);
if (tot_out[i]) MF.addEdge(i, superSink, tot_out[i]);
}
MF.maxFlow();
for (int i = MF.g[superSrc]; i; i = MF.e[i].nxt)
if (MF.e[i].f && MF.e[i].t != sink)
return 0;
for (int j = 1; j <= m; ++j)
for (int i = MF.g[j]; i; i = MF.e[i].nxt)
if (MF.e[i].t > m && MF.e[i].t <= n + m)
ans[j][MF.e[i].t - m] = MF.e[i^1].f;
return 1;
}
int main()
{
int T;
scanf("%d", &T);
while (T--) {
scanf("%lld%lld", &m, &n);
//init
ll totEdge = 0, sink = m + n + 1, src = 0;
//totEdge:总边数 newP:新结点编号 sink:原图汇点
for (int i = 1; i <= m; ++i)
scanf("%lld", &row[i]);
for (int i = 1; i <= n; ++i)
scanf("%lld", &column[i]);
for (int i = 1; i <= m; ++i)
for (int j = 1; j <= n; ++j)
_up[i][j] = INF, _low[i][j] = 0;
for (int i = 1; i <= m; ++i) {
up[++totEdge] = row[i];
low[totEdge] = row[i];
u[totEdge] = 0;
v[totEdge] = i;
}
for (int i = 1; i <= n; ++i) {
up[++totEdge] = column[i];
low[totEdge] = column[i];
u[totEdge] = m+i;
v[totEdge] = sink;
}
ll constraint, r, q, V, flag = 1;
char sign[3];
scanf("%lld", &constraint);
for (int i = 1; i <= constraint; ++i) {
scanf("%lld%lld%s%lld", &r, &q, sign, &V);
if (sign[0] == '>') {
V++;
if (!r && q) {
for (int j = 1; j <= m; j++) {
if (V > _up[j][q]) flag = 0;
_low[j][q] = V;
}
}
else if (r && !q) {
for (int j = 1; j <= n; ++j) {
if (V > _up[r][j]) flag = 0;
_low[r][j] = V;
}
}
else if (!r && !q) {
for (int j = 1; j <= m; ++j)
for (int k = 1; k <= n; ++k) {
if (V > _up[j][k]) flag = 0;
_low[j][k] = V;
}
}
else {
if (V > _up[r][q]) flag = 0;
_low[r][q] = V;
}
}
else if (sign[0] == '=') {
if (!r && q)
for (int j = 1; j <= m; j++) {
if (V < _low[j][q] || V > _up[j][q]) flag = 0;
_low[j][q] = _up[j][q] =V;
}
else if (r && !q)
for (int j = 1; j <= n; ++j) {
if (V < _low[r][j] || V > _up[r][j]) flag = 0;
_low[r][j] = _up[j][q] = V;
}
else if (!r && !q)
for (int j = 1; j <= m; ++j)
for (int k = 1; k <= n; ++k) {
if (V < _low[j][k] || V > _up[j][k]) flag = 0;
_low[j][k] = _up[j][k] = V;
}
else {
if (V < _low[r][q] || V > _up[r][q]) flag = 0;
_low[r][q] = _up[r][q] = V;
}
}
else if (sign[0] == '<') {
V--;
if (!r && q)
for (int j = 1; j <= m; j++) {
if (V < _low[j][q]) flag = 0;
_up[j][q] = V;
}
else if (r && !q)
for (int j = 1; j <= n; ++j) {
if (V < _low[r][j]) flag = 0;
_up[r][j] = V;
}
else if (!r && !q)
for (int j = 1; j <= n; ++j)
for (int k = 1; k <= m; ++k) {
if (V < _low[j][k]) flag = 0;
_up[j][k] = V;
}
else {
if (V < _low[r][q]) flag = 0;
_up[r][q] = V;
}
}
}
for (int i = 1; i <= m; ++i)
for (int j = 1; j <= n; ++j) {
up[++totEdge] = _up[i][j];
low[totEdge] = _low[i][j];
u[totEdge] = i;
v[totEdge] = m + j;
}
if (flag && lowbound_flow(src, sink, totEdge))
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j)
printf("%lld ", ans[i][j] + _low[i][j]);
printf("\n");
}
else {
printf("IMPOSSIBLE\n");
}
printf("\n");
}
return 0;
}