这个题是很难往网络流上面构思的。。。

从s向每个物品增加容量为Bob拥有数的弧,然后从每个物品向t增加容量为1的弧(代表种类个数)。这时候跑最大流的话,得到的肯定是Bob拥有的初始种类数。那么交换后的最大数呢?

对于Bob以外的小伙伴,如果i拥有j物品超过1个(交换后他自己至少保留一个),从人节点i向物品节点j增加容量为num-1的弧,表示他能输出多少物品,而如果i没有j物品,那么从物品节点j向人节点i增加容量为1的弧(他最多接受1单位的物品)。然后跑最大流得到的就是答案了。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<bitset>
#include<vector>
#include<string>
#include<cstdio>
#include<cmath>
#include<stack>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define FF(i, a, b) for(int i=a; i<b; i++)
#define FD(i, a, b) for(int i=a; i>=b; i--)
#define REP(i, n) for(int i=0; i<n; i++)
#define CLR(a, b) memset(a, b, sizeof(a))
#define debug puts("**debug**")
#define LL long long
#define PB push_back
using namespace std; const int maxn = 300;
const int INF = 1e9; int n, m, s, t, num[11][30];
int d[maxn], cur[maxn];
bool vis[maxn]; struct Edge
{
int from, to, cap, flow;
};
vector<Edge> edges;
vector<int> G[maxn]; void init()
{
s = 0, t = n + m + 1; CLR(num, 0);
REP(i, t+1) G[i].clear(); edges.clear();
} void add(int from, int to, int cap)
{
edges.PB((Edge){from, to, cap, 0});
edges.PB((Edge){to, from, 0, 0});
int nc = edges.size();
G[from].PB(nc-2); G[to].PB(nc-1);
} bool bfs()
{
CLR(vis, 0);
queue<int> q; q.push(s);
d[s] = 0, vis[s] = 1;
while(!q.empty())
{
int x = q.front(); q.pop();
int nc = G[x].size();
REP(i, nc)
{
Edge e = edges[G[x][i]];
if(!vis[e.to] && e.cap > e.flow)
{
vis[e.to] = 1;
d[e.to] = d[x] + 1;
q.push(e.to);
}
}
}
return vis[t];
} int dfs(int x, int a)
{
if(x == t || a == 0) return a;
int flow = 0, f, nc = G[x].size();
for(int& i = cur[x]; i<nc; i++)
{
Edge& e = edges[G[x][i]];
if(d[x] + 1 == d[e.to] && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0)
{
e.flow += f;
edges[G[x][i]^1].flow -= f;
flow += f;
a -= f;
if(a == 0) break;
}
}
return flow;
} int max_flow()
{
int flow = 0;
while(bfs())
{
CLR(cur, 0);
flow += dfs(s, INF);
}
return flow;
} int main()
{
int T; scanf("%d", &T);
FF(kase, 1, T+1)
{
scanf("%d%d", &n, &m);
init();
int x;
REP(i, n)
{
scanf("%d", &num[i][0]);
while(num[i][0]--)
{
scanf("%d", &x);
num[i][x]++;
}
}
FF(i, 1, m+1)
{
if(num[0][i]) add(s, i+n, num[0][i]);
add(i+n, t, 1);
}
FF(i, 1, n)
{
FF(j, 1, m+1)
{
if(num[i][j] > 1) add(i, j+n, num[i][j] - 1);
if(num[i][j] == 0) add(j+n, i, 1);
}
}
printf("Case #%d: %d\n", kase, max_flow());
}
return 0;
}

UVA 10779 Collectors Problem(最大流)的更多相关文章

  1. uva 10779 Collectors Problem 网络流

    链接 一共有n个人, m种收藏品, 每个人拥有的收藏品的种类和个数都是不相同的. 假设2-n这些人都只和1互相交换, 比例是1:1, 并且, 2-n这些人, 只换自己现在没有的, 如果他现在有第二种, ...

  2. AC日记——Collectors Problem uva 10779

    UVA - 10779 思路: 最大流: s向所有的贴纸的种类连边,流量为Bob拥有的数量: 然后,Bob的朋友如果没有这种贴纸,则这种贴纸向bob的朋友连边,容量1: 如果bob的朋友的贴纸很多大于 ...

  3. UVA 10779 (最大流)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33631 题目大意:Bob有一些贴纸,他可以和别人交换,他可以把自己 ...

  4. uva 11991 - Easy Problem from Rujia Liu?(STL)

    option=com_onlinejudge&Itemid=8&page=show_problem&problem=3142" target="_blank ...

  5. CJOJ 2485 UVa 11991 生日礼物 / UVa 11991 Easy Problem from Rujia Liu?

    CJOJ 2485 UVa 11991 生日礼物 / UVa 11991 Easy Problem from Rujia Liu? Description (原题来自刘汝佳<训练指南>Pa ...

  6. UVa10779 Collectors Problem(最大流)

    很容易想到源点向所类型有贴纸连边,容量为Bob一开始有的数量:然后贴纸向汇点连边,容量为1. 接下来就是交换部分的连边了.注意交换一次一次进行,每次只能交换一张. 交换,是对于两种贴纸而言,仅会发生在 ...

  7. Risk UVA - 12264 拆点法+最大流+二分 最少流量的节点流量尽量多。

    /** 题目:Risk UVA - 12264 链接:https://vjudge.net/problem/UVA-12264 题意:给n个点的无权无向图(n<=100),每个点有一个非负数ai ...

  8. UVA 820 --- POJ 1273 最大流

    找了好久这两个的区别...UVA820 WA了 好多次.不过以后就做模板了,可以求任意两点之间的最大流. UVA 是无向图,因此可能有重边,POJ 1273是有向图,而且是单源点求最大流,因此改模板的 ...

  9. UVa 11991:Easy Problem from Rujia Liu?(STL练习,map+vector)

    Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, ...

随机推荐

  1. [Unity菜鸟] 术语

    HUD Mozilla  Mozilla基金会,简称Mozilla(缩写MF或MoFo),是为支持和领导开源的Mozilla项目而设立的一个非营利组织. 称作Mozilla公司的子公司,雇佣了一些Mo ...

  2. 工具----IcoFX

    IcoFX IcoFX 是一款免费的图标编辑工具,让您轻松创建 Windows XP 和 Windows Vista 图标. 在编辑区您可以轻松的预览.保存.更改您的图标.您可以将您喜欢的图像转换为图 ...

  3. HTML5学习(九)----应用程序缓存

    参考教程:http://www.w3school.com.cn/html5/html_5_app_cache.asp 使用 HTML5,通过创建 cache manifest 文件,可以轻松地创建 w ...

  4. naotu.baidu.com 非常棒的脑图在线工具

    1.png 2.txt 短租 前台功能 房源查看 房源搜索 城市房源 注册登录 预定房源 房源退订 在线支付 评价房源 个人中心 我的订单 我的账户 我的收藏 消息通知 管理员后台 房源发布 会员管理 ...

  5. poj 2503 Babelfish (查找 map)

    题目:http://poj.org/problem?id=2503 不知道为什么 poj  的 数据好像不是100000,跟周赛的不一样 2000MS的代码: #include <iostrea ...

  6. poj 3267 The Cow Lexicon(dp)

    题目:http://poj.org/problem?id=3267 题意:给定一个字符串,又给n个单词,求最少删除字符串里几个字母,能匹配到n个单词里 #include <iostream> ...

  7. 函数fsp_seg_inode_page_find_free

    /**********************************************************************//** Looks for an unused segm ...

  8. 结构体 typedef struct hash_cell_struct hash_cell_t;

    typedef struct hash_cell_struct hash_cell_t; struct hash_cell_struct{ void* node; /*!< hash chain ...

  9. poj2828

    很容易想到一种动态的做法:平衡树…… 或者是二分+树状数组 但,前者编程复杂度较大,而且据说会被卡(没试过):后者理论上超时(据说可以擦边过?): 所以要尝试新的算法: 倒着考虑,显然最后一个对象的位 ...

  10. 使用Amoeba 实现MySQL DB 读写分离

    Amoeba(变形虫)项目是一个开源框架,于2008年开始发布一款 Amoeba for Mysql软件: 这个软件致力于MySQL的分布式数据库前端代理层,它主要在应用层访问MySQL的时候充当SQ ...