zoukankan      html  css  js  c++  java
  • POJ 3281 Dining(网络流-拆点)

    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 Fiintegers 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头牛,每头牛都有喜欢的饮料喝喜欢的食物,而每一种食物和饮料都只能给一头牛,让你求,最多可以分配多少头牛食物饮料;
    由于每种饮料喝每种食物只能给一种牛,因此我们可以将牛这个节点拆分为一条容量为1的边,然后让牛喝饮料喝食物分别建立一条容量为1的边,这样就转化为
    最大流的问题;
    参考代码为:
      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<cmath>
      5 #include<cstdlib>
      6 #include<algorithm>
      7 #include<queue>
      8 #include<deque>
      9 #include<stack>
     10 #include<set>
     11 #include<vector>
     12 #include<map>
     13 using namespace std;
     14 typedef long long LL;
     15 typedef pair<int,int> PII;
     16 #define PI acos(-1)
     17 #define EPS 1e-8
     18 const int INF=0x3f3f3f3f;
     19 const int maxn = 520;
     20 int n,m,k,s,t,x,y,z;
     21 struct Edge { 
     22     int from, to, cap, flow;
     23 };
     24 vector<Edge> edges;
     25 vector<int> G[maxn];
     26 bool vis[maxn];
     27 int d[maxn], cur[maxn];
     28 
     29 void Init()
     30 {
     31     memset(d,0,sizeof d);
     32     for(int i=0;i<=n;i++) G[i].clear();
     33 }
     34 
     35 void addedge(int from, int to, int cap) 
     36 {
     37     edges.push_back((Edge){from, to, cap, 0});
     38     edges.push_back((Edge){to, from, 0, 0});
     39     int m = edges.size();
     40     G[from].push_back(m-2); G[to].push_back(m-1);
     41 }
     42 
     43 bool bfs() 
     44 {
     45     memset(vis,0,sizeof vis);
     46     queue<int> q;
     47     q.push(s);
     48     d[s] = 0; vis[s] = 1;
     49     while (!q.empty()) 
     50     {
     51         int x = q.front(); q.pop();
     52         for(int i = 0; i < G[x].size(); ++i) 
     53         {
     54             Edge &e = edges[G[x][i]];
     55             if (!vis[e.to] && e.cap > e.flow) 
     56             {
     57                 vis[e.to] = 1;
     58                 d[e.to] = d[x] + 1;
     59                 q.push(e.to);
     60             }
     61         }
     62     }
     63     return vis[t];
     64 }
     65 
     66 int dfs(int x,int a) 
     67 {
     68     if(x == t || a == 0) return a;
     69     int flow = 0, f;
     70     for(int &i = cur[x]; i < G[x].size(); ++i) 
     71     {
     72         Edge &e = edges[G[x][i]];
     73         if (d[e.to] == d[x] + 1 && (f=dfs(e.to, min(a, e.cap-e.flow))) > 0) 
     74         {
     75             e.flow += f;
     76             edges[G[x][i]^1].flow -= f;
     77             flow += f; a -= f;
     78             if (a == 0) break;
     79         }
     80     }
     81     return flow;
     82 }
     83 
     84 int Maxflow(int s, int t) 
     85 {
     86     int flow = 0;
     87     while (bfs()) 
     88     {
     89         memset(cur,0,sizeof cur);
     90         flow += dfs(s, INF);
     91     }
     92     return flow;
     93 }
     94 
     95 int main()
     96 {
     97     while(~scanf("%d%d%d",&n,&m,&k))
     98     {
     99         Init();
    100         s=0,t=2*n+m+k+1;
    101         for(int i=1;i<=m;i++) addedge(s,i,1);
    102         for(int i=1;i<=n;i++) addedge(m+i,n+m+i,1);
    103         for(int i=2*n+m+1;i<=2*n+m+k;i++) addedge(i,t,1);
    104         for(int i=1;i<=n;i++)
    105         {
    106             scanf("%d%d",&x,&y);
    107             for(int j=1;j<=x;j++) scanf("%d",&z),addedge(z,m+i,1);
    108             for(int j=1;j<=y;j++) scanf("%d",&z),addedge(m+n+i,z+m+2*n,1);
    109         }
    110         printf("%d
    ",Maxflow(s,t));
    111     }
    112     return 0;
    113 }
    View Code
     
  • 相关阅读:
    SQL: 从一个表随机读取一行或几行记录的问题
    Android: 创建一个AlertDialog对话框,必须按确定或取消按钮才能关闭对话框,禁止按[返回键]或[搜索键]关闭
    Asp: 解决脚本输出网页出现乱码情况
    Java: |(或运算) 与 多选判断
    ASP: Response 对象 错误 'ASP 0251 : 80004005' 解决办法
    Java: 在dos窗口输入密码,不要把密码直接显示出来,原来可以这么简单
    Android: 网络随时需要在3G和Wifi切换,网络程序需要注意
    Android: 待机时如何让程序继续运行 extends Service
    Android: View换切后,无法正常设置焦点或切换后TextView的虚拟键盘不弹出
    MySQL存储过程学习笔记
  • 原文地址:https://www.cnblogs.com/csushl/p/9526146.html
Copyright © 2011-2022 走看看