POJ 3281 Dining【拆点+SAP网络流】

POJ 3281 Dining

  • 这里选择使用的是SAP网络流里的Dinic

题意:有n头牛,f个食物,d个饮料。每头牛都有一些自己喜欢的食物和饮料,当且仅当这头牛获得了一个自己喜欢的饮料和一个自己喜欢的食物,他才会开心。求,最多可能有多少牛会开心。每种食物和饮料只有一个。

一种解题思路:使用网络流,试图构建满流数量为答案的网络。
一种建图思路:超级源点S指向所有食物的边的权值为1,被喜欢的食物指向牛的边的权值为1,牛指向自己喜欢的饮料的边的权值为1,所有饮料指向超级汇点T的边的权值为1。这个时候会发现一个问题,现在图中的牛可能会开心好几次QAQ,那么我们不妨把牛的这个点增改成一个权值为1的边,那么就只会开心一次了Hhhhh(具体操作就是,把牛拆成两个点,中间连权值为1的边)。

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define ll long long int
#define LL ll
#define INF 0x3f3f3f3f
const int maxn = 500 + 10;
const int maxe = 1e5;
struct edge {
	int to, nx, w;
}e[maxe];
int sz;
int head[maxn];
int dep[maxn], flow[maxn];
int n, f, d, S, T;
int F[maxn][maxn]; int D[maxn][maxn];
int numf[maxn]; int numd[maxn];
void init() {
	sz = 0;
	memset(head, -1, sizeof head);
}
void add(int u, int v, int w) {
	e[sz].to = v;
	e[sz].nx = head[u];
	e[sz].w = w;
	head[u] = sz++; // sz ^ 1 = sz + 1(so sz must be an even number) sz ^ 1 is sz's reverse edge.

	e[sz].to = u;
	e[sz].nx = head[v];
	e[sz].w = 0;
	head[v] = sz++; // add reverse edge.
}
bool bfs() {
	memset(dep, 0, sizeof dep);
	dep[S] = 1; // S = 0
	queue<int> Q;
	Q.push(S); // S = 0
	while (!Q.empty()) {
		int nw = Q.front();
		Q.pop();
		for (int i = head[nw]; i != -1; i = e[i].nx) {
			int v = e[i].to;
			if (!dep[v] && e[i].w) { // exist room & haven't been visited
				dep[v] = dep[nw] + 1;
				Q.push(v);
			}
		}
	}
	return dep[T];// if dep[T] == 0, means no roads to T now;
}
int dfs(int nw, int flow) {
	if (flow == 0 || nw == T) return flow;
	int res = 0;
	for (int i = head[nw]; i != -1; i = e[i].nx) {
		//head[nw] = i;当前弧优化(比较弱的优化)
		int v = e[i].to;
		if (e[i].w&&dep[v] == dep[nw] + 1) { // find the road
			int cur = dfs(v, min(flow, e[i].w));
			flow -= cur; res += cur;
			e[i].w -= cur; e[i ^ 1].w += cur;
		}
	}
	if (!res) dep[nw] = 0; // the cutting of boom the node
	return res;
}
int dinic() {
	int res = 0;
	while (bfs()) {
		res += dfs(S, INF);
		//cout << res << endl;
	}
	return res;
}

void build() {
	S = 0; T = n + n + f + d + 1;
	// food first, cow second, drink last.
	for (int i = 1; i <= f; i++) { // S to food
		add(S, i, 1);
	}
	for (int i = f + 1; i <= f + n; i++) { // cow to cow'
		int u = i; int v = u + n;
		add(u, v, 1);
	}
	for (int i = 1; i <= n; i++) {
		int k1 = numf[i];
		int k2 = numd[i];
		for (int j = 1; j <= k1; j++) {// the food cow i like
			int u = F[i][j];
			int v = f + i;
			add(u, v, 1);
			//cout << u << " " << v << endl;
		}
		for (int j = 1; j <= k2; j++) {// the drink cow i like
			int u = f + i + n;
			int v = f + n + n + D[i][j];
			add(u, v, 1);
			//cout << u << " " << v << endl;
		}
	}
	for (int i = f + n + n + 1; i <= f + n + n + d; i++) { // drink to T
		add(i, T, 1);
	}
}
int main()
{
	init();
	scanf("%d %d %d", &n, &f, &d);
	for (int i = 1; i <= n; i++) {
		scanf("%d %d", &numf[i], &numd[i]);
		for (int j = 1; j <= numf[i]; j++) {
			scanf("%d", &F[i][j]);
		}
		for (int j = 1; j <= numd[i]; j++) {
			scanf("%d", &D[i][j]);
		}
	}
	/*for (int i = 1; i <= n; i++) {
		cout << numf[i] << " " << numd[i]<<" ";
		for (int j = 1; j <= numf[i]; j++) {
			cout << F[i][j] << " ";
		}
		for (int j = 1; j <= numd[i]; j++) {
			cout << D[i][j] << " ";
		}
		cout << endl;
	}*/
	build();
	int ans = dinic();
	printf("%d\n", ans);

	return 0;
}
//_CRT_SECURE_NO_WARNINGS

你可能感兴趣的:(Algorithm)