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

时间:2023-01-15 20:14:00

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