poj3281 最大流

Dining
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 12634 Accepted: 5801

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

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

农夫准备为他的牛准备了F种食物D种饮料,每头牛都有各自喜欢的饮料和食物,而每种食物饮料职能分配给一头牛问 最多有多少都牛得到自己喜欢的食物和饮料

s->食物->牛->牛->饮料->t    最大流量  ,每一个-> 的流量都是1

方法一:ford_fulkerson

#include <iostream>
#include <math.h>
#include <stdio.h>
#include <queue>
#include <vector>
#include <string.h>
const int maxn=1e5;
const int  inf=1e9;
using namespace std;
typedef long long LL;
struct edge
{
    int to,rev;
    int cap;
};
vector<edge> G[maxn];
int level[maxn],iter[maxn];
void add(int from,int to,int cap )
{
    edge e,w;
    e.to=to,e.cap=cap;
    e.rev=G[to].size();
    G[from].push_back(e);
    w.to=from;
    w.cap=0;
    w.rev=G[from].size()-1;
    G[to].push_back(w);

}
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);
            }
        }
    }
}

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(level[v]<level[e.to]&&e.cap>0)
        {
            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;
}
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 F,D,N;
    int f,d;
    int a[1000],b[1000];
    while(scanf("%d%d%d",&N,&F,&D)!=-1)
    {
        for(int i=0;i<=N*2+D+F+1;i++)
            G[i].clear();
        for(int i=1;i<=F;i++)
            add(0,i,1);///源点指向食物
        for(int i=1;i<=D;i++)
          add(i+2*N+F,2*N+F+D+1,1);
        ///饮料指向汇点
        for(int i=1;i<=N;i++)
            add(F+i,F+N+i,1);///牛指向牛
        for(int i=1; i<=N; i++)
        {
            scanf("%d%d",&f,&d);
            for(int j=0; j<f; j++)
            {
                scanf("%d",&a[j]);
                add(a[j],i+F,1);
              // add(a[j]+N,i+N+F,1);
            }
            for(int k=0; k<d; k++)
            {
                scanf("%d",&b[k]);
                add(i+N+F,b[k]+2*N+F,1);
               // add(b[k]+2*N+F,2*N+D+F+1,1);
            }
        }
        cout<<max_flow(0,2*N+F+D+1)<<endl;

        // cout<<min(max_flow(0,N+F+1),max_flow2(0,N+D+1))<<endl;
    }
    return 0;
}
方法二:dinic

#include <iostream>
#include <math.h>
#include <stdio.h>
#include <vector>
#include <string.h>
const int maxn=1e5;
const int inf=0x3f3f3f3f;
using namespace std;
int V,V2;
vector<int>G[maxn];
vector<int>G2[maxn];
bool used[maxn],used2[maxn];
int match[maxn],match2[maxn];
void add_edge(int u,int v)
{
    G[u].push_back(v);
    G[v].push_back(u);
}
void add_edge2(int u,int v)
{
    G2[u].push_back(v);
    G2[v].push_back(u);
}
bool dfs(int v)
{
    used[v]=true;
    for(int i=0;i<G[v].size();i++)
    {
        int u=G[v][i],w=match[u];
        if(w<0||!used[w]&&dfs(w))
        {
            match[v]=u;
            match[u]=v;
            return true;
        }
    }
    return false;
}
bool dfs2(int v)
{
    used2[v]=true;
    for(int i=0;i<G2[v].size();i++)
    {
        int u=G2[v][i],w=match2[u];
        if(w<0||!used2[w]&&dfs2(w))
        {
            match2[v]=u;
            match2[u]=v;
            return true;
        }
    }
    return false;
}
int bip()
{
    int res=0;
    memset(match,-1,sizeof(match));
    for(int v=0;v<V;v++)
    {
        if(match[v]<0)
        {
            memset(used,0,sizeof(used));
            if(dfs(v))
                res++;
        }
    }
    return res;
}
int bip2()
{
    int res=0;
    memset(match2,-1,sizeof(match2));
    for(int v=0;v<V2;v++)
    {
        if(match2[v]<0)
        {
            memset(used2,0,sizeof(used2));
            if(dfs2(v))
                res++;
        }
    }
    return res;
}
int main()
{
     int F,D,N;
    int f,d;
    int a[1000],b[1000];
        while(scanf("%d%d%d",&N,&F,&D)!=-1)
    {
        V=N+F;
        V2=N+D;
        for(int i=1;i<=N;i++)
        {
            //add_edge(0,i);
            //add_edge2(0,i);
            scanf("%d%d",&f,&d);
            for(int j=0;j<f;j++)
            {
                scanf("%d",&a[j]);
                add_edge(i,a[j]+N);
                add_edge(a[j]+N,N+F+1);
            }
            for(int k=0;k<d;k++)
            {
                scanf("%d",&b[k]);
                add_edge2(i,b[k]+N);
                add_edge2(b[k]+N,N+D+1);
            }
        }
        printf("%d\n",min(bip(),bip2()));
        //cout<<bip()<<endl;
        //cout<<bip2()<<endl;
       // cout<<min(max_flow(0,N+F+1),max_flow2(0,N+D+1))<<endl;
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值