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:
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. Source |
[Submit] [Go Back] [Status] [Discuss]
link:http://poj.org/problem?id=3281
建图:
首先知道要把牛分点,分为牛1,牛2;如果不分则建图为:食物->牛->饮料;
若牛选了食物A,饮料B,若食物C,饮料D也跟牛有边,则被算法认为是增广路,但这并不满足每牛只能吃一种饮料和食物;
所以要变为: 源点S->食物->牛1->牛2->饮料->汇点T,所有容量为1; copy
AC code:
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#define INF 1e9
using namespace std;
const int MAX_V=1000;
struct edge{
int to,cap,rev; //终点,容量,反向边
};
vector<edge> G[MAX_V]; //图的邻接表表示
int level[MAX_V]; //顶点到原点的距离标号
int iter[MAX_V]; //当前弧,在其之前的边已经没有用了
//增加一条从from到to的容量为cap的边
void add_edge(int from,int to,int cap){
G[from].push_back((edge){to,cap,G[to].size()});
G[to].push_back((edge){from,0,G[from].size()-1});
}
//通过BFS计算从原点出发的距离标号
void bfs(int s){
memset(level,-1,sizeof(level));
queue<int > que;
level[s]=0;
que.push(s);
while(!que.empty()){
int v=que.front(); que.pop();
for(int i=0;i<G[v].size();i++){
edge& e=G[v][i];
if(e.cap>0&&level[e.to]<0){
level[e.to]=level[v]+1;
que.push(e.to);
}
}
}
}
//通过DFS寻找增广路
int dfs(int v,int t,int f){
if(v==t) return f;
for(int& i=iter[v]; i<G[v].size(); i++){
edge& e=G[v][i];
if(e.cap>0 && level[v]<level[e.to]){
int d=dfs(e.to,t,min(f,e.cap));
if(d>0){
e.cap -= d;
G[e.to][e.rev].cap+=d;
return d;
}
}
}
return 0;
}
//求解从s到t的最大流
int max_flow(int s,int t){
int flow=0;
for(;;){
bfs(s);
if(level[t]<0) return flow;
memset(iter,0,sizeof(iter));
int f;
while((f=dfs(s,t,INF))>0){
flow+=f;
}
}
}
int main(){
int n,f,d;
while(scanf("%d%d%d",&n,&f,&d)!=EOF){
int src=600;
int end1=601;
for(int i=1;i<=f;i++) add_edge(src,i+200,1); //i+200==food
for(int i=1;i<=d;i++) add_edge(i+300,end1,1); //i+300==water
for(int i=1;i<=n;i++){
add_edge(i,i+100,1); //i+100==cow2 i==cow1
int fi,di;
scanf("%d%d",&fi,&di);
for(int j=1;j<=fi;j++){
int f1;
scanf("%d",&f1);
add_edge(f1+200,i,1); //food----cow1
}
for(int j=1;j<=di;j++){
int d1;
scanf("%d",&d1);
add_edge(i+100,d1+300,1);
}
}
int res=max_flow(src,end1);
printf("%d\n",res);
}
return 0;
}