B - Dining

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Appoint description:
 

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: N, F, 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.
 

每组样例有3个数据,代表牛的数量,实物的数量,饮料的数量,每头牛都需要吃特定的食物和饮料,且只能吃一份,每种食物或者饮料被一头牛吃掉后不能再被其他的牛使用,问最多可以满足多少头牛

 
 
 
思路,对每种牛与其固定的·1食物和饮料建边,容量为1,,,,因为每头牛只能食用一种饮料或者食物,所有将牛进行拆点,,中间边容量为1,建立一个超级源点和超级会点,,牛放中间,食物和饮料建在两边就ok了
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
int edge[][];//邻接矩阵
int dis[];//距源点距离,分层图
int start,end;
int m,n;//N:点数;M,边数
int bfs(){
memset(dis,-,sizeof(dis));//以-1填充
dis[]=;
queue<int>q;
q.push(start);
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=;i<=n;i++){
if(dis[i]<&&edge[u][i]){
dis[i]=dis[u]+;
q.push(i); }
}
}
if(dis[n]>)
return ;
else
return ;//汇点的DIS小于零,表明BFS不到汇点
}
//Find代表一次增广,函数返回本次增广的流量,返回0表示无法增广
int find(int x,int low){//Low是源点到现在最窄的(剩余流量最小)的边的剩余流量
int a=;
if(x==n)
return low;//是汇点
for(int i=;i<=n;i++){
if(edge[x][i]>&&dis[i]==dis[x]+&&//联通,,是分层图的下一层
(a=find(i,min(low,edge[x][i])))){//能到汇点(a <> 0)
edge[x][i]-=a;
edge[i][x]+=a;
return a;
} }
return ;
}
int main(){
int a,b,c;
while(scanf("%d%d%d",&a,&b,&c)!=EOF){ n=a+a+b+c+;
memset(edge,,sizeof(edge));
for(int i=;i<=b;i++)
edge[][i]=;
for(int i=a+a+b+;i<=a+a+b+c;i++)
edge[i][n]=;
int u;
int sum1,sum2;
for(int i=;i<=a;i++){
// int u,v,w; scanf("%d%d",&sum1,&sum2);
for(int j=;j<=sum1;j++){
scanf("%d",&u);
edge[u][i+b]=;
}
for(int j=;j<=sum2;j++){
scanf("%d",&u);
edge[b+a+i][a+a+b+u]=;
} }
for(int i=;i<=a;i++){
edge[i+b][i+b+a]=; }
start=;
end=n;
int ans=;
while(bfs()){//要不停地建立分层图,如果BFS不到汇点才结束
ans+=find(,0x7fffffff);//一次BFS要不停地找增广路,直到找不到为止
}
printf("%d\n",ans);
}
return ;
}

最新文章

  1. ORA-01102 报错解决方法
  2. mysql重点--正确使用
  3. java并发库 Lock 公平锁和非公平锁
  4. Quartz与Spring整合进行热部署的实现(二)
  5. Mac废纸篓 不能完全清空的有效解决方法
  6. c++ 创建 socket server
  7. 如何实现zs无限期试用
  8. Unity3d之Socket UDP协议
  9. Codeforces 439D Devu and his Brother 三分
  10. 消息队列msmq
  11. TCP应用编程--套接字C#实现
  12. Ansible2:主机清单【转】
  13. PHP字符串函数试题
  14. bzoj2111 Perm 排列计数
  15. vue----分级上传
  16. 7、...arg ...[1,2,3] 数组扩展
  17. linux系统 之 git
  18. Java 7 使用TWR(Try-with-resources)完成文件copy
  19. [转]看懂Oracle执行计划
  20. dbt 生产环境使用

热门文章

  1. JavaWeb学习笔记——JSTL核心标签库
  2. DecimalFormat类
  3. Ubuntu PostgreSQL安装和配置
  4. C#----XML操作小结
  5. 64bit upload app store
  6. 如何让Chrome浏览器可以加载本地XML文件?
  7. MyEclipse for linux 破解方法
  8. python 日志收集系统
  9. 【struts2】Result和ResultType
  10. 在linux命令行中直接执行php命令