POJ 3281 Dining,poj3281dining

來源:互聯網
上載者:User

POJ 3281 Dining,poj3281dining

ISAP最大流...果粉專用的最大流


Dining
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 9573   Accepted: 4417

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: N, F, 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 32 2 1 2 3 12 2 2 3 1 22 2 1 3 1 22 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

USACO 2007 Open Gold



#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=500;const int maxm=maxn*maxn;const int INF=0x3f3f3f3f;struct Edge{    int to,next,cap,flow;}edge[maxm];int Size,Adj[maxn];int gap[maxn],dep[maxn],pre[maxn],cur[maxn];void init(){    Size=0;    memset(Adj,-1,sizeof(Adj));}void addedge(int u,int v,int w,int rw=0){    edge[Size].to=v; edge[Size].cap=w; edge[Size].next=Adj[u];    edge[Size].flow=0; Adj[u]=Size++;    edge[Size].to=u; edge[Size].cap=rw; edge[Size].next=Adj[v];    edge[Size].flow=0; Adj[v]=Size++;}int sap(int start,int end,int N){    memset(gap,0,sizeof(gap));    memset(dep,0,sizeof(dep));    memcpy(cur,Adj,sizeof(Adj));    int u=start;    pre[u]=-1; gap[0]=N;    int ans=0;    while(dep[start]<N)    {        if(u==end)        {            int Min=INF;            for(int i=pre[u];~i;i=pre[edge[i^1].to])                if(Min>edge[i].cap-edge[i].flow)                    Min=edge[i].cap-edge[i].flow;            for(int i=pre[u];~i;i=pre[edge[i^1].to])            {                edge[i].flow+=Min;                edge[i^1].flow-=Min;            }            u=start;            ans+=Min;            continue;        }        bool flag=false;        int v;        for(int i=cur[u];~i;i=edge[i].next)        {            v=edge[i].to;            if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u])            {                flag=true;                cur[u]=pre[v]=i;                break;            }        }        if(flag)        {            u=v;            continue;        }        int Min=N;        for(int i=Adj[u];~i;i=edge[i].next)            if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min)            {                Min=dep[edge[i].to];                cur[u]=i;            }        gap[dep[u]]--;        if(!gap[dep[u]]) return ans;        dep[u]=Min+1;        gap[dep[u]]++;        if(u!=start) u=edge[pre[u]^1].to;    }    return ans;}int N,F,D;bool linked[600][600];int main(){    while(scanf("%d%d%d",&N,&F,&D)!=EOF)    {        init();        memset(linked,false,sizeof(linked));        for(int i=1;i<=N;i++)        {            int nf,nd;            scanf("%d%d",&nf,&nd);            for(int j=0;j<nf;j++)            {                int x;                scanf("%d",&x);                if(linked[0][x]==false)                {                    linked[0][x]=true;                    addedge(0,x,1);                }                if(linked[x][i+F]==false)                {                    linked[x][i+F]=true;                    addedge(x,i+F,1);                }            }            for(int j=0;j<nd;j++)            {                int x;                scanf("%d",&x);                if(linked[i+F+N][F+2*N+x]==false)                {                    linked[i+F+N][F+2*N+x]=true;                    addedge(i+F+N,F+2*N+x,1);                }                if(linked[x+F+2*N][F+2*N+D+1]==false)                {                    linked[x+F+2*N][F+2*N+D+1]=true;                    addedge(x+F+2*N,F+2*N+D+1,1);                }            }            if(linked[i+F][i+F+N]==false)            {                linked[i+F][i+F+N]=true;                addedge(i+F,i+F+N,1);            }        }        printf("%d\n",sap(0,F+2*N+D+1,F+2*N+D+2));    }    return 0;}



POJ上 對於二叉樹遍曆的題目

最小產生樹演算法(prim,kruskal) (poj1789,poj2485,poj1258,poj3026)
trie樹(靜態建樹、動態建樹) (poj2513)
線段樹. (poj2528,poj2828,poj2777,poj2886,poj2750)
靜態二叉檢索樹. (poj2482,poj2352)
樹狀樹組(poj1195,poj3321)
最短路,最小產生樹,二分圖,最大流問題的相關理論(主要是模型建立和求解)
(poj3155, poj2112,poj1966,poj3281,poj1087,poj2289,poj3216,poj2446)
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.