找不出错误
查看原帖
找不出错误
74874
laeva楼主2018/7/7 20:10

大佬知道我哪里错了吗。。。。。。调了一个晚上了。。。。

//#include<iostream>
#include<cstdio>
#include<queue>

#define fom(i, a, b) for(int i = a; i <= b; i++)
#define foi(i, a, b) for(int i = a; i >= b; i--)
#define MAXN 50010
#define mod 1000000007

int max(int x, int y) { return x > y ? x : y; }
int min(int x, int y) { return x < y ? x : y; }

using namespace std;

int read() {
    int ans = 0; char ch = getchar(), t;
    while (ch < '0' || ch > '9') { t = ch; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { ans = ans * 10 + ch - '0'; ch = getchar(); }
    if (t == '-') ans = -ans;
    return ans;
}

const int D[8][2] = { { 1, 2 },{ -1, 2 },{ 1, -2 },{ -1, -2 },{ 2, 1 },{ -2, 1 },{ 2, -1 },{ -2, -1 } };

int sum = 1, head[MAXN], d[MAXN], n, m, s, t, obs[210][210];
struct node {
    int next, pre, cost;
    node() :next(0), pre(0), cost(0) {};
    inline void add(int x, int y, int z) {
        pre = y; cost = z;
        next = head[x]; head[x] = sum;
    }
}e[MAXN * 2];
queue<int>q;

bool bfs() {
    fom(i, 1, n) d[i] = 0;
    while (!q.empty()) q.pop();
    q.push(s); d[s] = 1;
    while (!q.empty()) {
        int u = q.front(); q.pop();
        for (int i = head[u]; i; i = e[i].next) {
            if (e[i].cost && !d[e[i].pre]) {
                q.push(e[i].pre);
                d[e[i].pre] = d[u] + 1;
                if (e[i].pre == t) return true;
            }
        }
    }
    return false;
}

int dinic(int x, int flow) {
    if (x == t) return flow;
    int rest = flow, k;
    for (int i = head[x]; i && rest; i = e[i].next) {
        if (e[i].cost && d[e[i].pre] == d[x] + 1) {
            k = dinic(e[i].pre, min(rest, e[i].cost));
            if (!k) d[e[i].pre] = 0;
            e[i].cost -= k;
            e[i ^ 1].cost += k;
            rest -= k;
        }
    }
    return flow - rest;
}

int main(int argc, char const *argv[])
{
    n = read(); m = read();
    s = 0; t = n * n + 1;
    fom(i, 1, m) {
        int x = read(), y = read();
        obs[x][y] = 1;
    }
    fom(i, 1, n) fom(j, 1, n) {
        if (obs[i][j]) continue;
        int id = i * n - n + j;
        if ((i & 1) != (j & 1)) {
            e[++sum].add(s, id, 1);
            e[++sum].add(id, s, 0);
            fom(k, 0, 7) {
                int di = i + D[k][0], dj = j + D[k][1], did = di * n - n + dj;
                if (di > 0 && di <= n && dj > 0 && dj <= n)
                    if (!obs[di][dj]) {
                        e[++sum].add(id, did, mod);
                        e[++sum].add(did, id, 0);
                    }
            }
        }
        else e[++sum].add(id, t, 1), e[++sum].add(t, id, 0);
    }
    int flow = 0, maxflow = 0;
    while (bfs())
        while (flow = dinic(s, 1e9 + 7)) maxflow += flow;
    printf("%d", n * n - m - maxflow);
    return 0;
}
2018/7/7 20:10
加载中...