poj 3281 最大流拆点


Language:
Default

Dining

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10321   Accepted: 4744

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 theDi 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.

这是一道拆点题,要把牛拆点,主要就是因为每头牛最多只能喂饱一次,然后就是纯粹的最大流了,下面是ac代码:

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<climits>
using namespace std;
struct Edge
{
    int s,t,c,next;
}edge[1010000];
int head[440];
int pre[440];
int n,f,d;
int fi,di,fc[110],dc[110],s,t;
int Min(int a,int b)
{
    return a<b?a:b;
}
void init()
{
    int ent=0;
    memset(head,-1,sizeof(head));
    scanf("%d%d%d",&n,&f,&d);
    s=0;t=f+d+2*n+1;
    for(int i=1;i<=f;i++)
    {
        edge[ent].s=0;edge[ent].t=i;edge[ent].c=1;edge[ent].next=head[0];head[0]=ent++;
        edge[ent].s=i;edge[ent].t=0;edge[ent].c=0;edge[ent].next=head[i];head[i]=ent++;
    }
    for(int i=1;i<=d;i++)
    {
        edge[ent].s=f+i;edge[ent].t=t;edge[ent].c=1;edge[ent].next=head[f+i];head[f+i]=ent++;
        edge[ent].s=t;edge[ent].t=f+i;edge[ent].c=0;edge[ent].next=head[t];head[t]=ent++;
    }
    for(int i=f+d+1;i<=f+d+n;i++)
    {
        edge[ent].s=i;edge[ent].t=i+n;edge[ent].c=1;edge[ent].next=head[i];head[i]=ent++;
        edge[ent].s=i+n;edge[ent].t=i;edge[ent].c=0;edge[ent].next=head[i+n];head[i+n]=ent++;
    }
    for(int i=1;i<=n;i++)
    {
        int cow=f+d+i;
        scanf("%d%d",&fi,&di);
        for(int j=0;j<fi;j++)
        {
            scanf("%d",&fc[j]);
            int ok=1;
            for(int k=head[fc[j]];k!=-1;k=edge[k].next)
            {
                if(edge[k].t==cow)
                {
                    ok=0;break;
                }
            }
            if(ok)
            {
                edge[ent].s=fc[j];edge[ent].t=cow;edge[ent].c=1;edge[ent].next=head[fc[j]];head[fc[j]]=ent++;
                edge[ent].s=cow;edge[ent].t=fc[j];edge[ent].c=0;edge[ent].next=head[cow];head[cow]=ent++;
            }
        }
        for(int j=0;j<di;j++)
        {
            scanf("%d",&dc[j]);
            dc[j]+=f;
            int ok=1;
            for(int k=head[cow];k!=-1;k=edge[k].next)
            {
                if(edge[k].t==dc[j])
                {
                    ok=0;break;
                }
            }
            if(ok)
            {
                edge[ent].s=cow+n;edge[ent].t=dc[j];edge[ent].c=1;edge[ent].next=head[cow+n];head[cow+n]=ent++;
                edge[ent].s=dc[j];edge[ent].t=cow+n;edge[ent].c=0;edge[ent].next=head[dc[j]];head[dc[j]]=ent++;
            }
        }
    }
}
bool bfs()
{
    memset(pre,-1,sizeof(pre));
    pre[s]=0;
    queue<int>q;
    q.push(s);
    while(!q.empty())
    {
        int temp=q.front();
        q.pop();
        for(int i=head[temp];i!=-1;i=edge[i].next)
        {
            int temp2=edge[i].t;
            if(pre[temp2]==-1&&edge[i].c)
            {
                pre[temp2]=pre[temp]+1;
                q.push(temp2);
            }
        }
    }
    return pre[t]!=-1;
}
int dfs(int start,int minn)
{
    if(start==t)return minn;
    int flow=0;
    int small;
    for(int i=head[start];i!=-1;i=edge[i].next)
    {
        int temp=edge[i].t;
        if(pre[temp]==pre[start]+1&&edge[i].c)
        {
            small=Min(minn-flow,edge[i].c);
            small=dfs(temp,small);
            flow+=small;
            edge[i].c-=small;
            edge[i^1].c+=small;
        }
    }
    return flow;
}
void dinic()
{
    int flow=0;
    while(bfs())
    {
        flow+=dfs(s,INT_MAX);
    }
    printf("%d\n",flow);
}
int main()
{
    init();
    dinic();
    return 0;
}

时间: 2024-08-29 12:58:04

poj 3281 最大流拆点的相关文章

poj 3281 最大流+建图

很巧妙的思想 转自:http://www.cnblogs.com/kuangbin/archive/2012/08/21/2649850.html 本题能够想到用最大流做,那真的是太绝了.建模的方法很妙! 题意就是有N头牛,F个食物,D个饮料. N头牛每头牛有一定的喜好,只喜欢几个食物和饮料. 每个食物和饮料只能给一头牛.一头牛只能得到一个食物和饮料. 而且一头牛必须同时获得一个食物和一个饮料才能满足.问至多有多少头牛可以获得满足. 最初相当的是二分匹配.但是明显不行,因为要分配两个东西,两个东

POJ 3281 /// 最大流

题目大意: n 头牛 f 种食物 d 种饮料 每头牛有各自喜欢的食物和饮料 求最多有多少头牛能分配到自己喜欢的食物和饮料 因为同时有食物和饮料 所以不能用二分图匹配 用最大流解决二分图匹配的办法 增加一个源点连向所有食物 每头牛与各自喜欢的食物连边 增加一个汇点连向所有的饮料 每头牛与各自喜欢的饮料连边 以上边容量都为1 单纯这样连的话 一头牛可能分配到多种食物和饮料 把一头牛拆成两个点 一点与食物连边 另一点与饮料连边 再在两个点之间连一条容量为1的边 这样就能保证只有一个流量流过 即只有一种

Dining POJ - 3281(最大流)

Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25452   Accepted: 11183 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 fabul

POJ 3281 Dining(最大流建图 &amp;&amp; ISAP &amp;&amp; 拆点)

题目链接:http://poj.org/problem?id=3281 努力练建图ing!!! 题意:有 N 头牛,有 F 种食物和 D 种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料. 第2行-第N+1行.是牛i 喜欢A种食物,B种饮料,及食物种类列表和饮料种类列表. 问最多能使几头牛同时享用到自己喜欢的食物和饮料.->最大流. 本题难点是建图: 思路:一般都是左边一个集合表示源点与供应相连,右边一个集合表示需求与汇点相连. 但是本题,牛作为需求仍然是一个群体,但是供

poj 3281 Dining(最大流)

poj 3281 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 prefer

POJ 3281 Dining(网络最大流)

http://poj.org/problem?id=3281 Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9121   Accepted: 4199 Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others.

POJ 3281 Dining(最大流)

POJ 3281 Dining 题目链接 题意:n个牛,每个牛有一些喜欢的食物和饮料,每种食物饮料只有一个,问最大能匹配上多少只牛每个牛都能吃上喜欢的食物和喜欢的饮料 思路:最大流,建模源点到每个食物连一条边,容量为1,每个饮料向汇点连一条边容量为1,然后由于每个牛有容量1,所以把牛进行拆点,然后食物连向牛的入点,牛的出点连向食物,跑一下最大流即可 代码: #include <cstdio> #include <cstring> #include <queue> #in

POJ 3422 Kaka&#39;s Matrix Travels(最大费用最大流 + 拆点)

题目链接:http://poj.org/problem?id=3422 Description On an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travels with SUM = 0. For each travel, Kaka moves one rook from the left-upper grid to the right-bottom one, taking

POJ 3422 HDU 2686,3376 费用流拆点建图

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3376 http://acm.hdu.edu.cn/showproblem.php?pid=2686 http://poj.org/problem?id=3422 POJ 3422为从矩阵左上角走到右下角,最多走k次,每个格子里的数字只能算一次,后面可以重复经过,求经过的各个数字的和的最大值. 拆点,入点向出点连流量为1,费用为当前格子负值的边,向下方,右方连边,流量为k,费用为0,起点连流量为1,