AC日记——Dining poj 3281

[POJ-3281]

思路:

  把牛拆点;

  s向食物连边,流量1;

  饮料向t连边,流量1;

  食物向牛1连边,流量1;

  牛2向饮料连边,流量1;

  最大流;

来,上代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

#define maxn 2005
#define INF 0x7fffffff

int n,f,d,s,t,head[maxn],E[maxn<<2],V[maxn<<2],F[maxn<<2];
int cnt=1,deep[maxn],que[maxn<<4];

inline void in(int &now)
{
    char Cget=getchar();now=0;
    while(Cget>‘9‘||Cget<‘0‘) Cget=getchar();
    while(Cget>=‘0‘&&Cget<=‘9‘)
    {
        now=now*10+Cget-‘0‘;
        Cget=getchar();
    }
}

inline void edge_add(int u,int v,int f)
{
    E[++cnt]=head[u],V[cnt]=v,F[cnt]=f,head[u]=cnt;
    E[++cnt]=head[v],V[cnt]=u,F[cnt]=0,head[v]=cnt;
}

inline bool bfs()
{
    for(int i=s;i<=t;i++) deep[i]=-1;
    int h=0,tail=1;deep[s]=0,que[h]=s;
    while(h<tail)
    {
        int now=que[h++];
        for(int i=head[now];i;i=E[i])
        {
            if(deep[V[i]]<0&&F[i]>0)
            {
                deep[V[i]]=deep[now]+1;
                if(V[i]==t) return true;
                que[tail++]=V[i];
            }
        }
    }
    return false;
}

int flowing(int now,int flow)
{
    if(flow<=0||now==t) return flow;
    int oldflow=0;
    for(int i=head[now];i;i=E[i])
    {
        if(F[i]>0&&deep[V[i]]==deep[now]+1)
        {
            int pos=flowing(V[i],min(flow,F[i]));
            flow-=pos,oldflow+=pos,F[i]-=pos,F[i^1]+=pos;
            if(flow==0) return oldflow;
        }
    }
    if(oldflow==0) deep[now]=-1;
    return oldflow;
}

int main()
{
    in(n),in(f),in(d);
    int u,v,nf,nd;t=f+n+n+d+1;
    for(int i=1;i<=f;i++) edge_add(s,i,1);
    for(int i=1;i<=d;i++) edge_add(f+n+n+i,t,1);
    for(int i=1;i<=n;i++)
    {
        edge_add(f+i,f+n+i,1),in(nf),in(nd);
        for(int j=1;j<=nf;j++) in(u),edge_add(u,f+i,1);
        for(int j=1;j<=nd;j++) in(u),edge_add(f+n+i,f+n+n+u,1);
    }
    int ans=0;
    while(bfs()) ans+=flowing(s,INF);
    cout<<ans;
    return 0;
}
时间: 2024-10-12 09:36:49

AC日记——Dining poj 3281的相关文章

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

AC日记——Dividing poj 1014

Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 69575   Accepted: 18138 Description Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbl

AC日记——Crane poj 2991

POJ - 2991 思路: 向量旋转: 代码: #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 400005 const double pi=acos(-1.0); struct TreeNodeType { int l,r,R,mid,

AC日记——Oulipo poj 3461

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37958   Accepted: 15282 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote from

B - Dining POJ - 3281 网络流

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

kuangbin专题专题十一 网络流 Dining POJ - 3281

题目链接:https://vjudge.net/problem/POJ-3281 题目:有不同种类的食物和饮料,每种只有1个库存,有N头牛,每头牛喜欢某些食物和某些饮料,但是一头牛 只能吃一种食物和喝一种饮料,问怎么分配食物和饮料才能让最多数量的牛饱餐. 思路:容易想到  食物->牛->饮料的流,当然一个牛可以被多个饮料流到,需要把牛拆成入点和出点,入点和出点流量为1,这样可以保证牛只吃或者喝某种食物和饮料,别的都流是套路,除了牛的分点之间流量为1,别的连接设置成1或者INF都一样,因为有牛的

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(最大流建图 &amp;&amp; ISAP &amp;&amp; 拆点)

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