UVA 10779 - Collectors Problem(网络流)
来源:互联网 发布:淘宝企业店铺花钱吗 编辑:程序博客网 时间:2024/05/21 15:44
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;}
1 0
- UVA 10779 - Collectors Problem(网络流)
- UVA 10779 - Collectors Problem(网络流)
- UVA 10779 Collectors Problem 网络流+建图
- uva 10779 Collectors Problem(最大流)
- UVA 10779 Collectors Problem(最大流)
- UVA 10779 Collectors Problem (最大流)
- UVa 10779 Collectors Problem
- UVA 10779 Collectors Problem(建模,最大流)
- UVA 10779 Collectors Problem(最大流)
- UVA 10779 Collectors Problem(最大流)
- UVa 10779 Collectors Problem 最大流
- UVA 10779 Collectors Problem 最大流
- UVA - 10779 Collectors Problem(最大流)
- UVa10779 Collectors' Problem( 网络流,建图)
- uva 10779 Collectors Problem(最大流,好题)
- Collectors Problem UVA
- UVa10779 Collectors Problem(最大流)
- [UVa10779]Collectors Problem(最大流)
- 利用QQWry.dat 实现IP 地址高效检索注意事项
- 师傅的放大实打实大师
- 在MyEclipse中配置Oracle10g的连接报错:error while performing database login with the oracle driver
- C#socket编程基础
- equals的用法的需要注意的问题
- UVA 10779 - Collectors Problem(网络流)
- 读取固定格式文本文件到二维数组
- 面试需要准备!!
- Oracle中取余的方法,记录去重复后排序输出
- 关于Gridview中的自动编号
- 八十多分的手法士大夫士大夫
- 关于 视频同步vsync 信号在不同时钟域采样问题
- hashmap,使用iterator读取后顺序改变
- 字符串去空格问题(转)