---
id: 1147
lang: en
translation_of: index.md
source_sha256: db101bc1aa0b6c54b877616b48ee840caaf23382c4d504fb7f24fb38c8c660a9
title: "LibreOJ 6007: [24 Network Flow Problems] Picking Numbers from a Grid — Solution"
description: "Color the board red and black, connect the source to red cells and black cells to the sink, and the answer is the total weight minus the max flow."
tags: ["OI","Network Flow","Minimum Cut","Maximum Flow"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "「网络流 24 题」 (a well-known problem set) rendered as [24 Network Flow Problems], matching the English editions of posts 689, 694, 729 and 1000."
  - "Problem name 方格取数 rendered as “Picking Numbers from a Grid” (literally “grid cells, take numbers”)."
  - "大难 (sic, dà nàn, “catastrophe”) read as an input-method typo for 答案 (dá’àn, “answer”)."
  - "最大流限制为…的点权 (literally “max flow limited to … weight”) rendered as “with the max flow capped at … weight”."
---

## Solution

This problem can be seen as a network flow model. We color the board in red and black. Then the source connects to the red points, with the max flow capped at the red point's weight; all the black points connect to the sink, with the max flow capped at the black point's weight. The answer is the total point weight minus the max flow.

Let's try to make sense of it: for the case $m = 1, n = 2$:

$$
[ x, y ]
$$

Then suppose $x$ is a black point and $y$ is a red point; in the network it looks like this:

![graph-network](media/graph-1-1.png)

We find that the max flow only leaves the smaller of the two. That being so, we can <span class="slang" data-zh="感性推广" data-gloss="gǎnxìng tuīguǎng, “perceptually generalize”. OI slang for extending a result by hand-wavy intuition instead of a rigorous proof.">hand-wavily generalize</span> this to any case: the answer is just the point weight sum minus the min cut.

<!--more-->

## Code

```cpp
// LOJ6007.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 10003, INF = 0x3f3f3f3f;
const int vertical[4] = {0, 1, -1, 0}, hori[4] = {1, 0, 0, -1};
int n, m, sum, head[MAX_N], current, dep[MAX_N], s, t, cur[MAX_N];
int arr[50][50], color, colors[50][50];
struct edge
{
    int to, nxt, weight;
} edges[80010];
void addpath(int src, int dst, int weight)
{
    edges[current].to = dst, edges[current].nxt = head[src];
    edges[current].weight = weight, head[src] = current++;
}
void add(int src, int dst, int weight)
{
    addpath(src, dst, weight);
    addpath(dst, src, 0);
}
bool bfs()
{
    memset(dep, 0, sizeof(dep));
    queue<int> q;
    q.push(s), dep[s] = 1;
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for (int i = head[u]; i != -1; i = edges[i].nxt)
            if (edges[i].weight > 0 && dep[edges[i].to] == 0)
            {
                dep[edges[i].to] = dep[u] + 1;
                q.push(edges[i].to);
            }
    }
    return dep[t] != 0;
}
int dfs(int u, int flow)
{
    if (flow == 0 || u == t)
        return flow;
    for (int &i = cur[u]; i != -1; i = edges[i].nxt)
        if (dep[edges[i].to] == dep[u] + 1 && edges[i].weight > 0)
            if (int di = dfs(edges[i].to, min(flow, edges[i].weight)))
            {
                edges[i].weight -= di, edges[i ^ 1].weight += di;
                return di;
            }
    return 0;
}
int dinic()
{
    int ans = 0;
    while (bfs())
    {
        for (int i = 0; i <= t; i++)
            cur[i] = head[i];
        while (int di = dfs(s, INF))
            ans += di;
    }
    return ans;
}
int getPos(int x, int y) { return (x - 1) * m + y; }
int main()
{
    memset(head, -1, sizeof(head));
    scanf("%d%d", &n, &m);
    s = 0, t = n * m + 1;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            scanf("%d", &arr[i][j]), colors[i][j] = (i + j) & 1, sum += arr[i][j];
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            if (colors[i][j] == 1)
                add(getPos(i, j), t, arr[i][j]);
            else
                add(s, getPos(i, j), arr[i][j]);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            if (colors[i][j] == 0)
                for (int dir = 0; dir < 4; dir++)
                {
                    int dstX = i + vertical[dir], dstY = j + hori[dir];
                    if (dstX > 0 && dstX <= n && dstY > 0 && dstY <= m)
                        add(getPos(i, j), getPos(dstX, dstY), INF);
                }
    int answer = sum - dinic();
    printf("%d", answer);
    return 0;
}
```
