# POJ 3281-Dining(网络流_最大流_ISAP算法+拆点)

Dining
 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9938 Accepted: 4567

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 theDi 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

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
using namespace std;
const int inf=0x3f3f3f3f;
int cnt,n,s,t,nv;
int maxint=inf;
struct node
{
int u,v,cap;
int next;
}edge;
{
edge[cnt].v=v;
edge[cnt].cap=cap;

edge[cnt].v=u;
edge[cnt].cap=0;
}
void bfs()
{
int i,j;
memset(num,0,sizeof(num));
memset(d,-1,sizeof(d));
int f1=0,f2=0;
q[f1++]=t;
num=1;
d[t]=0;
while(f1>=f2){
int u=q[f2++];
int v=edge[i].v;
if(d[v]!=-1) continue;
d[v]=d[u]+1;
num[d[v]]++;
q[f1++]=v;
}
}
}
void isap()
{
bfs();
int flow=0,u=pre[s]=s,i;
while(d[s]<nv){
if(u==t){
int f=maxint,pos;
for(i=s;i!=t;i=edge[cur[i]].v){
if(f>edge[cur[i]].cap){
f=edge[cur[i]].cap;
pos=i;
}
}
for(i=s;i!=t;i=edge[cur[i]].v){
edge[cur[i]].cap-=f;
edge[cur[i]^1].cap+=f;
}
flow+=f;
u=pos;
}
for(i=cur[u];i!=-1;i=edge[i].next){
if(d[edge[i].v]+1==d[u]&&edge[i].cap)
break;
}
if(i!=-1){
cur[u]=i;
pre[edge[i].v]=u;
u=edge[i].v;
}
else{
if(--num[d[u]]==0) break;
int mind=nv;
if(mind>d[edge[i].v]&&edge[i].cap){
cur[u]=i;
mind=d[edge[i].v];
}
}
d[u]=mind+1;
num[d[u]]++;
u=pre[u];
}

}
printf("%d\n",flow);
}
int main()
{
int i,j;
int m,f,d,a,b,c;
scanf("%d %d %d",&m,&f,&d);
cnt=0;
s=0;
t=2*m+f+d+2+1;
nv=t+1;
for(i=1;i<=f;i++){
}
for(i=1;i<=d;i++){
}
for(i=1;i<=m;i++){
}
//点的顺序：食物，奶牛，饮料
for(i=1;i<=m;i++){
scanf("%d %d",&a,&b);
while(a--){
scanf("%d",&c);
}
while(b--)
{
scanf("%d",&c);
}
}
isap();
return 0;
} ©️2019 CSDN 皮肤主题: 酷酷鲨 设计师: CSDN官方博客 