NOI2006 最大获利 (最大权闭合图)

题目连接:

bzoj1497: http://www.lydsy.com/JudgeOnline/problem.php?id=1497

题意:

有m个用户群,n个中转站。

满足第i个用户群必须要建造Ai,Bi两个中转站,可以使公司获利Ci。建立第i个中转站的费用是Pi。

问最大获利是多少。


思路:

把用户群和中转站看成n+m个点,每个点有一个权值,用权值的正负表示获利还是花费。

满足用户群vi,必须建造Ai和Bi,连边vi, Ai, vi, Bi。

得到一副图,而本题所求的最大获利显然就是最大权闭合图的权。

于是,s连到每个用户群vi,权值为Ci,每个中转站ui连到t,权值为花费Pi,原图中vi, Ai, vi, Bi的权值全部设为INF。

最大权闭合图的的权 = 原图中权值为正的点的和(所有用户的收益之和) - 最小割(最大流)


代码:

#include cstdio
#include cstring
#include algorithm
#include iostream
#include vector
#include queue

#define INF 0x3f3f3f3f
#define T (n + m + 1)

using namespace std;

struct Edge{
	int from, to, cap;
	Edge() {}
	Edge(int a, int b, int c) : from(a), to(b), cap(c) {}
};

int n, m;
vectorEdge edges;
vectorint G[60005];

void addEdge(int from, int to, int cap) {
	edges.push_back(Edge(from, to, cap));
	edges.push_back(Edge(to, from, 0));
	int siz = edges.size();
	G[from].push_back(siz - 2);
	G[to].push_back(siz - 1);
}

int cur[60005];
int layer[60005];

bool build() {
	memset(layer, -1, sizeof(layer));
	queueint q;
	layer[0] = 0;
	q.push(0);
	while (!q.empty()) {
		int current = q.front();
		q.pop();
		for (int i = 0; i  G[current].size(); i++) {
			Edge e = edges[G[current][i]];
			if (layer[e.to] == -1  e.cap  0) {
				layer[e.to] = layer[current] + 1;
				q.push(e.to);
			}
		}
	}
	return layer[T] != -1;
}

int find(int x, int curFlow) {
	if (x == T || !curFlow) return curFlow;
	int flow = 0, f;
	for (int i = cur[x]; i  G[x].size(); i++) {
		Edge e = edges[G[x][i]];
		if (layer[e.to] == layer[x] + 1
			 (f = find(e.to, min(curFlow, e.cap)))) {
			e.cap -= f;
			edges[G[x][i] ^ 1].cap += f;
			flow += f;
			curFlow -= f;
			if (!curFlow) break;
		}
	}
	return flow;
}

int dinic() {
	int ans = 0;
	while (build()) {
		memset(cur, 0, sizeof(cur));
		ans += find(0, INF);
	}
	return ans;
}

int main() {
	scanf("%d %d", n, m);
	int x;
	for (int i = 1; i = n; i++) {
		scanf("%d", x);
		addEdge(i, T, x);
	}
	int a, b, c;
	int sum = 0;
	for (int i = 1; i = m; i++) {
		scanf("%d %d %d", a, b, c);
		sum += c;
		addEdge(0, i + n, c);
		addEdge(i + n, a, INF);
		addEdge(i + n, b, INF);
	}
	int ans = sum - dinic();
	printf("%d\n", ans);
	return 0;
}


最新回复(0)
/jishuAOJtg5RSBDP89ZBu_2B5R2zhzCCQtM196rzmQH0g_3D_3D4858329
8 简首页