POJ 2396 Budget 有上下界的可行流

求矩阵填数可行方案,给定每行每列和,和格子数字大小约束。
即:
jaij=ci; jaji=ri; aij {>|<|=} dij

对比一下有上下界的网络流线性规划表达:

vfsv=vfvtvfuv=vfvuluvfuvhuv

分析一下题目,存在关系式: ici=jrj
如果把这个看做源汇点的流量平衡,相当于,从源点引出了一些边,其流量必须为 ci ,即上下界均为 ci 。于是为每列每行专门建立点。
再来看数字范围,有题目肯定有 lijaijhij 。即边的上下界范围。

那么问题就简单了,为每列每行建立节点,分别连向源汇点,上下界为和的大小。每个格子建立连接对应行列的节点,上下界为格子的取值范围。跑一次有上下界的可行流即可。

代码写的很丑。。110+行。。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define FOR(i,j,k) for(i=j;i<=k;++i)
const int N = 505, M = 10005, P = 201, inf = 0x7f7f7f7f;
void getmax(int &a, int b) { if (a < b) a = b; }
void getmin(int &a, int b) { if (a > b) a = b; }
struct Graph {
    int h[N], p[M], v[M], w[M], g[N], level[N], q[M * 80], cnt;
    int ss, tt, s, t;

    void init(int a, int b) {
        ss = N - 2; tt = N - 1;
        s = a; t = b; cnt = 0; memset(h, -1, sizeof h);
        memset(g, 0, sizeof g);
    }

    private: void add(int a, int b, int c) {
        p[cnt] = h[a]; v[cnt] = b; w[cnt] = c; h[a] = cnt++;
        p[cnt] = h[b]; v[cnt] = a; w[cnt] = 0; h[b] = cnt++;
    }

    public: void add(int a, int b, int c, int d) {
        g[a] -= c; g[b] += c; add(a, b, d - c);
    }

    bool finish() {
        int i, in = 0, out = 0;
        add(t, s, inf);
        FOR(i,1,t-1)
            if (g[i] > 0) in += g[i], add(ss, i, g[i]);
            else if (g[i] < 0) out -= g[i], add(i, tt, -g[i]);
        return in == out && in == dinic();
    }

    bool bfs() {
        int f = 0, r = 0, u, i;
        memset(level, -1, sizeof level);
        q[r++] = ss; level[ss] = 0;
        while (f < r) {
            u = q[f++];
            for (i = h[u]; i != -1; i = p[i])
                if (w[i] > 0 && level[v[i]] == -1) {
                    level[v[i]] = level[u] + 1;
                    if (v[i] == tt) return true;
                    q[r++] = v[i];
                }
        }
        return false;
    }

    int dfs(int x, int low) {
        if (x == tt) return low;
        int res = 0, i, tmp;
        for (i = h[x]; i != -1 && res < low; i = p[i])
            if (w[i] > 0 && level[v[i]] == level[x] + 1) {
                tmp = dfs(v[i], min(low - res, w[i]));
                w[i] -= tmp; w[i ^ 1] += tmp; res += tmp;
            }
        if (!res) level[x] = -1;
        return res;
    }

    int dinic() {
        int ans = 0;
        while (bfs()) ans += dfs(ss, inf);
        return ans;
    }
} g;

int low[P][P], high[P][P], ans[P][P];

int main() {
    int n, m, kase, x, i, j, uL, uR, vL, vR, k, u, v, c, s, t, flag = 0;
    char ch[4];
    scanf("%d", &kase);
    while (kase--) {
        scanf("%d%d", &n, &m);
        g.init(s = 0, t = n + m + 1);
        FOR(i,1,n) scanf("%d", &x), g.add(s, i, x, x);
        FOR(i,1,m) scanf("%d", &x), g.add(i + n, t, x, x);
        memset(low, 0, sizeof low);
        memset(high, 127, sizeof high);
        scanf("%d", &k);
        while (k--) {
            scanf("%d%d%s%d", &u, &v, ch, &c);
            uL = uR = u; vL = vR = v;
            if (u == 0) uL = 1, uR = n;
            if (v == 0) vL = 1, vR = m;
            FOR(i,uL,uR) FOR(j,vL,vR) {
                if (ch[0] == '>') getmax(low[i][j], c + 1);
                if (ch[0] == '<') getmin(high[i][j], c - 1);
                if (ch[0] == '=') getmax(low[i][j], c), getmin(high[i][j], c);
            }
        }
        if (flag++) puts("");
        FOR(i,1,n) FOR(j,1,m) {
            if (low[i][j] > high[i][j]) goto fail;
            g.add(i, j + n, low[i][j], high[i][j]);
        }
        if (!g.finish()) goto fail;
        FOR(i,1,n) for (j = g.h[i]; j != -1; j = g.p[j]) if (g.v[j] > n)
            ans[i][g.v[j] - n] = g.w[j ^ 1] + low[i][g.v[j] - n];
        FOR(i,1,n) {
            FOR(j,1,m) printf("%d ", ans[i][j]);
            puts("");
        }
        continue;
        fail: puts("IMPOSSIBLE");
    }

    return 0;
}

Budget

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 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值