zoukankan      html  css  js  c++  java
  • Dining(最大流)

    Dining
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 11844   Accepted: 5444

    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 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.
    题解:一共n头母牛,想要吃的和喝的,每头母牛有一样吃的有一样喝的就满足了,现在有F种吃的,D种喝的,吃的和喝的只能被用一次,求可以满足的母牛个数;
    这个题可以加一个源点,一个终点,将吃的左边与源点连,右边与母牛连,将母牛拆点,母牛右边与喝的连,喝的与终点连。。。
    代码:
     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<queue>
     6 #include<algorithm>
     7 #define mem(x,y) memset(x,y,sizeof(x))
     8 using namespace std;
     9 const int INF=0x3f3f3f3f;
    10 const int MAXN=410;
    11 const int MAXM=50010<<1;
    12 int head[MAXM];
    13 int vis[MAXN],dis[MAXN];
    14 int edgnum;
    15 struct Node{
    16     int from,to,next,cup,flow;
    17 };
    18 Node edg[MAXM];
    19 queue<int>dl;
    20 void add(int u,int v,int w){
    21     Node E={u,v,head[u],w,0};
    22     edg[edgnum]=E;
    23     head[u]=edgnum++;
    24     E={v,u,head[v],0,0};
    25     edg[edgnum]=E;
    26     head[v]=edgnum++;
    27 }
    28 void initial(){
    29     mem(head,-1);edgnum=0;
    30 }
    31 bool bfs(int s,int e){
    32     mem(vis,0);mem(dis,-1);
    33     while(!dl.empty())dl.pop();
    34     dis[s]=0;vis[s]=1;dl.push(s);
    35     while(!dl.empty()){
    36         int u=dl.front();
    37         dl.pop();
    38         for(int i=head[u];i!=-1;i=edg[i].next){
    39             Node v=edg[i];
    40             if(!vis[v.to]&&v.cup>v.flow){
    41                 vis[v.to]=1;dl.push(v.to);
    42                 dis[v.to]=dis[u]+1;
    43                 if(v.to==e)return true;
    44             }
    45         }
    46     }
    47     return false;
    48 }
    49 int dfs(int x,int la,int e){
    50     if(x==e||la==0)return la;
    51     int temp;
    52     int flow=0;
    53     for(int i=head[x];i!=-1;i=edg[i].next){
    54         Node &v=edg[i];
    55         if(dis[v.to]==dis[x]+1&&(temp=dfs(v.to,min(la,v.cup-v.flow),e))>0){
    56             v.flow+=temp;
    57             edg[i^1].flow-=temp;
    58             la-=temp;
    59             flow+=temp;
    60             if(la==0)break;
    61         }
    62     }
    63     return flow;
    64 }
    65 int maxflow(int s,int e){
    66     int flow=0;
    67     while(bfs(s,e)){
    68         flow+=dfs(s,INF,e);
    69     }
    70     return flow;
    71 }
    72 int main(){
    73     int n,F,D,f,d,a,b;
    74     while(~scanf("%d%d%d",&n,&F,&D)){
    75         initial();
    76         for(int i=1;i<=n;i++){
    77             scanf("%d%d",&f,&d);
    78             while(f--){
    79                 scanf("%d",&a);
    80                 add(2*n+a,i,1);
    81             }
    82             while(d--){
    83                 scanf("%d",&a);
    84                 add(n+i,2*n+F+a,1);
    85             }
    86             add(i,n+i,1);
    87         }
    88         for(int i=1;i<=F;i++)add(0,2*n+i,1);
    89         for(int i=1;i<=D;i++)add(2*n+F+i,2*n+F+D+1,1);
    90         printf("%d
    ",maxflow(0,2*n+F+D+1));//醉了,应该从0开始,找了半天错。。。 
    91     }
    92     return 0;
    93 }

     另一种解法:

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<queue>
     6 #include<algorithm>
     7 #define mem(x,y) memset(x,y,sizeof(x))
     8 using namespace std;
     9 const int INF=0x3f3f3f3f;
    10 const int MAXN=500;
    11 const int MAXM=50010<<1;
    12 queue<int>dl;
    13 int vis[MAXN],map[MAXN][MAXN],pre[MAXN];
    14 bool bfs(int s,int e){
    15     mem(vis,0);mem(pre,-1);
    16     while(!dl.empty())dl.pop(); //忘了初始化。。。。。 
    17     vis[s]=1;dl.push(s);
    18     int a;
    19     while(!dl.empty()){
    20         a=dl.front();
    21         dl.pop();//忘写pop了。。。 
    22         if(a==e)return true;
    23         for(int i=1;i<=e;i++){
    24             if(!vis[i]&&map[a][i]){
    25                 dl.push(i);
    26                 vis[i]=1;
    27                 pre[i]=a;
    28                 //if(i==e)return true;
    29             }
    30         }
    31     }
    32     return false;
    33 }
    34 int maxflow(int s,int e){
    35     int flow=0;
    36     while(bfs(s,e)){
    37         int temp=INF;
    38         int r=e;
    39         //puts("fasf");
    40         while(r!=s)temp=min(temp,map[pre[r]][r]),r=pre[r];
    41         r=e;
    42         while(r!=s)map[pre[r]][r]-=temp,map[r][pre[r]]+=temp,r=pre[r];
    43         flow+=temp;
    44     }
    45     return flow;
    46 }
    47 int main(){
    48     int n,F,D,f,d,a,b;
    49     while(~scanf("%d%d%d",&n,&F,&D)){
    50         mem(map,0);
    51         for(int i=1;i<=n;i++){
    52             scanf("%d%d",&f,&d);
    53             while(f--){
    54                 scanf("%d",&a);
    55             //    add(2*n+a,i,1);
    56                 map[2*n+a][i]=1;
    57             }
    58             while(d--){
    59                 scanf("%d",&a);
    60             //    add(n+i,2*n+F+a,1);
    61                 map[n+i][2*n+F+a]=1;
    62             }
    63             //add(i,n+i,1);
    64             map[i][n+i]=1;
    65         }
    66         for(int i=1;i<=F;i++)map[0][2*n+i]=1;//add(0,2*n+i,1);
    67         for(int i=1;i<=D;i++)map[2*n+F+i][2*n+F+D+1]=1;//add(2*n+F+i,2*n+F+D+1,1);
    68         printf("%d
    ",maxflow(0,2*n+F+D+1));//醉了,应该从0开始,找了半天错。。。 
    69     }
    70     return 0;
    71 } 
  • 相关阅读:
    linux 学习笔记 groupadd创建组
    linux学习笔记 4建立用户
    Linux学习笔记 3 权限篇
    Linux学习笔记 1 环境变量 2 vi命令
    指针 以及取地址
    练习题
    weblogic domain creation
    hibernate log4j 输出sql
    练习九 组函数应用
    练习八 spool导出
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4937268.html
Copyright © 2011-2022 走看看