码迷,mamicode.com
首页 > 其他好文 > 详细

poj 3281 Dining

时间:2017-05-20 11:23:17      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:lan   lines   cer   ace   time   using   eal   ++   mem   

Dining
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 17734   Accepted: 7908

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

 

题意:n头牛,f种食物,d种饮料,

每头牛有喜欢的食物、饮料,

每头牛只能分配一种食物和一种饮料

问最后最多能满足多少头牛

 

题解:把牛拆点,放在中间,左边食物,右边饮料,最大流

#include<cstdio>
#include<algorithm>
#include<queue>
#define N 501
#define M N*N
using namespace std;
int src,decc,ans;
int from[N],to[M],nxt[M],tot=1;
int cap[M];
int cur[N],lev[N];
queue<int>q;
void add(int u,int v,int f)
{
    to[++tot]=v; nxt[tot]=from[u]; from[u]=tot; cap[tot]=f;
    to[++tot]=u; nxt[tot]=from[v]; from[v]=tot; cap[tot]=0;
}
bool bfs()
{
    while(!q.empty()) q.pop();
     for(int i=src;i<=decc;i++) cur[i]=from[i],lev[i]=-1;
    q.push(src); lev[src]=0;
    int now;
    while(!q.empty())
    {
        now=q.front();  q.pop();
        for(int i=from[now];i;i=nxt[i])
        {
            if(lev[to[i]]==-1&&cap[i]>0)
            {
                lev[to[i]]=lev[now]+1;
                if(to[i]==decc) return true;
                q.push(to[i]);
            }
        }
    }
    return false;
}
int dinic(int now,int flow)
{
    if(now==decc) return flow;
    int rest=0,delta;
    for(int &i=cur[now];i;i=nxt[i])
    {
        if(lev[to[i]]>lev[now]&&cap[i]>0)
        {
            delta=dinic(to[i],min(cap[i],flow-rest));
            if(delta)
            {
                cap[i]-=delta; cap[i^1]+=delta;
                rest+=delta; if(rest==flow) break;
            }
        }
    }
    if(rest!=flow) lev[now]=-1;
    return rest;
}
int main()
{
    int n,f,d,fi,di,x;
    scanf("%d%d%d",&n,&f,&d);
    decc=(n<<1|1)+f+d+1;
    for(int i=1;i<=f;i++) add(src,(n<<1|1)+i,1);
    for(int i=1;i<=d;i++) add((n<<1|1)+f+i,decc,1);
    for(int i=1;i<=n;i++) add(i<<1,i<<1|1,1);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&fi,&di);
        for(int j=1;j<=fi;j++) 
        {
            scanf("%d",&x);
            add((n<<1|1)+x,i<<1,1);
        }
        for(int j=1;j<=di;j++)
        {
            scanf("%d",&x);
            add(i<<1|1,(n<<1|1)+f+x,1);
        }
    }
    while(bfs()) ans+=dinic(src,N);
    printf("%d",ans);
}

错误:拆点方式:i<<1,i<<1|1,所以最后一头牛的编号为n<<1|1

 

poj 3281 Dining

标签:lan   lines   cer   ace   time   using   eal   ++   mem   

原文地址:http://www.cnblogs.com/TheRoadToTheGold/p/6881404.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!