https://vjudge.net/problem/UVA-10779#author=0

网络流

1.Bob向他有的贴纸连边,流量为他有的贴纸数量

2.每一种贴纸向汇点连流量为1的边

3.其余人,如果没贴纸i,由i向这个人连一条流量为1的边

4.如果贴纸i数量>1,由这个人向i连一条流量为数量-1的边

#include <cstdio>
#include <algorithm>
#include <queue> using namespace std;
const int N = ;
const int M = ; #define gc getchar()
#define oo 999999999 int n, m, now, S, T, TI;
int head[N], now_head[N], calc[N], dis[N];
struct Node{
int u, v, flow, nxt;
}G[M];
queue <int> Q; inline int read(){
int x = ; char c = gc;
while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc;
return x;
} inline void add(int u, int v, int flow){
G[now].v = v; G[now].flow = flow; G[now].nxt = head[u]; head[u] = now ++;
} inline bool bfs(){
for(int i = S; i <= T; i ++) now_head[i] = head[i], dis[i] = -;
dis[S] = ;
while(!Q.empty()) Q.pop();
Q.push(S);
while(!Q.empty()){
int topp = Q.front();
Q.pop();
for(int i = head[topp]; ~ i; i = G[i].nxt){
int v = G[i].v;
if(dis[v] == - && G[i].flow > ){
dis[v] = dis[topp] + ;
if(v == T) return ;
Q.push(v);
}
}
}
return ;
} int dfs(int now, int flow){
if(now == T) return flow;
int ret = ;
for(int & i = now_head[now]; ~ i; i = G[i].nxt){
int v = G[i].v;
if(dis[v] == dis[now] + && G[i].flow > ){
int f = dfs(v, min(G[i].flow, flow - ret));
if(f) {G[i].flow -= f; G[i ^ ].flow += f; ret += f; if(ret == flow) break;}
}
}
if(ret != flow) dis[now] = -;
return ret;
} inline int Dinic(){
int ret = ;
while(bfs()) ret += dfs(S, oo);
return ret;
} int main()
{
TI = read();
for(int Ti = ; Ti <= TI; Ti ++){
n = read(); m = read(); now = ;
T = n + m + ; S = ;
for(int i = ; i <= T; i ++) head[i] = -;
for(int i = ; i <= m; i ++) add(n + i, T, ), add(T, n + i, );
int k = read();
for(int i = ; i <= k; i ++){int im = read(); calc[im] ++;}
for(int i = ; i <= m; i ++) if(calc[i]) add(, n + i, calc[i]), add(n + i, , );
for(int i = ; i <= m; i ++) calc[i] = ;
for(int i = ; i <= n; i ++){
int k = read();
for(int j = ; j <= k; j ++){int im = read(); calc[im] ++;}
for(int j = ; j <= m; j ++)
if(calc[j] > ) add(i, n + j, calc[j] - ), add(n + j, i, );
else if(!calc[j]) add(n + j, i, ), add(i, n + j, );
for(int j = ; j <= m; j ++) calc[j] = ;
}
int answer = Dinic();
printf("Case #%d: %d\n", Ti, answer);
} return ;
}
/*
2
2 5
6 1 1 1 1 1 1
3 1 2 2
3 5
4 1 2 1 1
3 2 2 2
5 1 3 4 4 3
*/

[Bob]Collectors Problem的更多相关文章

  1. 湘潭校赛 Bob's Problem

    Bob's Problem Accepted : 18   Submit : 115 Time Limit : 1000 MS   Memory Limit : 65536 KB  题目描述 Bob今 ...

  2. UVA10779 Collectors Problem

    题目链接:https://cn.vjudge.net/problem/UVA-10779 前言: 本题是关于姜志豪<网络流的一些建模方法>的笔记. 知识点: 最大流 题意摘抄: \(Bob ...

  3. UVa10779 Collectors Problem(最大流)

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

  4. UVA 10779 Collectors Problem(最大流)

    这个题是很难往网络流上面构思的... 从s向每个物品增加容量为Bob拥有数的弧,然后从每个物品向t增加容量为1的弧(代表种类个数).这时候跑最大流的话,得到的肯定是Bob拥有的初始种类数.那么交换后的 ...

  5. UVA-10779 Collectors Problem (网络流建模)

    题目大意:有n个人,已知每人有ki个糖纸,并且知道每张糖纸的颜色.其中,Bob希望能和同伴交换使得手上的糖纸数尽量多.他的同伴只会用手上的重复的交换手上没有的,并且他的同伴们之间不会产生交换.求出Bo ...

  6. UVA-10779 Collectors Problem

    https://vjudge.net/problem/UVA-10779 题意:n个人,m种贴纸,每个人开始有一些贴纸 第一个人可以跟任何人交换任何贴纸 其余人只能用重复的贴纸 跟第一个人交换他们没有 ...

  7. AC日记——Collectors Problem uva 10779

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

  8. UVA10779 Collectors Problem 【迁移自洛谷博客】

    这是一道不错的练最大流建模的基础题. 这种题目审题是关键. Bob's friends will only exchange stickers with Bob, and they will give ...

  9. uva 10779 Collectors Problem 网络流

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

随机推荐

  1. Scala 算法案例

    移除第一个负数之后的所有负数 // 构建数组 val a = ArrayBuffer[Int]() a += (1, 2, 3, 4, 5, -1, -3, -5, -9) // 每发现一个第一个负数 ...

  2. 为什么无人机测量主流现在都不用RTK技术,而是PPK技术【转】

    为什么无人机测量主流现在都不用RTK技术,而是PPK技术_宇辰网_让世界读懂无人机_全球专业无人机资讯|电商|大数据服务平台 大疆Phantom 4 RTK正式发布_宇辰网_让世界读懂无人机_全球专业 ...

  3. 什么是RAID(磁盘阵列)

    RAID全称Redundant Array of Independent Disk,即独立冗余磁盘阵列.RAID技术由加州大学伯克利分校1987年提出,最初是为了组合小的廉价磁盘来代替大的昂贵磁盘,同 ...

  4. springboot2.0+mybatis多数据源集成

    最近在学springboot,把学的记录下来.主要有springboot2.0+mybatis多数据源集成,logback日志集成,springboot单元测试. 一.代码结构如下 二.pom.xml ...

  5. 使用docker搭建reids主从,哨兵。

    Redis主从配置,如果没有真机就要用虚拟机,使用Docke for Windows host网络有问题. 准备: 1.安装虚拟机. 2.下载redis的安装文件:http://download.re ...

  6. Mybatis:缓存

    1.什么是缓存[Cache] 存在内存中的临时数据. 将用户经常查询的数据放在缓存(内存)中,用户去查询数据就不用从磁盘上(关系型数据库数据文件)查询,从缓存中查询,从而提高查询效率,解决了高并发系统 ...

  7. ECMAScript5面向对象技术(2)--函数

    在JavaScript中,函数其实就是对象.使函数不同于其他对象的决定性特点是函数存在一个被称为[[Call]]的内部属性.内部属性无法通过代码访问而是定义了代码执行时的行为.ECMAScript为J ...

  8. Java中异常关键字throw和throws使用方式的理解

    Java中应用程序在非正常的情况下停止运行主要包含两种方式: Error 和 Exception ,像我们熟知的 OutOfMemoryError 和 IndexOutOfBoundsExceptio ...

  9. (坑爹错误)记录prometheus中配置alertmanager.yml一次报错

    global: resolve_timeout: 5m #处理超时时间,默认为5min smtp_smarthost: 'smtp.sina.com:25' # 邮箱smtp服务器代理 smtp_fr ...

  10. CentOS7安装CDH 第十一章:离线升级CDH版本

    相关文章链接 CentOS7安装CDH 第一章:CentOS7系统安装 CentOS7安装CDH 第二章:CentOS7各个软件安装和启动 CentOS7安装CDH 第三章:CDH中的问题和解决方法 ...