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

    Dining

    http://poj.org/problem?id=3281

    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 23462   Accepted: 10371

    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.

    建图是关键。。。

    把牛拆成两个点才能起到限流的作用

    源点连食物,食物连牛in,牛in连牛out,牛out连饮料,饮料连汇点

     不能牛连食物,食物连饮料,不然食物可能会被使用一次以上,在这里想了半天,太菜了= =

      1 #include<iostream>
      2 #include<cstring>
      3 #include<string>
      4 #include<cmath>
      5 #include<cstdio>
      6 #include<algorithm>
      7 #include<queue>
      8 #include<vector>
      9 #include<set>
     10 #define maxn 100005
     11 #define MAXN 100005
     12 #define mem(a,b) memset(a,b,sizeof(a))
     13 const int N=200005;
     14 const int M=200005;
     15 const int INF=0x3f3f3f3f;
     16 using namespace std;
     17 int n;
     18 struct Edge{
     19     int v,next;
     20     int cap,flow;
     21 }edge[MAXN*20];//注意这里要开的够大。。不然WA在这里真的想骂人。。问题是还不报RE。。
     22 int cur[MAXN],pre[MAXN],gap[MAXN],path[MAXN],dep[MAXN];
     23 int cnt=0;//实际存储总边数
     24 void isap_init()
     25 {
     26     cnt=0;
     27     memset(pre,-1,sizeof(pre));
     28 }
     29 void isap_add(int u,int v,int w)//加边
     30 {
     31     edge[cnt].v=v;
     32     edge[cnt].cap=w;
     33     edge[cnt].flow=0;
     34     edge[cnt].next=pre[u];
     35     pre[u]=cnt++;
     36 }
     37 void add(int u,int v,int w){
     38     isap_add(u,v,w);
     39     isap_add(v,u,0);
     40 }
     41 bool bfs(int s,int t)//其实这个bfs可以融合到下面的迭代里,但是好像是时间要长
     42 {
     43     memset(dep,-1,sizeof(dep));
     44     memset(gap,0,sizeof(gap));
     45     gap[0]=1;
     46     dep[t]=0;
     47     queue<int>q;
     48     while(!q.empty())
     49     q.pop();
     50     q.push(t);//从汇点开始反向建层次图
     51     while(!q.empty())
     52     {
     53         int u=q.front();
     54         q.pop();
     55         for(int i=pre[u];i!=-1;i=edge[i].next)
     56         {
     57             int v=edge[i].v;
     58             if(dep[v]==-1&&edge[i^1].cap>edge[i^1].flow)//注意是从汇点反向bfs,但应该判断正向弧的余量
     59             {
     60                 dep[v]=dep[u]+1;
     61                 gap[dep[v]]++;
     62                 q.push(v);
     63                 //if(v==sp)//感觉这两句优化加了一般没错,但是有的题可能会错,所以还是注释出来,到时候视情况而定
     64                 //break;
     65             }
     66         }
     67     }
     68     return dep[s]!=-1;
     69 }
     70 int isap(int s,int t)
     71 {
     72     if(!bfs(s,t))
     73     return 0;
     74     memcpy(cur,pre,sizeof(pre));
     75     //for(int i=1;i<=n;i++)
     76     //cout<<"cur "<<cur[i]<<endl;
     77     int u=s;
     78     path[u]=-1;
     79     int ans=0;
     80    // cout<<dep[s]<<" "<<n<<endl;
     81     while(dep[s]<n)//迭代寻找增广路
     82     {
     83         if(u==t)
     84         {
     85             int f=INF;
     86             for(int i=path[u];i!=-1;i=path[edge[i^1].v])//修改找到的增广路
     87                 f=min(f,edge[i].cap-edge[i].flow);
     88             for(int i=path[u];i!=-1;i=path[edge[i^1].v])
     89             {
     90                 edge[i].flow+=f;
     91                 edge[i^1].flow-=f;
     92             }
     93             ans+=f;
     94             u=s;
     95             continue;
     96         }
     97         bool flag=false;
     98         int v;
     99         for(int i=cur[u];i!=-1;i=edge[i].next)
    100         {
    101             v=edge[i].v;
    102             if(dep[v]+1==dep[u]&&edge[i].cap-edge[i].flow)
    103             {
    104                 cur[u]=path[v]=i;//当前弧优化
    105                 flag=true;
    106                 break;
    107             }
    108         }
    109         if(flag)
    110         {
    111             u=v;
    112             continue;
    113         }
    114         int x=n;
    115         if(!(--gap[dep[u]]))return ans;//gap优化
    116         for(int i=pre[u];i!=-1;i=edge[i].next)
    117         {
    118             if(edge[i].cap-edge[i].flow&&dep[edge[i].v]<x)
    119             {
    120                 x=dep[edge[i].v];
    121                 cur[u]=i;//常数优化
    122             }
    123         }
    124         dep[u]=x+1;
    125         gap[dep[u]]++;
    126         if(u!=s)//当前点没有增广路则后退一个点
    127         u=edge[path[u]^1].v;
    128      }
    129      return ans;
    130 }
    131 
    132 
    133 int main(){
    134     int m,s,t;
    135     int F,D;
    136     scanf("%d %d %d",&n,&F,&D);
    137     int a,b,c;
    138     isap_init();
    139     int x1[105],x2[105];
    140     int f,d;
    141     for(int i=1;i<=n;i++){
    142         scanf("%d %d",&f,&d);
    143         for(int j=1;j<=f;j++){
    144             scanf("%d",&x1[j]);
    145             add(x1[j],F+i,1);
    146         }
    147         for(int j=1;j<=d;j++){
    148             scanf("%d",&x2[j]);
    149             for(int k=1;k<=f;k++){
    150                 add(n+F+i,x2[j]+n+n+F,1);
    151             }
    152         }
    153     }
    154     for(int i=1;i<=F;i++){
    155         add(0,i,1);
    156     }
    157     for(int i=1;i<=n;i++){
    158         add(F+i,n+F+i,1);
    159     }
    160     for(int i=1;i<=D;i++){
    161         add(i+n+n+F,F+n+n+D+1,1);
    162     }
    163     s=0,t=F+n+n+D+1;
    164     n=F+n+n+D+2;
    165     printf("%d
    ",isap(s,t));
    166 }
    167 
    168 
    169 /*
    170 2 2 2
    171 2 1 1 2 1
    172 1 1 1 2
    173 
    174 2
    175 */
    View Code
  • 相关阅读:
    开源的excel读取库libxls在windows下的编译,且支持中文,全网首发
    UTF8与GBK、GB2312等其他字符编码的相互转换
    深入理解Netty框架
    论一道编程面试题解法
    JVM调优总结-Xms -Xmx -Xmn -Xss
    Linux上安装jdk1.8和配置环境变量
    .NET GC垃圾回收器
    T4 模板
    Vue 环境搭建
    Spring Cloud 入门系列(一)
  • 原文地址:https://www.cnblogs.com/Fighting-sh/p/9818674.html
Copyright © 2011-2022 走看看