uva 10779 Collectors Problem 网络流
一共有n个人, m种收藏品, 每个人拥有的收藏品的种类和个数都是不相同的。 假设2-n这些人都只和1互相交换, 比例是1:1, 并且, 2-n这些人, 只换自己现在没有的, 如果他现在有第二种, 那么他就不会在去和别人换这一种。比如说第一个人有6个第一种, 2个第三种, 第二个人有2个第一种, 1个第二种, 那么第二个人就不会去和第一个人交换第二种, 而是去交换第三种。
问第一个人最后可以得到多少种不同的收藏品。
首先, 对于第一个人所拥有的收藏品, 建边(s, i+n, num[i]), 对于第2-n个人, 如果他所拥有的某种收藏品j的个数>1, 那么建边(i, j+n, num[j]-1), 如果没有某种收藏品, 那么建边(j+n, i, 1)。 对于所有收藏品, 建边(i+n, t, 1)。 最后跑一遍最大流即为答案。
#include<bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 2e5+;
int cnt[], num, q[maxn*], head[maxn*], dis[], s, t;
struct node
{
int to, nextt, c;
node(){}
node(int to, int nextt, int c):to(to), nextt(nextt), c(c){}
}e[maxn*];
int bfs() {
mem(dis);
int st = , ed = ;
dis[s] = ;
q[ed++] = s;
while(st<ed) {
int u = q[st++];
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(!dis[v]&&e[i].c) {
dis[v] = dis[u]+;
if(v == t)
return ;
q[ed++] = v;
}
}
}
return ;
}
int dfs(int u, int limit) {
int cost = ;
if(u == t)
return limit;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(e[i].c&&dis[v] == dis[u]+) {
int tmp = dfs(v, min(e[i].c, limit-cost));
if(tmp>) {
e[i].c -= tmp;
e[i^].c += tmp;
cost += tmp;
if(cost == limit)
break;
} else {
dis[v] = -;
}
}
}
return cost;
}
int dinic() {
int ans = ;
while(bfs()) {
ans += dfs(s, inf);
}
return ans;
}
void add(int u, int v, int c) {
e[num] = node(v, head[u], c); head[u] = num++;
e[num] = node(u, head[v], ); head[v] = num++;
}
void init() {
mem1(head);
num = ;
}
int main()
{
int T, m, n, x, y;
cin>>T;
for(int casee = ; casee <= T; casee++) {
cin>>n>>m;
init();
s = , t = n+m+;
for(int i = ; i<=n; i++) {
scanf("%d", &x);
mem(cnt);
while(x--) {
scanf("%d", &y);
cnt[y]++;
}
if(i == ) {
for(int j = ; j<=m; j++) {
if(cnt[j])
add(s, j+n, cnt[j]);
}
} else {
for(int j = ; j<=m; j++) {
if(cnt[j]>) {
add(i, j+n, cnt[j]-);
}
if(cnt[j] == ) {
add(j+n, i, );
}
}
}
}
for(int j = ; j<=m; j++) {
add(j+n, t, );
}
printf("Case #%d: ", casee);
int ans = dinic();
cout<<ans<<endl;
}
}
uva 10779 Collectors Problem 网络流的更多相关文章
- UVA 10779 Collectors Problem(最大流)
这个题是很难往网络流上面构思的... 从s向每个物品增加容量为Bob拥有数的弧,然后从每个物品向t增加容量为1的弧(代表种类个数).这时候跑最大流的话,得到的肯定是Bob拥有的初始种类数.那么交换后的 ...
- AC日记——Collectors Problem uva 10779
UVA - 10779 思路: 最大流: s向所有的贴纸的种类连边,流量为Bob拥有的数量: 然后,Bob的朋友如果没有这种贴纸,则这种贴纸向bob的朋友连边,容量1: 如果bob的朋友的贴纸很多大于 ...
- UVA 10779 (最大流)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33631 题目大意:Bob有一些贴纸,他可以和别人交换,他可以把自己 ...
- UVA 10480 Sabotage (网络流,最大流,最小割)
UVA 10480 Sabotage (网络流,最大流,最小割) Description The regime of a small but wealthy dictatorship has been ...
- uva 11991 - Easy Problem from Rujia Liu?(STL)
option=com_onlinejudge&Itemid=8&page=show_problem&problem=3142" target="_blank ...
- CJOJ 2485 UVa 11991 生日礼物 / UVa 11991 Easy Problem from Rujia Liu?
CJOJ 2485 UVa 11991 生日礼物 / UVa 11991 Easy Problem from Rujia Liu? Description (原题来自刘汝佳<训练指南>Pa ...
- UVA-10779 Collectors Problem (网络流建模)
题目大意:有n个人,已知每人有ki个糖纸,并且知道每张糖纸的颜色.其中,Bob希望能和同伴交换使得手上的糖纸数尽量多.他的同伴只会用手上的重复的交换手上没有的,并且他的同伴们之间不会产生交换.求出Bo ...
- UVa 11082 Matrix Decompressing - 网络流
开始眨眼一看怎么也不像是网络流的一道题,再怎么看也觉得像是搜索.不过虽然这道题数据范围很小,但也不至于搜索也是可以随随便便就可以过的.(不过这道题应该是special judge,因为一题可以多解而且 ...
- 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, ...
随机推荐
- bootstrap注意事项(三)
1.<code> 标签用于表示计算机源代码或者其他机器可以阅读的文本内容.软件代码的编写者已经习惯了编写源代码时文本表示的特殊样式.<code> 标签就是为他们设计的.包含在该 ...
- IOS app启动过程
1.main函数 2.UIApplicationMain * 创建UIApplication对象 * 创建UIApplication的delegate对象 3.delegate对象开始处理(监 ...
- [C++空间分配]new运算符、operator new、placement new的区别于联系
先科普一下: 1. new的执行过程: (1)通过operator new申请内存 (2)使用placement new调用构造函数(内置类型忽略此步) (3)返回内存指针 2. new和malloc ...
- PHP搭建简单暴力的mvc
对于一个web系统来说,我们使用mvc很必要, 给我们带来的是清晰的结构,易运维,易扩展, mvc 我对其的理解应该叫mxvc, 多了一个x , 这个x代表什么,x可以理解为 relay,proxy, ...
- OC语法6——内存管理之引用计数器(retain,release)
OC内存管理: 一.引用计数器: Java有垃圾回收机制(Garbage Collection,GC).也就是说当我们创建对象后,不需要考虑回收内存的事,Java的垃圾回收机制会自动销毁该对象,回收它 ...
- VueJS搭建简单后台管理系统框架(一)环境搭建
做这份笔记的时候,Vue.js还是1.0,10月份Vue已经升级到2.0,这边也进行相应的更新,用Vue2.0进行一个简单的环境搭建 饿了么也刚好开源了其基于Vue.js的UI框架element-ui ...
- Javascript 字符串浏览器兼容问题
先看下不兼容的写法,若我想获取某个字符串的第几位 var str='aavvvcc'; console.info(str[0]); 这种写法 在IE 7以下的浏览器都不兼容,以下提供浏览器全兼容的方式 ...
- struts中简单的校验
Struts中简单的校验 “计应134(实验班) 凌豪” Struts2校验简要说明:struts2中通常情况下,类型转换要在数据校验之前进行.类型转换其实也是基本的服务器端校验,合法数据必然可以通过 ...
- Sencha Touch对DOM的访问及控制
HTML代码: <!doctype html> <html> <head> <meta charset="utf-8"> <t ...
- “Options模式”下的配置是如何绑定为Options对象
“Options模式”下的配置是如何绑定为Options对象 配置的原子结构就是单纯的键值对,并且键和值都是字符串,但是在真正的项目开发中我们一般不会单纯地以键值对的形式来使用配置.值得推荐的做法就是 ...