首页 > 题解 > bzoj 2406 矩阵

bzoj 2406 矩阵

Description

Input

第一行两个数n、m,表示矩阵的大小。
接下来n行,每行m列,描述矩阵A。
最后一行两个数L,R。
Output

第一行,输出最小的答案;

Sample Input

2 2

0 1

2 1

0 1

Sample Output

1

HINT

对于100%的数据满足N,M<=200,0<=L<=R<=1000,0<=Aij<=1000

题解

最大值最小,想到二分答案。

每次二分一个答案mid,相当于$|\sum a_i – \sum bi| \le mid$

拆开$\sum a_i-mid\le \sum b_i\le \sum a_i+mid$

这就可以用可行流check了

每一行和每一列建一个点xi,yi

s->xi,[ai-mid,ai+mid]
yj->t,[aj-mid,aj+mid]
xi->yj,[L,R]

只要判断是否有可行流就行了

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#define N 100010
using namespace std;
const int inf = 0x3f3f3f3f;
int n, m, S, T, s, t, l, r, x, d[N], st[N], cr[N];
struct flows {
    struct node {
        int to, next, flow;
    } e[N];
    int tot, st[N], dis[N], cur[N];
    void init() {
        tot = -1, memset(e, -1, sizeof e),
        memset(st, -1, sizeof st);
        memset(d, 0, sizeof d);
    }
    void add(int x, int y, int z) {
        e[++tot].to = y;
        e[tot].flow = z;
        e[tot].next = st[x];
        st[x] = tot;
    }
    void add_edge(int x, int y, int z) {
//      printf("add %d %d %d\n", x, y, z);
        add(x, y, z), add(y, x, 0);
    }
    queue <int> que;
    int bfs(int S, int T) {
        memcpy(cur, st, sizeof st);
        memset(dis, 0, sizeof dis);
        while (!que.empty()) que.pop();
        que.push(S);
        dis[S] = 1;
        while(!que.empty()) {
            int now = que.front();
            que.pop();
            for (int i = st[now]; ~i; i = e[i].next)
                if (e[i].flow && !dis[e[i].to]) {
                    dis[e[i].to] = dis[now] + 1;
                    if (e[i].to == T) return 1;
                    que.push(e[i].to);
                }
        }
        return 0;
    }
    int finds(int now, int T, int flow) {
        if (now == T)
            return flow;
        int f;
        for (int i = cur[now]; ~i; i = e[i].next) {
            cur[now] = i;
            if (e[i].flow && dis[e[i].to] == dis[now] + 1 &&
                (f = finds(e[i].to, T, min(flow, e[i].flow)))) {
                e[i].flow -= f;
                e[i^1].flow += f;
                return f;
            }
        }
        return 0;
    }
    int dinic(int S, int T) {
        int flow = 0, x;
        while(bfs(S, T)) {
            while(x = finds(S, T, inf)) {
                flow += x;
            }
        }
        return flow;
    }
}flow;
bool check(int mid) {
    flow.init();
    int in = 0, out = 0;
    for (int i = 1; i <= n; ++i)
        flow.add_edge(s, i, mid * 2),
        d[s] -= st[i] - mid, d[i] += st[i] - mid;
    for (int i = 1; i <= m; ++i)
        flow.add_edge(i + n, t, mid * 2),
        d[t] += cr[i] - mid, d[i + n] -= cr[i] - mid;
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j)
            flow.add_edge(i, j + n, r - l),
            d[i] -= l, d[j + n] += l;
    flow.add_edge(t, s, inf);
    for (int i = 1; i <= t; ++i)
        if (d[i] > 0)
            flow.add_edge(S, i, d[i]), in += d[i];
        else if (d[i] < 0)
            flow.add_edge(i, T, -d[i]), out -= d[i];
    if (in != out) return 0;
    int ans = flow.dinic(S, T);
    return ans == in;
}
int find() {
    int l = 0, r = 2000000, ans;
    while (l <= r) {
        int mid = l + r >> 1;
        if (check(mid))
            ans = mid, r = mid - 1;
        else
            l = mid + 1;
    }
    return ans;
}
main() {
    scanf("%d%d", &n, &m);
    s = n + m + 1, t = s + 1, S = t + 1, T = S + 1;
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j)
            scanf("%d", &x),
            st[i] += x, cr[j] += x;
    scanf("%d%d", &l, &r);
    printf("%d\n", find());
}