Dining
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 11828   Accepted: 5437

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.
 
题意:农夫为他的n头牛准备了f分的食物和d份的饮料,但是每头牛都很挑剔,他们只吃自己喜欢的食物,现在要让你给牛分配食物和饮料,(一头牛吃过的食物不可以分给其他牛)问最多能给多少头牛分配他们喜欢的食物和饮料
 
输入:第一行输入n,f,d,表示牛的个数,食物和饮料的数量,   接下来n行,每行前两个数fi,di,分别代表当前行(即第i行为第i头牛)这头牛喜欢吃的食物的类型的数量和喜欢喝的饮料的类型的数量,再接下来fi个数代表第i头牛喜欢吃的食物的编号,接下来di个数代表牛喜欢喝的饮料的编号
 
题解:将牛分为左牛和右牛,设置超级源点和超级汇点   建图的思路:超级源点->食物->左牛->右牛->饮料->超级汇点   每条边的容量为1        不能直接食物指向牛,牛再指向饮料是因为这样的话可能一头牛就不止分配一种食物和饮料了  画个图来解释
1、若直接食物->牛->饮料
          食物             牛                   饮料

如图,例如当1到4这条路走过后 4连上5,当2经过4后4又可以连上6,这就造成了一头牛吃了多种食物和饮料

2、食物->左牛->右牛->饮料

这种情况当左牛选中一个食物之后经过右牛,左右牛之间的路径已经满流不能继续走,就避免了一头牛吃多种食物和饮料的情况

AC代码:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
#include<algorithm>
#define MAX 1000
#define MAXM 200100
#define INF 0x7fffff
using namespace std;
int n,f,d;
int ans,head[MAX];
int cur[MAX];
int dis[MAX];
int vis[MAX];
int sect;
struct node
{
int from,to,cap,flow,next;
}edge[MAXM];
void init()
{
ans=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
edge[ans]={u,v,w,0,head[u]};
head[u]=ans++;
edge[ans]={v,u,0,0,head[v]};
head[v]=ans++;
}
void getmap()
{
int i,j,a,b,fi,di;
for(i=1;i<=n;i++)
{
scanf("%d%d",&fi,&di);
while(fi--)
{
scanf("%d",&a);
///xxx//add(0,a,1);//源点连食物
add(a,f+i,1);//食物连左牛
}
add(f+i,n+i+f,1);//左牛连右牛
while(di--)
{
scanf("%d",&b);
add(n+i+f,2*n+f+b,1);//右牛连饮料
}
}
int db=2*n+f+1,de=2*n+f+d;
for(i=1;i<=f;i++)
add(0,i,1);//源点连食物
for(i=db;i<=de;i++)
add(i,de+1,1);//饮料连超级汇点
sect=2*n+f+d+1;
}
int bfs(int beg,int end)
{
int i;
memset(vis,0,sizeof(vis));
memset(dis,-1,sizeof(dis));
queue<int>q;
while(!q.empty())
q.pop();
vis[beg]=1;
dis[beg]=0;
q.push(beg);
while(!q.empty())
{
int u=q.front();
q.pop();
for(i=head[u];i!=-1;i=edge[i].next)//遍历所有的与u相连的边
{
node E=edge[i];
if(!vis[E.to]&&E.cap>E.flow)//如果边未被访问且流量未满继续操作
{
dis[E.to]=dis[u]+1;//建立层次图
vis[E.to]=1;//将当前点标记
if(E.to==end)//如果当前点搜索到终点则停止搜索 返回1表示有从原点到达汇点的路径
return 1;
q.push(E.to);//将当前点入队
}
}
}
return 0;//返回0表示未找到从源点到汇点的路径
}
int dfs(int x,int a,int end)//把找到的这条边上的所有当前流量加上a(a是这条路径中的最小残余流量)
{
//int i;
if(x==end||a==0)//如果搜索到终点或者最小的残余流量为0
return a;
int flow=0,f;
for(int& i=cur[x];i!=-1;i=edge[i].next)//i从上次结束时的弧开始
{
node& E=edge[i];
if(dis[E.to]==dis[x]+1&&(f=dfs(E.to,min(a,E.cap-E.flow),end))>0)//如果
{//bfs中我们已经建立过层次图,现在如果 dis[E.to]==dis[x]+1表示是我们找到的路径
//如果dfs>0表明最小的残余流量还有,我们要一直找到最小残余流量为0
E.flow+=f;//正向边当前流量加上最小的残余流量
edge[i^1].flow-=f;//反向边
flow+=f;//总流量加上f
a-=f;//最小可增流量减去f
if(a==0)
break;
}
}
return flow;//所有边加上最小残余流量后的值
}
int Maxflow(int beg,int end)
{
int flow=0;
while(bfs(beg,end))//存在最短路径
{
memcpy(cur,head,sizeof(head));//复制数组
flow+=dfs(beg,INF,end);
}
return flow;//最大流量
}
int main()
{
int t;
while(scanf("%d%d%d",&n,&f,&d)!=EOF)
{
init();
getmap();
printf("%d\n",Maxflow(0,sect));
}
return 0;
}

  

 

poj 3281 Dining【拆点网络流】的更多相关文章

  1. POJ 3281 Dining (拆点)【最大流】

    <题目链接> 题目大意: 有N头牛,F种食物,D种饮料,每一头牛都有自己喜欢的食物和饮料,且每一种食物和饮料都只有一份,让你分配这些食物和饮料,问最多能使多少头牛同时获得自己喜欢的食物和饮 ...

  2. poj 3281 Dining 拆点 最大流

    题目链接 题意 有\(N\)头牛,\(F\)个食物和\(D\)个饮料.每头牛都有自己偏好的食物和饮料列表. 问该如何分配食物和饮料,使得尽量多的牛能够既获得自己喜欢的食物又获得自己喜欢的饮料. 建图 ...

  3. POJ 3281 Dining (网络流)

    POJ 3281 Dining (网络流) Description Cows are such finicky eaters. Each cow has a preference for certai ...

  4. POJ 3281 Dining(最大流)

    POJ 3281 Dining id=3281" target="_blank" style="">题目链接 题意:n个牛.每一个牛有一些喜欢的 ...

  5. poj 3281 Dining(网络流+拆点)

    Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20052   Accepted: 8915 Descripti ...

  6. poj 3281 Dining 网络流-最大流-建图的题

    题意很简单:JOHN是一个农场主养了一些奶牛,神奇的是这些个奶牛有不同的品味,只喜欢吃某些食物,喝某些饮料,傻傻的John做了很多食物和饮料,但她不知道可以最多喂饱多少牛,(喂饱当然是有吃有喝才会饱) ...

  7. POJ 3281 Dining(网络流拆点)

    [题目链接] http://poj.org/problem?id=3281 [题目大意] 给出一些食物,一些饮料,每头牛只喜欢一些种类的食物和饮料, 但是每头牛最多只能得到一种饮料和食物,问可以最多满 ...

  8. POJ - 3281 Dining(拆点+最大网络流)

    Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18230   Accepted: 8132 Descripti ...

  9. 图论--网络流--最大流--POJ 3281 Dining (超级源汇+限流建图+拆点建图)

    Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, an ...

随机推荐

  1. tomcat+apache 实现负载均衡之一:同一台电脑部署2个以上tomcat

    1.  下载tomcat 8.0.17 http://apache.fayea.com/tomcat/tomcat-8/v8.0.17/bin/apache-tomcat-8.0.17.tar.gz ...

  2. css之自动换行

    自动换行问题,正常字符的换行是比较合理的,而连续的数字和英文字符常常将容器撑大, 挺让人头疼,下面介绍的是CSS如何实现换行的方法 对于div,p等块级元素 正常文字的换行(亚洲文字和非亚洲文字)元素 ...

  3. zoj 3716

    题目给我们四个点,要求我们以这四个点为圆心,形成四个相切的圆: 求他们的半径和: 首先我们从他们中间选出三个点,以这三个点为圆心的三个圆最大可以两两互相相切: 证明:假设这三个圆的半径分别为a,b,c ...

  4. 标准C++中的string类的用法总结(转)

    http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的 ...

  5. 【形式化方法:VDM++系列】1.前言

    1.前言 今天开始上课学习软件需求分析与VDM++,经过一节课的学习,我又增长了见识. 软件需求工程在软件工程中处于十分核心的地位:需求分析的好坏直接决定软件工程的成败.这一点是我之前对需求工程的理解 ...

  6. php重定向 htaccess文件的编写

    主页的url重写规则:/controller/action.html(其中第一个英文代表控制器,第二个英文代表动作,映射到:index.php?c=controller&a=action) 后 ...

  7. Windows 内核(WRK)编译

    引子 WRK 是微软于 2006 年针对教育和学术界开放的 Windows 内核的部分源码, WRK(Windows Research Kernel)也就是 Windows 研究内核, 在 WRK 中 ...

  8. 使用智遥工作流,优化SAP请购流程

    传统请购流程,都是用户在SAP系统中填写请购单,然后再打印出来,递交给上级领导审批.领导审批完了,再到SAP系统中更新release标识.若中途请购单内容需要变更,则需要重新打印,审批. 智遥工作流, ...

  9. mingw32 下编译 zlib

    cp win32/makefile.gcc makefile.gcc make -f makefile.gcc make install -f Makefile.gcc INCLUDE_PATH=/m ...

  10. Formatting is Specified but argument is not IFormattable

    private void DeviceSetText(TextBox textBox, string text) { //处理text的显示值 ") //小数位后保留2位 { //小数点后保 ...