码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 10779 - Collectors Problem(网络流)

时间:2014-09-18 16:30:14      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   2014   

UVA 10779 - Collectors Problem

题目链接

题意:每个人有一种贴图,现在第0个人要去和别人交换贴图,来保证自己的贴图尽量多,只有别人没有该种贴图,并且自己有2张以上另一种贴图才会换,问最多有几张贴图

思路:最大流,关键在于如何建模,把0号人和物品连边,容量为有的容量,然后其他人如果物品等于0的,连一条边从物品到这个人,表示能交换,然后如果物品大于1的,连一条边从这个人到物品,容量为物品减1(自己要留一个),然后把所有物品连到汇点,跑一次最大流即可

代码:

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

const int MAXNODE = 1005;
const int MAXEDGE = 100005;

const int INF = 0x3f3f3f3f;

struct Edge {
	int u, v, cap, flow;
	Edge() {}
	Edge(int u, int v, int cap, int 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];
	int d[MAXNODE];
	int cur[MAXNODE];

	void init(int n) {
		this->n = n;
		memset(first, -1, sizeof(first));
		m = 0;
	}

	void add_Edge(int u, int v, int 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];
	}

	int dfs(int u, int a) {
		if (u == t || a == 0) return a;
		int 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;
	}

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

const int N = 30;

int T, n, m, cnt[N];

int main() {
	int cas = 0;
	scanf("%d", &T);
	while (T--) {
		scanf("%d%d", &n, &m);
		int s = 0, t = n + m;
		gao.init(t + 1);
		for (int i = 0; i < n; i++) {
			int tot, tmp;
			memset(cnt, 0, sizeof(cnt));
			scanf("%d", &tot);
			while (tot--) {
				scanf("%d", &tmp);
				cnt[tmp]++;
			}
			if (i == 0) {
				for (int j = 1; j <= m; j++)
					if (cnt[j] > 0) gao.add_Edge(i, j + n - 1, cnt[j]);
			} else {
				for (int j = 1; j <= m; j++) {
					if (cnt[j] > 1) gao.add_Edge(i, j + n - 1, cnt[j] - 1);
					if (cnt[j] == 0) gao.add_Edge(j + n - 1, i, 1);
				}
			}
		}
		for (int i = 1; i <= m; i++)
			gao.add_Edge(i + n - 1, t, 1);
		printf("Case #%d: %d\n", ++cas, gao.Maxflow(s, t));
	}
	return 0;
}


UVA 10779 - Collectors Problem(网络流)

标签:style   blog   http   color   io   os   ar   for   2014   

原文地址:http://blog.csdn.net/accelerator_/article/details/39374407

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!