【POJ 2396】 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

解题报告

这是一道比较裸的上下界网络流

由题目给出的限制, 建造m + n + 2个节点,
其中m个节点代表行, n个节点代表列
S向每个代表行的节点连接下界l,上节r都为行上数字和的边 记为 [l,r]
同理, 每个代表列的节点向T连边。
对于每个方格(i,j) 看做一条S + i -> S + m + j [low[i][j], up[i][j]]的边进行连接
然后套上上下界网络流的版跑就行了

比如样例1:
这里写图片描述

比较恶心的是有可能出现0 0 这种对整个图都有限制的输入和非法的输入。。。

下面贴代码 :

#include <cstdio>
#include <cstring>
#include <queue>

typedef long long LL;
const int MAXM = 220;
const int MAXN = 22;
const int INF  = 0x3f3f3f3f;

template <class T> T min(T a, T b) {
    return a<b ? a : b;
}
template <class T> T max(T a, T b) {
    return a>b ? a : b;
}

inline int getInt() {
    int ret = 0;
    char ch;
    bool k = false;
    while((ch = getchar()) < '0' || ch > '9') if(ch == '-') k = true;
    do {ret *= 10; ret += ch - '0';}
    while((ch = getchar()) >= '0' && ch <= '9') ;
    ungetc(ch, stdin);
    return k ? -ret : ret;
}

namespace isap {

    const int MAXD = MAXN * MAXM * 4;
    const int MAXE = MAXN * MAXM * 200;

    struct Node {
        int v,w,bk,nxt;
    } d[MAXE];
    int head[MAXD], etot;
    inline int addedge(int a, int b, int c) {
        ++ etot;
        d[etot].v = b;
        d[etot].w = c;
        d[etot].bk = etot + 1;
        d[etot].nxt = head[a];
        head[a] = etot;
        ++ etot;
        d[etot].v = a;
        d[etot].w = 0;
        d[etot].bk = etot - 1;
        d[etot].nxt = head[b];
        head[b] = etot;
        return etot;
    }

    int dis[MAXD], vd[MAXD];
    std :: queue<int> que;
    void bfs(int s){
        memset(dis, 0x3f, sizeof dis);
        dis[s] = 0;
        que.push(s);

        while(!que.empty()) {
            int u = que.front();
            que.pop();

            for(int e = head[u]; e; e = d[e].nxt)
                if(dis[d[e].v] > dis[u] + 1) {
                    dis[d[e].v] = dis[u] + 1;
                    que.push(d[e].v);
                }
        }
    }

    int S, T, n;


    int dfs(int u, int val) {
        if(u == T) return val;
        int rest = val;

        for(int e = head[u]; e; e = d[e].nxt)
            if(d[e].w && dis[d[e].v] + 1 == dis[u]) {
                int t = dfs(d[e].v, min(d[e].w, rest));
                d[e].w -= t;
                d[d[e].bk].w += t;
                rest -= t;
                if(!rest) return val;
                if(dis[S] == n) return val - rest;
            }
        if(rest == val) {
            -- vd[dis[u]];
            if(!vd[dis[u]]) return 0;
            dis[u] = n;
            for(int e = head[u]; e; e = d[e].nxt)
                if(d[e].w) dis[u] = min(dis[u], dis[d[e].v] + 1);
            ++ vd[dis[u]];
        }
        return val - rest;
    }

    int sap (int s, int t, int nn) {
        S = s;
        T = t;
        n = nn;
        bfs(T);
        memset(vd, 0, sizeof vd);
        for(int i = 1; i<=n; i++)
            if(dis[i] <= n) ++ vd[dis[i]];
        int flow = 0;
        while(dis[S] < n) flow += dfs(S, INF);
        return flow;
    }

    void reset() {
        etot = 0;
        memset(head, 0, sizeof head);
        S = T = n = 0;
    }
}


int X, Y;
int edge_sum;

const int IDMOV = 2;
inline int addedgeEx(int a, int b, int c, int d){
    //printf("%d to %d : [%d,%d]\n", a,b,c,d);
    isap :: addedge(X, b, c);
    isap :: addedge(a, Y, c);
    edge_sum += c;
    return isap :: addedge(a, b, d - c) - IDMOV;
}


int upper[MAXM][MAXN];
int lower[MAXM][MAXN];
int ans  [MAXM][MAXN];

int main () {
    int cas ;
    scanf("%d", &cas);

    while(cas -- ){

        isap :: reset();
        memset(lower, 0xff, sizeof lower);
        memset(upper, 0x3f, sizeof upper);

        int n = getInt(), m = getInt();
        int S = 1, T = S + n + m + 1;
        int sum1 = 0, sum2 = 0;
        X = T + 1, Y = T + 2;
        int a, b;
        edge_sum = 0;
        for(int i = 1; i<=n; i++) {
            a = getInt();
            sum1 += a;
            addedgeEx(S, S + i, a, a);
        }
        for(int i = 1; i<=m; i++) {
            b = getInt();
            sum2 += b;
            addedgeEx(S + n + i, T, b, b);
        }

        int q = getInt();
        bool nosol = false;
        char tmp[10];
        while(q --) {

            a = getInt();
            b = getInt();

            scanf("%s", tmp);
            if(tmp[0] == '>')
                lower[a][b] = max(lower[a][b], getInt());
            else if(tmp[0] == '<')
                upper[a][b] = min(upper[a][b], getInt());
            else if(tmp[0] == '='){
                int t = getInt();
                if(lower[a][b] >= t || upper[a][b] <= t)
                    nosol = true;
                lower[a][b] = t - 1, upper[a][b] = t + 1;
            }
        }

        if(sum1 != sum2) {puts("IMPOSSIBLE"); if(q)putchar('\n'); continue ;}

        if(nosol) {puts("IMPOSSIBLE"); if(q)putchar('\n'); continue ;}
        for(int i = 1; i<=n; i++)
            for(int j = 1; j<=m; j++){
                a = max(max(lower[0][0], lower[i][j]), max(lower[i][0], lower[0][j])) + 1;
                b = min(min(upper[0][0], upper[i][j]), min(upper[i][0], upper[0][j])) - 1;
                if(a > b) {nosol = true; break;}
                else ans[i][j] = addedgeEx(S + i, S + n + j, a, b);
            }
        if(nosol) {puts("IMPOSSIBLE"); if(q)putchar('\n'); continue ;}

        int r = isap :: addedge(T, S, INF);
        int flow = isap :: sap(X, Y, Y);
        if(flow != edge_sum) {puts("IMPOSSIBLE"); if(q)putchar('\n'); continue ;}
        if(isap :: d[r].w != sum1 || isap :: d[r].w != sum2){
            puts("IMPOSSIBLE");
            if(q)putchar('\n');
        }
        else {
            for(int i = 1; i<=n; i++){
                printf("%d",
                 isap :: d[ans[i][1]].w
                 + isap :: d[ans[i][1] + 2].w);
                for(int j = 2; j<=m; j++)
                    printf(" %d",
                    isap :: d[ans[i][j]].w
                    + isap :: d[ans[i][j] + 2].w);
                putchar('\n');
            }
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值