HDU 1565 1569 方格取数(最大点权独立集)

Posted yfceshi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 1565 1569 方格取数(最大点权独立集)相关的知识,希望对你有一定的参考价值。

HDU 1565 1569 方格取数(最大点权独立集)

题目链接

题意:中文题

思路:最大点权独立集 = 总权值 - 最小割 = 总权值 - 最大流

那么原图周围不能连边,那么就能够分成黑白棋盘。源点连向黑点。白点连向汇点,容量都为点容量。然后黑白之间相邻的就连一条容量无限大的边

代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;

const int MAXNODE = 2505;
const int MAXEDGE = 100005;

typedef int Type;
const Type INF = 0x3f3f3f3f;

struct Edge {
	int u, v;
	Type cap, flow;
	Edge() {}
	Edge(int u, int v, Type cap, Type flow) {
		this->u = u;
		this->v = v;
		this->cap = cap;
		this->flow = flow;
	}
};

struct Dinic {
	int n, m, s, t;
	Edge edges[MAXEDGE];
	int first[MAXNODE];
	int next[MAXEDGE];
	bool vis[MAXNODE];
	Type d[MAXNODE];
	int cur[MAXNODE];
	vector<int> cut;

	void init(int n) {
		this->n = n;
		memset(first, -1, sizeof(first));
		m = 0;
	}
	void add_Edge(int u, int v, Type cap) {
		edges[m] = Edge(u, v, cap, 0);
		next[m] = first[u];
		first[u] = m++;
		edges[m] = Edge(v, u, 0, 0);
		next[m] = first[v];
		first[v] = m++;
	}

	bool bfs() {
		memset(vis, false, sizeof(vis));
		queue<int> Q;
		Q.push(s);
		d[s] = 0;
		vis[s] = true;
		while (!Q.empty()) {
			int u = Q.front(); Q.pop();
			for (int i = first[u]; i != -1; i = next[i]) {
				Edge& e = edges[i];
				if (!vis[e.v] && e.cap > e.flow) {
					vis[e.v] = true;
					d[e.v] = d[u] + 1;
					Q.push(e.v);
				}
			}
		}
		return vis[t];
	}

	Type dfs(int u, Type a) {
		if (u == t || a == 0) return a;
		Type flow = 0, f;
		for (int &i = cur[u]; i != -1; i = next[i]) {
			Edge& e = edges[i];
			if (d[u] + 1 == d[e.v] && (f = dfs(e.v, min(a, e.cap - e.flow))) > 0) {
				e.flow += f;
				edges[i^1].flow -= f;
				flow += f;
				a -= f;
				if (a == 0) break;
			}
		}
		return flow;
	}

	Type Maxflow(int s, int t) {
		this->s = s; this->t = t;
		Type flow = 0;
		while (bfs()) {
			for (int i = 0; i < n; i++)
				cur[i] = first[i];
			flow += dfs(s, INF);
		}
		return flow;
	}

	void MinCut() {
		cut.clear();
		for (int i = 0; i < m; i += 2) {
			if (vis[edges[i].u] && !vis[edges[i].v])
				cut.push_back(i);
		}
	}
} gao;

const int N = 55;
const int d[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};

int n, m, g[N][N], sum;

int main() {
	while (~scanf("%d%d", &n, &m)) {
		sum = 0;
		for (int i = 0; i < n; i++)
			for (int j = 0; j < m; j++) {
				scanf("%d", &g[i][j]);
				sum += g[i][j];
			}
		gao.init(n * m + 2);
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if ((i + j) % 2 == 0) {
					gao.add_Edge(0, i * m + j + 1, g[i][j]);
					for (int k = 0; k < 4; k++) {
						int x = i + d[k][0];
						int y = j + d[k][1];
						if (x < 0 || x >= n || y < 0 || y >= m) continue;
						gao.add_Edge(i * m + j + 1, x * m + y + 1, g[i][j] + g[x][y]);
					}
				} else gao.add_Edge(i * m + j + 1, n * m + 1, g[i][j]);
			}
		}
		printf("%d\n", sum - gao.Maxflow(0, n * m + 1));
	}
	return 0;
}


以上是关于HDU 1565 1569 方格取数(最大点权独立集)的主要内容,如果未能解决你的问题,请参考以下文章

HDU 1569 方格取数 << 最大点权独立集

HDU 1565 - 方格取数 - [状压DP][网络流 - 最大点权独立集和最小点权覆盖集]

HDU 1569 - 方格取数 - [最大点权独立集与最小点权覆盖集]

HDU 1565&1569 方格取数系列(状压DP或者最大流)

hdu 1565&hdu 1569(网络流--最小点权值覆盖)

HDU1569 最大流(最大点权独立集)