P3866 [TJOI2009]战争游戏 最小割

$ \color{#0066ff}{ 题目描述 }$

小R正在玩一个战争游戏。游戏地图是一个M行N列的矩阵,每个格子可能是障碍物,也可能是空地,在游戏开始时有若干支敌军分散在不同的空地格子中。每支敌军都可以从当前所在的格子移动到四个相邻的格子之一,但是不能移动到包含障碍物的格子。如果敌军移动出了地图的边界,那么战争就失败了。

现在你的任务是,在敌军开始移动前,通过飞机轰炸使得某些原本是空地的格子变得不可通行,这样就有可能阻止敌军移出地图边界(出于某种特殊的考虑,你不能直接轰炸敌军所在的格子)。由于地形不同的原因,把每个空地格子轰炸成不可通行所需的炸弹数目可能是不同的,你需要计算出要阻止敌军所需的最少的炸弹数。

\(\color{#0066ff}{输入格式}\)

输入文件的第一行包含两个数M和N,分别表示矩阵的长和宽。接下来M行,每行包含用空格隔开的N个数字,每个数字表示一个格子的情况:若数字为-1,表示这个格子是障碍物;若数字为0,表示这个格子里有一支敌军;若数字为一个正数x,表示这个格子是空地,且把它轰炸成不可通行所需的炸弹数为x。

地图上的敌军数量不为1,及地图上有多个0

\(\color{#0066ff}{输出格式}\)

输出一个数字,表示所需的最少炸弹数。数据保证有解存在

\(\color{#0066ff}{输入样例}\)

4 3
1 2 1
1 10 1
1 0 -1
1 1 1

\(\color{#0066ff}{输出样例}\)

6

\(\color{#0066ff}{数据范围与提示}\)

对50%的数据,1 ≤ M,N ≤ 10

对100%的数据,1 ≤ M,N ≤ 30

矩阵里的每个数不超过100

\(\color{#0066ff}{题解}\)

简单来说就是让一些路径不连通,再加上数据范围,那就是最小割了

把这些点都拆成两个点,这两个点之间的边当且仅当可以轰炸,连轰炸的代价的容量,否则连inf代表不可割

起点向所有有敌人的格子连,还是不能割的inf,所有边界格子向t连inf的边,跑最小割就行了

吐槽一句博客园,zhayao居然是关键字,我只好改成了炸弹qwq

#include<bits/stdc++.h>
#define LL long long
LL in() {
	char ch; LL x = 0, f = 1;
	while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
	for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
	return x * f;
}
template<class T> bool chkmax(T &a, const T &b) { return a < b? a = b, 1 : 0; }
template<class T> bool chkmin(T &a, const T &b) { return b < a? a = b, 1 : 0; }
const int maxn = 1e5 + 10;
const int inf = 0x7fffffff;
struct node {
	int to, can;
	node *nxt, *rev;
	node(int to = 0, int can = 0, node *nxt = NULL): to(to), can(can), nxt(nxt) { rev = NULL; }
};
node *head[maxn], *cur[maxn];
int dep[maxn];
int rx[] = {1, -1, 0, 0};
int ry[] = {0, 0, 1, -1};
int n, m, s, t;
void add(int from, int to, int can) {
	head[from] = new node(to, can, head[from]);
}
void link(int from, int to, int can) {
	add(from, to, can), add(to, from, 0);
	head[from]->rev = head[to], head[to]->rev = head[from];
}
bool bfs() {
	for(int i = s; i <= t; i++) dep[i] = 0, cur[i] = head[i];
	std::queue<int> q; q.push(s); dep[s] = 1;
	while(!q.empty()) {
		int tp = q.front(); q.pop();
		for(node *i = head[tp]; i; i = i->nxt)
			if(!dep[i->to] && i->can)
				q.push(i->to), dep[i->to] = dep[tp] + 1;
	}
	return dep[t];
}
int dfs(int x, int change) {
	if(x == t || !change) return change;
	int flow = 0, ls;
	for(node *&i = cur[x]; i; i = i->nxt) 
		if(dep[i->to] == dep[x] + 1 && (ls = dfs(i->to, std::min(change, i->can)))) {
			flow += ls;
			change -= ls;
			i->can -= ls;
			i->rev->can += ls;
			if(!change) break;
		}
	return flow;
}
int dinic() {
	int flow = 0;
	while(bfs()) flow += dfs(s, inf);
	return flow;
}
int id(int x, int y) { return (x - 1) * m + y; }
int main() {
	n = in(), m = in();
	s = 0, t = n * m * 2 + 1;
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++) {
			int x = in();
			if(x == -1) continue;
			for(int k = 0; k < 4; k++) {
				int xx = i + rx[k];
				int yy = j + ry[k];
				if(xx >= 1 && xx <= n && yy >= 1 && yy <= m) link(id(i, j) + n * m, id(xx, yy), inf);
			}
			if(x == 0) link(s, id(i, j), inf), link(id(i, j), id(i, j) + n * m, inf);
			if(x > 0) link(id(i, j), id(i, j) + n * m, x);
			if(i == 1 || i == n || j == 1 || j == m) link(id(i, j) + n * m, t, inf);
		}
	printf("%d\n", dinic());
	return 0;
}
posted @ 2019-04-10 19:41  olinr  阅读(191)  评论(0编辑  收藏  举报