[Bob]Collectors Problem
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的更多相关文章
- 湘潭校赛 Bob's Problem
Bob's Problem Accepted : 18 Submit : 115 Time Limit : 1000 MS Memory Limit : 65536 KB 题目描述 Bob今 ...
- UVA10779 Collectors Problem
题目链接:https://cn.vjudge.net/problem/UVA-10779 前言: 本题是关于姜志豪<网络流的一些建模方法>的笔记. 知识点: 最大流 题意摘抄: \(Bob ...
- UVa10779 Collectors Problem(最大流)
很容易想到源点向所类型有贴纸连边,容量为Bob一开始有的数量:然后贴纸向汇点连边,容量为1. 接下来就是交换部分的连边了.注意交换一次一次进行,每次只能交换一张. 交换,是对于两种贴纸而言,仅会发生在 ...
- UVA 10779 Collectors Problem(最大流)
这个题是很难往网络流上面构思的... 从s向每个物品增加容量为Bob拥有数的弧,然后从每个物品向t增加容量为1的弧(代表种类个数).这时候跑最大流的话,得到的肯定是Bob拥有的初始种类数.那么交换后的 ...
- UVA-10779 Collectors Problem (网络流建模)
题目大意:有n个人,已知每人有ki个糖纸,并且知道每张糖纸的颜色.其中,Bob希望能和同伴交换使得手上的糖纸数尽量多.他的同伴只会用手上的重复的交换手上没有的,并且他的同伴们之间不会产生交换.求出Bo ...
- UVA-10779 Collectors Problem
https://vjudge.net/problem/UVA-10779 题意:n个人,m种贴纸,每个人开始有一些贴纸 第一个人可以跟任何人交换任何贴纸 其余人只能用重复的贴纸 跟第一个人交换他们没有 ...
- AC日记——Collectors Problem uva 10779
UVA - 10779 思路: 最大流: s向所有的贴纸的种类连边,流量为Bob拥有的数量: 然后,Bob的朋友如果没有这种贴纸,则这种贴纸向bob的朋友连边,容量1: 如果bob的朋友的贴纸很多大于 ...
- UVA10779 Collectors Problem 【迁移自洛谷博客】
这是一道不错的练最大流建模的基础题. 这种题目审题是关键. Bob's friends will only exchange stickers with Bob, and they will give ...
- uva 10779 Collectors Problem 网络流
链接 一共有n个人, m种收藏品, 每个人拥有的收藏品的种类和个数都是不相同的. 假设2-n这些人都只和1互相交换, 比例是1:1, 并且, 2-n这些人, 只换自己现在没有的, 如果他现在有第二种, ...
随机推荐
- C语言的关键字和数据类型
C语言关键字 C语言数据类型
- go 数据渲染到终端 01
package main import ( "fmt" "text/template" "os" ) type Person struct ...
- VMWare安装Ubuntu16.04
一 概述 VMware Workstation 12的安装(略过,自行百度) Ubuntu16.04的安装 克隆出多个镜像 二 Ubuntu16.04的安装 1 准备 Window10 专业版(关闭H ...
- 什么是HTTPS协议
一 HTTPS协议一直是web开发,无论前后端都不可或缺的重要知识点,然而由于历史原因,这个协议和知识点枯燥而繁多,如果看书和文字十分难懂苦涩.但又不得不掌握,怎么办呢? 正好,从朋友小灰那里得到一片 ...
- restTemplate源码解析(目录)
restTemplate是spring实现的,基于restful风格的http请求模板.使用restTemplate可以简化请求操作的复杂性,同时规范了代码风格.本系列文章,将根据以下目录顺序,从源码 ...
- md5 helper
public static string ToMD5Hash(this string str) { if (string.IsNullOrEmpty(str)) return null; return ...
- mysql把A表数据插入到B表数据的几种方法
web开发中,我们经常需要将一个表的数据插入到另外一个表,有时还需要指定导入字段,设置只需要导入目标表中不存在的记录,虽然这些都可以在程序中拆分成简单sql来实现,但是用一个sql的话,会节省大量代码 ...
- Lumen5.6使用JWT【最新教程】,亲身失败百次的总结
一.前言 如果需要使用 Passport,可以参考在下之前的教程: 'Lumen5.4配置OAuth2.0[强迫症,就是要用最新版本的Lumen]' . 由于原作者文档的简洁性,同时 Lumen 下的 ...
- vue简单todolist
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Oracle死锁处理实例
Oracle死锁常规语句 1.查询Oracle死锁常规语句 SELECT l.session_id sid, s.serial#, l.locked_mode,l.oracle_username, l ...