# 洛谷P2774 方格取数问题(最小割)

## 题意

$n \times m$的矩阵，不能取相邻的元素，问最大能取多少

## Sol

/*

*/
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN = 1e5 + 10, INF = 1e9 + 10;
char c = getchar();
int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = 1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, M, a[101][101], black[101][101], S, T = 100001;
struct Edge {
int u, v, f, nxt;
} E[MAXN];
int head[MAXN], cur[MAXN], num = 0;
inline void add_edge(int x, int y, int z) {
E[num] = (Edge) {x, y, z, head[x]};
}
inline void AddEdge(int x, int y, int z) {
}
int trans(int x, int y) {
return (x - 1) * M + y;
}
int xx[5] = {0, -1, +1, 0, 0};
int yy[5] = {0, 0, 0, -1, +1};
int deep[MAXN];
inline int BFS() {
queue<int> q; q.push(S);
memset(deep, 0, sizeof(deep));
deep[S] = 1;
while(!q.empty()) {
int p = q.front(); q.pop();
for(int i = head[p]; i != -1; i = E[i].nxt) {
int to = E[i].v;
if(!deep[to] && E[i].f)
deep[to] = deep[p] + 1, q.push(to);
}
}
return deep[T];
}
int DFS(int x, int flow) {
if(x == T) return flow;
int ansflow = 0;
for(int &i = cur[x]; i != -1; i = E[i].nxt) {
int to = E[i].v;
if(E[i].f && deep[to] == deep[x] + 1) {
int nowflow = DFS(to, min(flow, E[i].f));
E[i].f -= nowflow; E[i ^ 1].f += nowflow;
flow -= nowflow; ansflow += nowflow;
if(flow <= 0) break;
}
}
return ansflow;
}
int Dinic() {
int ans = 0;
while(BFS()) {
ans += DFS(S, INF);
}
return ans;
}
int main() {
int sum = 0;
for(int i = 1; i <= N; i++)
for(int j = 1; j <= M; j++) {
a[i][j] = read(); sum += a[i][j];
if((i + j) % 2 == 0) {
for(int k = 1; k <= 4; k++) {
int wx = i + xx[k], wy = j + yy[k];
if(wx >= 1 && wx <= N && wy >= 1 && wy <= M)
}
}
}
printf("%d", sum - Dinic());
return 0;
}
/*
3 3
1 000 00-
1 00- 0-+
2 0-- -++

*/

1811 篇文章121 人订阅

0 条评论

## 相关文章

### LeetCode-70-Climbing Stairs

You are climbing a stair case. It takes n steps to reach to the top. Each time y...

364120

50470

37080

27010

### 1570. [POJ3461]乌力波

★☆   输入文件：oulipo.in   输出文件：oulipo.out 简单对比 时间限制：1 s   内存限制：256 MB 【题目描述】 法国作家乔治·...

31570

249100

19110

6010

16020

### Lucas定理学习(进阶中)

（1）Lucas定理：p为素数，则有: ? ? ? （2）证明： n=(ak...a2,a1,a0)p = (ak...a2,a1)p*p + a0 =  [n...

30280