Dining

Description

Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others.

Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their preferences. Although he might not be able to stuff everybody, he wants to give a complete meal of both food and drink to as many cows as possible.

Farmer John has cooked F (1 ≤ F ≤ 100) types of foods and prepared D (1 ≤ D ≤ 100) types of drinks. Each of his N (1 ≤ N ≤ 100) cows has decided whether she is willing to eat a particular food or drink
a particular drink. Farmer John must assign a food type and a drink type to each cow to maximize the number of cows who get both.

Each dish or drink can only be consumed by one cow (i.e., once food type 2 is assigned to a cow, no other cow can be assigned food type 2).

Input

Line 1: Three space-separated integers: NF, and D 

Lines 2..N+1: Each line i starts with a two integers Fi and Di, the number of dishes that cow i likes and the number of drinks that cow i likes. The next Fi integers
denote the dishes that cow i will eat, and the Di integers following that denote the drinks that cow i will drink.

Output

Line 1: A single integer that is the maximum number of cows that can be fed both food and drink that conform to their wishes

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

Sample Output

3

Hint

One way to satisfy three cows is: 

Cow 1: no meal 

Cow 2: Food #2, Drink #2 

Cow 3: Food #1, Drink #1 

Cow 4: Food #3, Drink #3 

The pigeon-hole principle tells us we can do no better since there are only three kinds of food or drink. Other test data sets are more challenging, of course.
——————————————————————————————————————
题目的意思是给牛喂食物和饮料,每头牛有各自的喜爱的食物和饮料,只有当它同时拿到喜爱的食物和饮料他会满足,问最多多少头牛满足。
思路:网络最大流。 我们可以把食物和牛连边流量为1,饮料和牛连边流量为1,为了防止每头牛得到多个食物和饮料,我们可以把一头牛拆成2个点,再加上一个源点和一个汇点,源点和所有食物连边流量为1,汇点和所有饮料连边流量为1,这样计算源点到汇点的最大流就可以了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset> using namespace std; #define LL long long
const int INF = 0x3f3f3f3f;
#define MAXN 500 struct node
{
int u, v, next, cap;
} edge[MAXN*MAXN];
int nt[MAXN], s[MAXN], d[MAXN], visit[MAXN];
int cnt; void init()
{
cnt = 0;
memset(s, -1, sizeof(s));
} void add(int u, int v, int c)
{
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].cap = c;
edge[cnt].next = s[u];
s[u] = cnt++;
edge[cnt].u = v;
edge[cnt].v = u;
edge[cnt].cap = 0;
edge[cnt].next = s[v];
s[v] = cnt++;
} bool BFS(int ss, int ee)
{
memset(d, 0, sizeof d);
d[ss] = 1;
queue<int>q;
q.push(ss);
while (!q.empty())
{
int pre = q.front();
q.pop();
for (int i = s[pre]; ~i; i = edge[i].next)
{
int v = edge[i].v;
if (edge[i].cap > 0 && !d[v])
{
d[v] = d[pre] + 1;
q.push(v);
}
}
}
return d[ee];
} int DFS(int x, int exp, int ee)
{
if (x == ee||!exp) return exp;
int temp,flow=0;
for (int i = nt[x]; ~i ; i = edge[i].next, nt[x] = i)
{
int v = edge[i].v;
if (d[v] == d[x] + 1&&(temp = (DFS(v, min(exp, edge[i].cap), ee))) > 0)
{
edge[i].cap -= temp;
edge[i ^ 1].cap += temp;
flow += temp;
exp -= temp;
if (!exp) break;
}
}
if (!flow) d[x] = 0;
return flow;
} int Dinic_flow(int ss, int ee)
{
int ans = 0;
while (BFS(ss, ee))
{
for (int i = 0; i <= ee; i++) nt[i] = s[i];
ans+= DFS(ss, INF, ee);
}
return ans;
} int main()
{
int n,f,d,a,b,x;//f:1~f,牛:f+1~f+n&&f+n+1~f+2*n,d:f+2*n+1~f+2*n+d
while(~scanf("%d%d%d",&n,&f,&d))
{
init();
for(int i=0; i<n; i++)
{
scanf("%d%d",&a,&b);
for(int j=0; j<a; j++)
{
scanf("%d",&x);
add(x,f+1+i,1);
}
for(int j=0; j<b; j++)
{
scanf("%d",&x);
add(f+n+1+i,f+2*n+x,1);
}
}
for(int i=0; i<f; i++)
{
add(0,i+1,1);
}
for(int i=0; i<d; i++)
{
add(f+2*n+i+1,f+d+2*n+1,1);
}
for(int i=0; i<n; i++)
{
add(f+i+1,f+i+1+n,1);
}
printf("%d\n",Dinic_flow(0,f+d+2*n+1));
}
return 0;
}

POJ3281 Dining 2017-02-11 23:02 44人阅读 评论(0) 收藏的更多相关文章

  1. HDU 2040 亲和数 [补] 分类: ACM 2015-06-25 23:10 10人阅读 评论(0) 收藏

    今天和昨天都没有做题,昨天是因为复习太累后面忘了,今天也是上午考毛概,下午又忙着复习计算机图形学,晚上也是忘了结果打了暗黑3,把暗黑3 打通关了,以后都不会玩太多游戏了,争取明天做3题把题目补上,拖越 ...

  2. ZOJ2482 IP Address 2017-04-18 23:11 44人阅读 评论(0) 收藏

    IP Address Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose you are reading byte streams fr ...

  3. hilbert矩阵 分类: 数学 2015-07-31 23:03 2人阅读 评论(0) 收藏

    希尔伯特矩阵 希尔伯特矩阵是一种数学变换矩阵 Hilbert matrix,矩阵的一种,其元素A(i,j)=1/(i+j-1),i,j分别为其行标和列标. 即: [1,1/2,1/3,--,1/n] ...

  4. pascal矩阵 分类: 数学 2015-07-31 23:01 3人阅读 评论(0) 收藏

    帕斯卡矩阵 1.定义       帕斯卡矩阵:由杨辉三角形表组成的矩阵称为帕斯卡(Pascal)矩阵. 杨辉三角形表是二次项 (x+y)^n 展开后的系数随自然数 n 的增大组成的一个三角形表. 如4 ...

  5. NYOJ-235 zb的生日 AC 分类: NYOJ 2013-12-30 23:10 183人阅读 评论(0) 收藏

    DFS算法: #include<stdio.h> #include<math.h> void find(int k,int w); int num[23]={0}; int m ...

  6. HDU2033 人见人爱A+B 分类: ACM 2015-06-21 23:05 13人阅读 评论(0) 收藏

    人见人爱A+B Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  7. Number of Containers(数学) 分类: 数学 2015-07-07 23:42 1人阅读 评论(0) 收藏

    Number of Containers Time Limit: 1 Second Memory Limit: 32768 KB For two integers m and k, k is said ...

  8. ZOJ2405 Specialized Four-Digit Numbers 2017-04-18 20:43 44人阅读 评论(0) 收藏

    Specialized Four-Digit Numbers Time Limit: 2 Seconds      Memory Limit: 65536 KB Find and list all f ...

  9. PAT甲 1048. Find Coins (25) 2016-09-09 23:15 29人阅读 评论(0) 收藏

    1048. Find Coins (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva loves t ...

随机推荐

  1. 使用正则表达式去除html标签

    不知道大家遇到这话总情况没有,从数据库读取数据,数据参杂着html标记<p>等,在显式的时候控制字符个数,这个时候就会出现页面样式串行,使用正则表达式去除html标记就不会有还这个问题. ...

  2. Quick guide for converting from JAGS or BUGS to NIMBLE

    Converting to NIMBLE from JAGS, OpenBUGS or WinBUGS NIMBLE is a hierarchical modeling package that u ...

  3. Mac上如何把图片中的文字转换成word/pdf文字

    如何把图片文字转换成word文字? - 知乎 https://www.zhihu.com/question/25488536 在 OneNote for Mac 中插入的圖片複製文字 - OneNot ...

  4. 编译安装php-5.4.44

    编译安装php-5.4.44 1. 首先,安装必要的库文件,一面编译被打断: yum install -y gcc gcc-c++  make zlib zlib-devel pcre pcre-de ...

  5. mysql 使用inet_aton和inet_ntoa处理ip地址数据

    mysql 使用inet_aton和inet_ntoa处理ip地址数据 mysql提供了两个方法来处理ip地址 inet_aton 把ip转为无符号整型(4-8位) inet_ntoa 把整型的ip转 ...

  6. HDU 1043 八数码问题的多种解法

    一.思路很简单,搜索.对于每一种状态,利用康托展开编码成一个整数.于是,状态就可以记忆了. 二.在搜索之前,可以先做个优化,对于逆序数为奇数的序列,一定无解. 三.搜索方法有很多. 1.最普通的:深搜 ...

  7. zookeeper无故挂掉,启动不起来的问题

    一台测试的服务器重启后,运行zookeeperd的启动脚本显示启动成功: [plain] view plain copy     JMX enabled by default Using config ...

  8. java 序列化时排除指定属性

    java 序列化对象如何排除指定属性呢? java 中序列化对象有多种方式:struts2 ,jackson,json-lib (1)使用struts2 json插件 依赖的jar包:struts2- ...

  9. Linux中shell变量$0,$?等含义

    linux中shell变量$#,$@,$0,$1,$2的基本含义: 变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行 ...

  10. 15.3Sum (Two-Pointers)

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...