HDOJ1569 方格取数(2) #最大流 最小割 Dinic算法#

方格取数(2)

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7690    Accepted Submission(s): 2459

Problem Description

给你一个m*n的格子的棋盘,每个格子里面有一个非负数。
从中取出若干个数,使得任意的两个数所在的格子没有公共边,就是说所取数所在的2个格子不能相邻,并且取出的数的和最大。

Input

包括多个测试实例,每个测试实例包括2整数m,n和m*n个非负数(m<=50,n<=50)

Output

对于每个测试实例,输出可能取得的最大的和

Sample Input

3 3 75 15 21 75 15 28 34 70 5

Sample Output

188

Author

ailyanlu

Source

Recommend

8600

Solution

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> p;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int maxn = 1e4 + 10;
const int dx[4] = { 0, 0, 1, -1 };
const int dy[4] = { 1, -1, 0, 0 };
int cnt, sum, head[maxn], dep[maxn], cur[maxn];
struct edge { int to, flow, next; } e[maxn << 2];

template<typename T = int>
inline const T read()
{
    T x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + ch - '0'; ch = getchar(); }
    return x * f;
}

template<typename T>
inline void write(T x)
{
    if (x < 0) { putchar('-'); x = -x; }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

void init()
{
    cnt = sum = 0;
    memset(head, -1, sizeof(head));
    memset(dep, 0, sizeof(dep));
    memset(cur, 0, sizeof(cur));
}

void addEdge(int u, int v, int w)
{
    e[cnt].to = v;
    e[cnt].flow = w;
    e[cnt].next = head[u];
    head[u] = cnt++;
}

bool bfs(int s, int t)
{
    queue<int> q;
    memset(dep, -1, sizeof(dep));
    dep[s] = 0;
    q.push(s);
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for (int i = head[u]; ~i; i = e[i].next)
        {
            int v = e[i].to;
            if (dep[v] == -1 && e[i].flow)
            {
                q.push(v);
                dep[v] = dep[u] + 1;
            }
        }
    }
    return ~dep[t];
}

int dfs(int u, int t, int rflow)
{
    if (u == t) return rflow;
    int res = 0;
    for (int i = cur[u]; ~i && rflow; i = e[i].next)
    {
        int v = e[i].to;
        if (dep[v] != dep[u] + 1 || !e[i].flow) continue;
        cur[u] = i;
        int flow = dfs(v, t, min(e[i].flow, rflow));
        e[i].flow -= flow;
        e[i ^ 1].flow += flow;
        rflow -= flow;
        res += flow;
    }
    if (!res) dep[u] = -1;
    return res;
}

int dinic(int s, int t)
{
    int res = 0;
    while (bfs(s, t))
    {
        memcpy(cur, head, sizeof(head));
        res += dfs(s, t, inf);
    }
    return res;
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("input.txt", "r", stdin);
#endif
    int n, m;
    while (~scanf("%d%d", &n, &m))
    {
        init();
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                int u = (i - 1) * m + j, w = read();
                sum += w;
                if ((i + j) & 1)
                {
                    addEdge(0, u, w);
                    addEdge(u, 0, w);
                    for (int k = 0; k < 4; k++)
                    {
                        int x = i + dx[k], y = j + dy[k];
                        int v = (x - 1) * m + y;
                        if (x >= 1 && x <= n && y >= 1 && y <= m)
                        {
                            addEdge(u, v, inf);
                            addEdge(v, u, 0);
                        }
                    }
                }
                else
                {
                    addEdge(u, n * m + 1, w);
                    addEdge(n * m + 1, u, 0);
                }
            }
        }
        printf("%d\n", sum - dinic(0, n * m + 1));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_35850147/article/details/104208393