http://www.elijahqi.win/2017/11/24/bzoj1711luogu2891-usaco2007-opendining%e5%90%83%e9%a5%ad/
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
Gold
最大流裸题 建边有难度
我源点向每个食物建边 每个食物向牛建边 让牛拆点 然后限制流量为1来限制每种食物和饮料的搭配只有一种
然后跑最大流即可 这样的建边比我原来的想法点数少了很多 讲边数从n^3减为n^2
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define inf 0x3f3f3f3f
#define N 440
using namespace std;
inline char gc(){
static char now[1<<16],*T,*S;
if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0;char ch=gc();
while (ch<'0'||ch>'9') ch=gc();
while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();}
return x;
}
struct node{
int y,z,next;
}data[11000];
int num=1,h[N],level[N];
int t,n,f,d,ans;
inline void insert1(int x,int y,int z){
data[++num].y=y;data[num].z=z;data[num].next=h[x];h[x]=num;
data[++num].y=x;data[num].z=0;data[num].next=h[y];h[y]=num;
}
inline int calc(int x){return f+(x-1)*2+1;}
inline int calc1(int x){return f+n*2+x;}
inline bool bfs(){
queue<int>q;memset(level,0,sizeof(level));q.push(0);level[0]=1;
while (!q.empty()){
int x=q.front();q.pop();
for (int i=h[x];i;i=data[i].next){
int y=data[i].y,z=data[i].z;
if (level[y]||!z) continue;level[y]=level[x]+1;q.push(y);
if (y==t) return 1;
}
}return 0;
}
int dfs(int x,int s){
if (x==t) return s;int ss=s;
for (int i=h[x];i;i=data[i].next){
int y=data[i].y,z=data[i].z;
if (level[x]+1==level[y]&&z){
int xx=dfs(y,min(s,z));if (!xx) level[y]=0;
s-=xx;data[i].z-=xx;data[i^1].z+=xx;if (!s) return ss;
}
}return ss-s;
}
int main(){
freopen("2891.in","r",stdin);
n=read();f=read();d=read();
for (int i=1;i<=f;++i) insert1(0,i,1);
for (int i=1;i<=n;++i){
int ff=read(),dd=read(),tmp=0;int now=calc(i);
for (int i=1;i<=ff;++i) tmp=read(),insert1(tmp,now,1);insert1(now,now+1,1);
for (int j=1;j<=dd;++j) tmp=read(),insert1(now+1,calc1(tmp),1);
}int ans=0;t=n*2+f+d+1;
for (int i=1;i<=d;++i) insert1(calc1(i),t,1);
while (bfs())
ans+=dfs(0,inf);
printf("%d",ans);
return 0;
}