POJ3281 Dining 网络流建图

时间:2021-06-04 04:30:01

http://poj.org/problem?id=3281

Dining
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14564   Accepted: 6607

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

题意:有n头牛f中食品d中饮料,每种牛都有自己喜欢的食品和饮料,问最多有多少头牛能够吃到自己喜欢的食品和饮料。

思路:这是一道最大流问题,难点在于如何建图。初学网络流借鉴了别人的思路。


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<string.h>
using namespace std;
const int maxm=500;
const int inf=10000000;
struct node
{
int to,cap,rev;
};
vector<node>v[maxm];
bool used[maxm];
int dfs(int s,int t,int f);
int max_flow(int s,int t);
int main()
{
int n,i,j,k,food,drink,ff,dd,x,y;
while(scanf("%d%d%d",&n,&food,&drink)!=EOF)
{
memset(v,0,sizeof(v));
for(i=1;i<=n;i++)
{
scanf("%d%d",&ff,&dd);
while(ff--)
{
scanf("%d",&x);
x=2*n+x;
v[x].push_back((node){i,1,v[i].size()});
v[i].push_back((node){x,0,v[x].size()-1});
}
while(dd--)
{
scanf("%d",&x);
x=2*n+food+x;
v[i+n].push_back((node){x,1,v[x].size()});
v[x].push_back((node){i+n,0,v[i+n].size()-1});
}
}
for(i=1;i<=n;i++)
{
v[i].push_back((node){i+n,1,v[i+n].size()});
v[i+n].push_back((node){i,0,v[i].size()-1});
}
for(i=2*n+1;i<=2*n+food;i++)
{
v[0].push_back((node){i,1,v[i].size()});
v[i].push_back((node){0,0,v[0].size()-1});
}
int e=2*n+food+drink+1;
for(i=2*n+food+1;i<=2*n+food+drink;i++)
{
v[i].push_back((node){e,1,v[e].size()});
v[e].push_back((node){i,0,v[i].size()-1});
}
printf("%d\n",max_flow(0,e));
}
return 0;
}
int max_flow(int s,int t)
{
int i,j,ans,d;
ans=0;
while(1)
{
memset(used,false,sizeof(used));
d=dfs(s,t,inf);
if(d==0)
return ans;
ans+=d;
}
}
int dfs(int s,int t,int f)
{
int i,j;
if(s==t)
return f;
used[s]=true;
for(i=0;i<v[s].size();i++)
{
node &temp=v[s][i];
if(!used[temp.to] && temp.cap>0)
{
int d=dfs(temp.to,t,min(temp.cap,f));
if(d>0)
{
temp.cap-=d;
v[temp.to][temp.rev].cap+=d;
return d;
}
}
}
return 0;
}