POJ 3281 Dining(最大流dinic&&SAP)

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.

题意:每头牛都要一种食物和一种饮料,最多多少牛能够满足。

网络流拆点求最大流。建图,S->食物->牛->牛->饮料->T

因为牛的数量是一定的,为了保证每头牛用一次要拆点,A->A‘之间连一条容量为1的边。

跑一下最大流即可。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int mod = 1000000007;
const int INF=0x3f3f3f3f;
const int maxn=440;
int mp[maxn][maxn];
int dis[maxn],q[maxn];
int n,f,d,N,maxflow,s,t;
bool BFS()
{
    CLEAR(dis,-1);
    dis[s]=0;
    int h=0,r=0;
    q[++r]=s;
    while(h<r)
    {
        int x=q[++h];
        for(int i=0;i<N;i++)
        {
            if(dis[i]<0&&mp[x][i])
            {
                dis[i]=dis[x]+1;
                q[++r]=i;
            }
        }
    }
    if(dis[t]>0)  return true;
    else  return false;
}
int dfs(int x,int low)
{
    int a=0;
    if(x==t)  return low;
    for(int i=0;i<N;i++)
    {
        if(mp[x][i]>0&&dis[i]==dis[x]+1&&(a=dfs(i,min(low,mp[x][i]))))
        {
            mp[x][i]-=a;
            mp[i][x]+=a;
            return a;
        }
    }
    return 0;
}
void dinic()
{
    maxflow=0;
    while(BFS())
        maxflow+=dfs(s,INF);
}
int main()
{
    int s1,s2,x;
    while(~scanf("%d%d%d",&n,&f,&d))
    {
        CLEAR(mp,0);
        s=0;t=2*n+f+d+1;
        for(int i=1;i<=f;i++)  mp[s][i]=1;
        for(int i=1;i<=d;i++)  mp[2*n+f+i][t]=1;
        for(int i=1;i<=n;i++)  mp[f+i][f+n+i]=1;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&s1,&s2);
            for(int j=1;j<=s1;j++)
            {
                scanf("%d",&x);
                mp[x][f+i]=1;
            }
            for(int j=1;j<=s2;j++)
            {
                scanf("%d",&x);
                mp[f+n+i][f+2*n+x]=1;
            }
        }
        N=t+1;
        dinic();
        printf("%d\n",maxflow);
    }
    return 0;
}

HDU 4292 :

Problem Description

  You, a part-time dining service worker in your college’s dining hall, are now confused with a new problem: serve as many people as possible.

  The issue comes up as people in your college are more and more difficult to serve with meal: They eat only some certain kinds of food and drink, and with requirement unsatisfied, go away directly.

  You have prepared F (1 <= F <= 200) kinds of food and D (1 <= D <= 200) kinds of drink. Each kind of food or drink has certain amount, that is, how many people could this food or drink serve. Besides, You know there’re N (1 <= N <= 200) people and you too
can tell people’s personal preference for food and drink.

  Back to your goal: to serve as many people as possible. So you must decide a plan where some people are served while requirements of the rest of them are unmet. You should notice that, when one’s requirement is unmet, he/she would just go away, refusing any
service.

Input

  There are several test cases.

  For each test case, the first line contains three numbers: N,F,D, denoting the number of people, food, and drink.

  The second line contains F integers, the ith number of which denotes amount of representative food.

  The third line contains D integers, the ith number of which denotes amount of representative drink.

  Following is N line, each consisting of a string of length F. e jth character in the ith one of these lines denotes whether people i would accept food j. “Y” for yes and “N” for no.

  Following is N line, each consisting of a string of length D. e jth character in the ith one of these lines denotes whether people i would accept drink j. “Y” for yes and “N” for no.

  Please process until EOF (End Of File).

Output

  For each test case, please print a single line with one integer, the maximum number of people to be satisfied.

Sample Input

4 3 3
1 1 1
1 1 1
YYN
NYY
YNY
YNY
YNY
YYN
YYN
NNY

Sample Output

3

题意差不多,建图差不多。

只不过食物和牛以及牛与饮料的关系用图表示了。我的没优化的dinic跑TLE了。

SAP:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int mod = 1000000007;
const int INF=0x3f3f3f3f;
const int MAXN=1500;
const int MAXM=200010;
struct node
{
    int from,to,next;
    int cap;
}e[MAXM];
int head[MAXN],d[MAXN];
int tol,mp[MAXN];
int st,ed,n;
int N,F,D;
void addedge(int u,int v,int w)
{
    e[tol].from=u;
    e[tol].to=v;
    e[tol].cap=w;
    e[tol].next=head[u];
    head[u]=tol++;
    e[tol].from=v;
    e[tol].to=u;
    e[tol].cap=0;
    e[tol].next=head[v];
    head[v]=tol++;
}
void BFS(int st,int ed)
{
    memset(d,-1,sizeof(d));
    memset(mp,0,sizeof(mp));
    mp[0]=1;
    int q[MAXN];
    int front,rear;
    front=rear=0;
    d[ed]=0;
    q[rear++]=ed;
    while(front!=rear)
    {
        int u=q[front++];
        if(front==MAXN)front=0;
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            int v=e[i].to;
            if(d[v]!=-1)continue;
            q[rear++]=v;
            if(rear==MAXN)rear=0;
            d[v]=d[u]+1;
            ++mp[d[v]];
        }
    }
}
int SAP(int s,int t)
{
    int res=0;
    BFS(st,ed);
    int cur[MAXN];
    int S[MAXN];
    int top=0;
    memcpy(cur,head,sizeof(head));
    int u=s;
    int i;
    while(d[s]<n)
    {
        if(u==t)
        {
            int temp=INF;
            int inser;
            for(i=0;i<top;i++)
               if(temp>e[S[i]].cap)
               {
                   temp=e[S[i]].cap;
                   inser=i;
               }
            for(i=0;i<top;i++)
            {
                e[S[i]].cap-=temp;
                e[S[i]^1].cap+=temp;
            }
            res+=temp;
            top=inser;
            u=e[S[top]].from;
        }
        if(u!=t&&mp[d[u]-1]==0)//出现断层,无增广路
          break;
        for(i=cur[u];i!=-1;i=e[i].next)
           if(e[i].cap!=0&&d[u]==d[e[i].to]+1)
             break;
        if(i!=-1)
        {
            cur[u]=i;
            S[top++]=i;
            u=e[i].to;
        }
        else
        {
            int min=n;
            for(i=head[u];i!=-1;i=e[i].next)
            {
                if(e[i].cap==0)continue;
                if(min>d[e[i].to])
                {
                    min=d[e[i].to];
                    cur[u]=i;
                }
            }
            --mp[d[u]];
            d[u]=min+1;
            ++mp[d[u]];
            if(u!=st)u=e[S[--top]].from;
        }
    }
    return res;
}

int main()
{
    int x;char str[220][220];
    while(~scanf("%d%d%d",&N,&F,&D))
    {
        CLEAR(head,-1);tol=0;
        st=0;ed=2*N+F+D+1;
        for(int i=1;i<=F;i++)
        {
            scanf("%d",&x);
            addedge(st,i,x);
        }
        for(int i=1;i<=D;i++)
        {
            scanf("%d",&x);
            addedge(2*N+F+i,ed,x);
        }
        for(int i=1;i<=N;i++)
           addedge(F+i,F+N+i,1);
        for(int i=1;i<=N;i++)
        {
            scanf("%s",&str[i]);
            for(int j=0;j<F;j++)
               if(str[i][j]=='Y')  addedge(j+1,F+i,1);
        }
        for(int i=1;i<=N;i++)
        {
            scanf("%s",&str[i]);
            for(int j=0;j<D;j++)
               if(str[i][j]=='Y')
                   addedge(F+N+i,2*N+F+j+1,1);
        }
        n=ed+1;
        printf("%d\n",SAP(st,ed));
    }
    return 0;
}
时间: 2024-11-03 21:41:33

POJ 3281 Dining(最大流dinic&&SAP)的相关文章

POJ 3281 Dining 最大流 Dinic算法

Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10768   Accepted: 4938 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 fabulo

POJ 3281 Dining(最大流)

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

POJ 3281 Dining (最大流)

Dining Time Limit: 2000MS   Memory Limit: 65536K       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

【网络流#7】POJ 3281 Dining 最大流 - 《挑战程序设计竞赛》例题

不使用二分图匹配,使用最大流即可,设源点S与汇点T,S->食物->牛->牛->饮料->T,每条边流量为1,因为流过牛的最大流量是1,所以将牛拆成两个点. 前向星,Dinic,复杂度:O(V2E) 直接套用模板 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<set

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种饮料,及食物种类列表和饮料种类列表. 问最多能使几头牛同时享用到自己喜欢的食物和饮料.->最大流. 本题难点是建图: 思路:一般都是左边一个集合表示源点与供应相连,右边一个集合表示需求与汇点相连. 但是本题,牛作为需求仍然是一个群体,但是供

最大流 Dinic + Sap 模板

不说别的,直接上模板. Dinic+当前弧优化: struct Edge{ int x,y,c,ne; }e[M*2]; int be[N],all; int d[N],q[N]; int stack[N],top;//栈存的是边 int cur[N];//当前弧优化 void add(int x, int y, int z)//需保证相反边第一个为偶数 { e[all].x=x; e[all].y=y; e[all].c=z; e[all].ne=be[x]; be[x]=all++; e[a

POJ 3281 Dining (网络流最大流 拆点建图 Edmonds-Karp算法)

Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10159   Accepted: 4676 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 fabulo