Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 21265 | Accepted: 9401 |
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
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
Sample Output
3
Hint
Source
USACO 2007 Open Gold
网络流建图:源点——>食物——>牛1——>牛2——>饮料——>汇点
为了不让一头牛吃好多次,每头牛都拆成牛1和牛2两个点,连一条流量为1的边。
#include
#include
#include
#include
#include
using namespace std;
const int INF=1<<29;
int src=0,des=500,e,head[5005],dis[5005];
struct Edge{
int v,cap,next;
Edge(){}
Edge(int vv,int ca,int ne):v(vv),cap(ca),next(ne){}
}edge[5005];
void addedge(int u,int v,int cap){
edge[e]=Edge(v,cap,head[u]);
head[u]=e++;
edge[e]=Edge(u,0,head[v]);
head[v]=e++;
}
void init(){
e=0;
memset(head,-1,sizeof(head));
}
int bfs(){
memset(dis,0,sizeof(dis));
dis[src]=1;
queue q;
q.push(src);
while (q.size()){
int x=q.front();q.pop();
for (int i=head[x];i!=-1;i=edge[i].next){
int v=edge[i].v;
if (edge[i].cap && dis[v]==0){
dis[v]=dis[x]+1;
if (v==des) return 1;
q.push(v);
}
}
}
return 0;
}
int dfs(int s,int f){
if (s==des) return f;
int tmp,cost=0;
for (int i=head[s];i!=-1;i=edge[i].next){
int v=edge[i].v;
if (edge[i].cap && dis[s]==dis[v]-1){
tmp=dfs(v,min(f-cost,edge[i].cap));
if (tmp>0){
edge[i].cap-=tmp;
edge[i^1].cap+=tmp;
cost+=tmp;
if (f==cost) break;
}
else dis[v]=-1;
}
}
return cost;
}
int Dinic(){
int ans=0;
while (bfs()) ans+=dfs(src,INF);
return ans;
}
int main(){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
init();
for (int i=1;i<=a;i++){
int x,y;
scanf("%d%d",&x,&y);
for (int j=0;j