SRM 577

这题方法很多,我是用的最小割


S -> i -> T

然后S到i边为100,i到T边为100,割左边意思是这个格子的#属于竖着的,右边表示横着的

(为什么选100。。。额,最后还要减去,为了S到i和i到T两条边只被割一条)


如果选的竖着的(i到T的边没被割),那么如果这个格子上面一格不是#,那么要额外花费1的,选横着同理。


如果i在j上面,那么如果i选横的(割右边),j选竖的(割左边),那么需要额外花费1代价,就是以j格为开始的竖着一条。那么就连一条i到j的边。横着的两个格子同理。


最后减去多算的100。

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

class BoardPainting {
public:
	int minimalSteps(vector<string> );
};
#define N 20009
#define M 2000009
#define inf 2000000000
#define INT int
struct E {
	int t, next;
	INT flow, cap;
} edge[M];
int node[N], eid;
vector<int> e[N];
int rr[N];
int que[N];
void init() {
	eid = 0;
	memset(rr, -1, sizeof(rr));
	memset(node, -1, sizeof(node));
}
void addedge(int a, int b, INT c) {
	edge[eid].t = b;
	edge[eid].cap = c;
	edge[eid].flow = 0;
	edge[eid].next = node[a];
	node[a] = eid++;

	edge[eid].t = a;
	edge[eid].cap = 0; //0单向,c双向
	edge[eid].flow = 0;
	edge[eid].next = node[b];
	node[b] = eid++;
}
void addedge1(int a, int b, INT c) {
	edge[eid].t = b;
	edge[eid].cap = c;
	edge[eid].flow = 0;
	edge[eid].next = node[a];
	node[a] = eid++;

	edge[eid].t = a;
	edge[eid].cap = c; //0单向,c双向
	edge[eid].flow = 0;
	edge[eid].next = node[b];
	node[b] = eid++;
}
int bfs(int s, int t, int n) {
	memset(rr, -1, sizeof(rr));
	int i;
	for (i = 0; i < n; ++i)
		e[i].clear();
	int u, v;
	int front = 0, rear = 1;
	rr[s] = 0;
	que[0] = s;
	while (rear > front) {
		u = que[front++];
		for (i = node[u]; i != -1; i = edge[i].next) {
			v = edge[i].t;
			if (rr[v] == -1 && edge[i].cap) {
				que[rear++] = v;
				rr[v] = rr[u] + 1;
			}
			if (rr[v] == rr[u] + 1)
				e[u].push_back(i);
		}
	}
	return (rr[t] != -1);
}
INT dinic(int s, int t, int n) {
	int st[N];
	INT maxflow = 0;
	int aux[N];
	int top, cur;
	int p, i, k;
	while (bfs(s, t, n)) {
		top = 0;
		st[top] = s;
		cur = s;
		while (1) {
			if (cur == t) {
				INT minc = inf;
				for (i = 0; i < top; ++i) {
					p = aux[i + 1];
					if (minc > edge[p].cap)
						minc = edge[p].cap, k = i;
				}
				for (i = 0; i < top; ++i) {
					p = aux[i + 1];
					edge[p].cap -= minc;
					edge[p ^ 1].cap += minc;
				}
				maxflow += minc;
				cur = st[top = k];
			}
			int len = e[cur].size();
			while (len) {
				p = e[cur][len - 1];
				if (edge[p].cap && rr[edge[p].t] == rr[cur] + 1)
					break;
				else {
					len--;
					e[cur].pop_back();
				}
			}
			if (len) {
				cur = st[++top] = edge[p].t;
				aux[top] = p;
			} else {
				if (top == 0)
					break;
				rr[cur] = -1;
				cur = st[--top];
			}
		}
	}
	return maxflow;
}

int BoardPainting::minimalSteps(vector<string> a) {
	int i, j, k;
	int n, m;
	int ans = 0;
	int s, t;
	n = a.size();
	m = a[0].size();
	init();
	s = n * m;
	t = s + 1;
	for (i = 0; i < n; ++i) {
		for (j = 0; j < m; ++j) {
			if (a[i][j] == '#') {
				ans -= 100;
				addedge(s, i * m + j, 100);
				addedge(i * m + j, t, 100);

				if (i == 0 || a[i - 1][j] != '#') {
					addedge(s, i * m + j, 1);
				}
				if (j == 0 || a[i][j - 1] != '#') {
					addedge(i * m + j, t, 1);
				}
				if (i != n - 1 && a[i + 1][j] == '#') {
					addedge(i * m + j, (i + 1) * m + j, 1);
				}
				if (j != m - 1 && a[i][j + 1] == '#') {
					addedge(i * m + j + 1, i * m + j, 1);
				}
			}
		}
	}
	return ans + dinic(s, t, t + 1);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值