There is an undirected graph describing the location and edge between the rabbit nest. Now the wolf has caught the rabbit, and the rabbit quickly escaped from (1, 1) to (m, n ). The maximum number of rabbits on each side. A wolf does not want a rabbit to escape. Every time a wolf stays on one side, it can avoid a rabbit passing through. Ask the minimum number of wolves that can prevent all rabbits from escaping.
Idea: to create a graph, remove the smallest edge in the question so that the source to sink is not connected, obviously the smallest cut.
Code:
#include <queue>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAX 7000000#define INF 0x7f7f7f7f#define S 1#define T (m * n)using namespace std;int m,n;int head[MAX],total = 1;int next[MAX],aim[MAX],flow[MAX];int deep[MAX];inline void Add(int x,int y,int f);inline bool BFS();int Dinic(int x,int f);int main(){cin >> m >> n;for(int i = 1;i <= m; ++i)for(int x,j = 1;j <= n - 1; ++j) {scanf("%d",&x);Add((i - 1) * n + j,(i - 1) * n + j + 1,x);Add((i - 1) * n + j + 1,(i - 1) * n + j,x);}for(int i = 1;i <= m - 1; ++i)for(int x,j = 1;j <= n; ++j) {scanf("%d",&x);Add((i - 1) * n + j,i * n + j,x);Add(i * n + j,(i - 1) * n + j,x);}for(int i = 1;i <= m - 1; ++i)for(int x,j = 1;j <= n - 1; ++j) {scanf("%d",&x);Add((i - 1) * n + j,i * n + j + 1,x);Add(i * n + j + 1,(i - 1) * n + j,x);}int temp,max_flow = 0;while(BFS())while(temp = Dinic(S,INF),temp)max_flow += temp;cout << max_flow;return 0;}inline void Add(int x,int y,int f){next[++total] = head[x];aim[total] = y;flow[total] = f;head[x] = total;}inline bool BFS(){static queue<int> q;while(!q.empty())q.pop();memset(deep,0,sizeof(deep));q.push(S);deep[S] = 1;while(!q.empty()) {int x = q.front(); q.pop();for(int i = head[x];i;i = next[i]) if(!deep[aim[i]] && flow[i]) {deep[aim[i]] = deep[x] + 1;q.push(aim[i]);if(aim[i] == T)return true;}}return false;}int Dinic(int x,int f){int temp = f;if(x == T)return f;for(int i = head[x];i;i = next[i])if(deep[aim[i]] == deep[x] + 1 && flow[i] && temp) {int away = Dinic(aim[i],min(flow[i],temp));if(!away)deep[aim[i]] = 0;flow[i] -= away;flow[i^1] += away;temp -= away;}return f - temp;}
Bzoj 1001 Beijing 2006 wolf catch rabbit minimum cut