1711: [Usaco2007 Open]Dining吃饭

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 902  Solved: 476
[Submit][Status][Discuss]

Description

农夫JOHN为牛们做了很好的食品,但是牛吃饭很挑食. 每一头牛只喜欢吃一些食品和饮料而别的一概不吃.虽然他不一定能把所有牛喂饱,他还是想让尽可能多的牛吃到他们喜欢的食品和饮料. 农夫JOHN做了F (1 <= F <= 100) 种食品并准备了D (1 <= D <= 100) 种饮料. 他的N (1 <= N <= 100)头牛都以决定了是否愿意吃某种食物和喝某种饮料. 农夫JOHN想给每一头牛一种食品和一种饮料,使得尽可能多的牛得到喜欢的食物和饮料. 每一件食物和饮料只能由一头牛来用. 例如如果食物2被一头牛吃掉了,没有别的牛能吃食物2.

Input

* 第一行: 三个数: N, F, 和 D

* 第2..N+1行: 每一行由两个数开始F_i 和 D_i, 分别是第i 头牛可以吃的食品数和可以喝的饮料数.下F_i个整数是第i头牛可以吃的食品号,再下面的D_i个整数是第i头牛可以喝的饮料号码.

Output

* 第一行: 一个整数,最多可以喂饱的牛数.

Sample Input

4 3 3
2 2 1 2 3 1
2 2 2 3 1 2
2 2 1 3 1 2
2 1 1 3 3

输入解释:

牛 1: 食品从 {1,2}, 饮料从 {1,2} 中选
牛 2: 食品从 {2,3}, 饮料从 {1,2} 中选
牛 3: 食品从 {1,3}, 饮料从 {1,2} 中选
牛 4: 食品从 {1,3}, 饮料从 {3} 中选

Sample Output

3
输出解释:

一个方案是:
Cow 1: 不吃
Cow 2: 食品 #2, 饮料 #2
Cow 3: 食品 #1, 饮料 #1
Cow 4: 食品 #3, 饮料 #3
用鸽笼定理可以推出没有更好的解 (一共只有3总食品和饮料).当然,别的数据会更难.

HINT

 

Source

[Submit][Status][Discuss]

网络流,拆点建图,每一条流都对应着满足一头牛的方案。和今天考试T1贼像……

 #include <cstdio>

 inline int nextChar(void) {
const int siz = ; static char buf[siz];
static char *hd = buf + siz;
static char *tl = buf + siz; if (hd == tl)
fread(hd = buf, , siz, stdin); return *hd++;
} inline int nextInt(void) {
register int ret = ;
register int neg = false;
register int bit = nextChar(); for (; bit < ; bit = nextChar())
if (bit == '-')neg ^= true; for (; bit > ; bit = nextChar())
ret = ret * + bit - ; return neg ? -ret : ret;
} inline int min(int a, int b)
{
return a < b ? a : b;
} const int siz = ;
const int inf = ; int tot;
int s, t;
int hd[siz];
int to[siz];
int fl[siz];
int nt[siz]; inline void add(int u, int v, int f)
{
nt[tot] = hd[u]; to[tot] = v; fl[tot] = f; hd[u] = tot++;
nt[tot] = hd[v]; to[tot] = u; fl[tot] = ; hd[v] = tot++;
} int dep[siz]; inline bool bfs(void)
{
static int que[siz], head, tail; for (int i = s; i <= t; ++i)dep[i] = ; dep[que[head = ] = s] = tail = ; while (head != tail)
{
int u = que[head++], v; for (int i = hd[u]; ~i; i = nt[i])
if (!dep[v = to[i]] && fl[i])
dep[que[tail++] = v] = dep[u] + ;
} return dep[t];
} int cur[siz]; int dfs(int u, int f)
{
if (u == t || !f)
return f; int used = , flow, v; for (int i = cur[u]; ~i; i = nt[i])
if (dep[v = to[i]] == dep[u] + && fl[i])
{
flow = dfs(v, min(fl[i], f - used)); used += flow;
fl[i] -= flow;
fl[i^] += flow; if (used == f)
return f; if (fl[i])
cur[u] = i;
} if (!used)
dep[u] = ; return used;
} inline int maxFlow(void)
{
int maxFlow = , newFlow; while (bfs())
{
for (int i = s; i <= t; ++i)
cur[i] = hd[i]; while (newFlow = dfs(s, inf))
maxFlow += newFlow;
} return maxFlow;
} int N, F, D; inline int cow(int x, int y)
{
return F + D + y * N + x;
} inline int food(int x)
{
return x;
} inline int drink(int x)
{
return x + F;
} signed main(void)
{
N = nextInt();
F = nextInt();
D = nextInt(); s = , t = N* + F + D + ; for (int i = s; i <= t; ++i)
hd[i] = -; for (int i = ; i <= F; ++i)
add(s, food(i), ); for (int i = ; i <= D; ++i)
add(drink(i), t, ); for (int i = ; i <= N; ++i)
{
int f = nextInt();
int d = nextInt(); add(cow(i, ), cow(i, ), ); for (int j = ; j <= f; ++j)
add(food(nextInt()), cow(i, ), ); for (int j = ; j <= d; ++j)
add(cow(i, ), drink(nextInt()), );
} printf("%d\n", maxFlow());
}

@Author: YouSiki

BZOJ 1711: [Usaco2007 Open]Dining吃饭的更多相关文章

  1. bzoj 1711 [Usaco2007 Open]Dining吃饭&&poj 3281 Dining

    最大流. 这东西好像叫三分图匹配. 源点向每个食物点连一条容量为1的边. 每个饮料点向汇点连一条容量为1的边. 将每个牛点拆点,食物点向喜欢它的牛的入点连一条容量为1的边,牛的出点向它喜欢的饮料点连一 ...

  2. BZOJ 1711: [Usaco2007 Open]Dingin吃饭( 最大流 )

    将牛拆成两个点 i 和 i' 并连弧 , S 向每种 food 连边 , 每种 drink 向 T 连边 , 每种 food 向喜欢他的 cow 连边 到 i , 每种 drink 从喜欢它的 cow ...

  3. 【BZOJ】1711: [Usaco2007 Open]Dining吃饭

    [算法]最大流 [题解] S连向食物连向牛连向牛‘连向饮料连向T. 经典的一个元素依赖于两个元素的建图方式. #include<cstdio> #include<algorithm& ...

  4. BZOJ 1711: [Usaco2007 Open]Dingin吃饭

    Description 农夫JOHN为牛们做了很好的食品,但是牛吃饭很挑食. 每一头牛只喜欢吃一些食品和饮料而别的一概不吃.虽然他不一定能把所有牛喂饱,他还是想让尽可能多的牛吃到他们喜欢的食品和饮料. ...

  5. 1711: [Usaco2007 Open]Dingin吃饭

    1711: [Usaco2007 Open]Dingin吃饭 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 560  Solved: 290[Submit ...

  6. BZOJ 1711:[Usaco2007 Open]Dining吃饭(最大流)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1711 [题目大意] 每头牛都有一些喜欢的饮料和食物, 现在有一些食物和饮料,但是每样只 ...

  7. 【最大流】bzoj1711: [Usaco2007 Open]Dining吃饭

    正在网络流入门(原来这种题用网络流做) Description 农夫JOHN为牛们做了很好的食品,但是牛吃饭很挑食. 每一头牛只喜欢吃一些食品和饮料而别的一概不吃.虽然他不一定能把所有牛喂饱,他还是想 ...

  8. Bzoj1711 [Usaco2007 Open]Dining吃饭

    Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 872  Solved: 459 Description 农夫JOHN为牛们做了很好的食品,但是牛吃饭很挑食 ...

  9. BZOJ1711: [Usaco2007 Open]Dingin吃饭

    1711: [Usaco2007 Open]Dingin吃饭 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 508  Solved: 259[Submit ...

随机推荐

  1. No.004:Median of Two Sorted Arrays

    问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...

  2. MySQL数据库不能远程访问的解决办法

    MySQL数据库不允许从远程访问怎么办? 下面提供两种方法: 1.改表法 MySQL的帐号不允许从远程登陆,只能在localhost.这个时候只要在localhost的那台电脑,登入mysql后,更改 ...

  3. 高性能 TCP & UDP 通信框架 HP-Socket v3.5.2

    HP-Socket 是一套通用的高性能 TCP/UDP 通信框架,包含服务端组件.客户端组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C++.C#.Del ...

  4. CString转换为LPSTR和LPSTR转化为CString

    一.CString转换为LPSTR 方法一: CString strFileName LPSTR lpstr - strFileName.GetBuffer(); strFileName.Releas ...

  5. jqGrid插件getCol方法的一个改进

    jgGrid插件是非常常用的一个基于jQuery的表格插件,功能非常强大.我最近也频繁使用.但是这个插件也有一些不够完善的地方.比如这个getCol方法. getCol方法接受三个参数 colname ...

  6. HTTPS和HTTP的区别

    (转自:http://www.php100.com/html/it/biancheng/2015/0209/8582.html) 总的来说,http效率更高,https安全性更高. 首先谈谈什么是HT ...

  7. Java 性能分析工具 , 第 1 部分: 操作系统工具

    引言 性能分析的前提是将应用程序内部的运行状况以及应用运行环境的状况以一种可视化的方式更加直接的展现出来,如何来达到这种可视化的展示呢?我们需要配合使用操作系统中集成的程序监控工具和 Java 中内置 ...

  8. PHP对象在内存堆栈中的分配

    对象在PHP里面和整型.浮点型一样,也是一种数据类,都是存储不同类型数据用的, 在运行的时候都要加载到内存中去用,那么对象在内存里面是怎么体现的呢?内存从逻辑上说大体上是分为4段,栈空间段.堆空间段. ...

  9. Ext JS - 问答

    Ext JS - 问答 在下面你将可以找到关于Ext JS 的最常见问题的答复.如果没有找到您所需的答复,请访问 Ext JS 论坛或者提交一个支持申请. 如果你确信你的问题可以对本页有补充,请让我们 ...

  10. VMware中CPU分配不合理以及License限制引起的SQL Scheduler不能用于查询处理

    有一台SQL Server(SQL Server 2014 标准版)服务器中的scheduler_count与cpu_count不一致,如下截图所示: SELECT  cpu_count ,      ...