【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');
            }
        }

    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章