POJ2396 Budget
嘟嘟嘟
上下界网络流之可行流。
对于这种矩阵的题,做过就应该知道怎么建图。
像二分图一样,左边\(n\)个点代表行,右边\(m\)个点代表列。对于点\((i, j)\)的限制,就从左边第\(i\)个点向右边第\(j\)个点连边。
然后这题基本也就完事了。
建图虽然不难,但写起来比较麻烦,因为数据较小,推荐邻接矩阵存每一条边的最小最大容量,这样方便更新取最紧的限制。
刚开始我WA了是为了减小复杂度,建图的时候如果当前条件不符合就break,导致后面的数据没有读入,从而影响了后面的几组。结果RE了……
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 205;
const int maxe = 1e6 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline 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, ss, tt;
char ch[2];
int id[maxn][maxn];
struct Edge
{
int nxt, from, to, cap, flow;
}e[maxe];
int head[maxn << 1], ecnt = -1;
void addEdge(int x, int y, int w, int i, int j)
{
e[++ecnt] = (Edge){head[x], x, y, w, 0};
head[x] = id[i][j] = ecnt;
e[++ecnt] = (Edge){head[y], y, x, 0, 0};
head[y] = ecnt;
}
int dis[maxn << 1];
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 != -1; i = e[i].nxt)
if(!dis[v = e[i].to] && e[i].cap > e[i].flow)
{
dis[v] = dis[now] + 1;
q.push(v);
}
}
return dis[t];
}
int cur[maxn << 1];
int dfs(int now, int res)
{
if(now == t || res == 0) return res;
int flow = 0, f;
for(int& i = cur[now], v; i != -1; i = e[i].nxt)
{
if(dis[v = e[i].to] == dis[now] + 1 && (f = dfs(v, min(e[i].cap - e[i].flow, res))) > 0)
{
e[i].flow += f; e[i ^ 1].flow -= f;
flow += f; res -= f;
if(res == 0) break;
}
}
return flow;
}
int maxflow()
{
int flow = 0;
while(bfs())
{
memcpy(cur, head, sizeof(head));
int x = dfs(s, INF);
flow += x;
}
return flow;
}
bool flg = 1;
int sumi[maxn], sumo[maxn], tot = 0;
int b[maxn][maxn], c[maxn][maxn];
void Set(int x, int y, char ch, int w)
{
if(ch == '=')
{
if(b[x][y] > w || c[x][y] < w) flg = 0;
else b[x][y] = c[x][y] = w;
}
else if(ch == '<')
{
if(b[x][y] >= w) flg = 0;
else c[x][y] = min(c[x][y], w - 1);
}
else if(ch == '>')
{
if(c[x][y] <= w) flg = 0;
else b[x][y] = max(b[x][y], w + 1);
}
}
void build()
{
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
addEdge(i, j + n, c[i][j] - b[i][j], i, j);
for(int i = 1; i <= n; ++i)
{
int sum = sumi[i];
for(int j = 1; j <= m; ++j) sum -= b[i][j];
if(sum > 0) addEdge(s, i, sum, 0, 0), tot += sum;
else addEdge(i, t, -sum, 0, 0);
}
for(int j = 1; j <= m; ++j)
{
int sum = -sumo[j];
for(int i = 1; i <= n; ++i) sum += b[i][j];
if(sum > 0) addEdge(s, n + j, sum, 0, 0), tot += sum;
else addEdge(n + j, t, -sum, 0, 0);
}
}
void init()
{
Mem(head, -1); ecnt = -1;
Mem(b, 0); Mem(c, 0x3f); tot = 0; flg = 1;
s = 0; t = n + m + 1;
}
int main()
{
int T = read();
while(T--)
{
n = read(); m = read();
init();
for(int i = 1; i <= n; ++i) sumi[i] = read();
for(int i = 1; i <= m; ++i) sumo[i] = read();
int q = read();
for(int i = 1, x, y, w; i <= q; ++i)
{
x = read(), y = read(); scanf("%s", ch); w = read();
if(!x && !y)
for(int j = 1; j <= n; ++j)
for(int k = 1; k <= m; ++k) Set(j, k, ch[0], w);
else if(!x && y)
for(int j = 1; j <= n; ++j) Set(j, y, ch[0], w);
else if(x && !y)
for(int j = 1; j <= m; ++j) Set(x, j, ch[0], w);
else Set(x, y, ch[0], w);
}
if(!flg) {puts("IMPOSSIBLE"); continue;}
build();
if(maxflow() < tot) puts("IMPOSSIBLE");
else
{
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= m; ++j) write(e[id[i][j]].flow + b[i][j]), space;
enter;
}
}
}
return 0;
}