POJ 3281 网络流dinic算法

B - Dining

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status Practice POJ 3281

Appoint description:

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.

每组样例有3个数据,代表牛的数量,实物的数量,饮料的数量,每头牛都需要吃特定的食物和饮料,且只能吃一份,每种食物或者饮料被一头牛吃掉后不能再被其他的牛使用,问最多可以满足多少头牛

思路,对每种牛与其固定的·1食物和饮料建边,容量为1,,,,因为每头牛只能食用一种饮料或者食物,所有将牛进行拆点,,中间边容量为1,建立一个超级源点和超级会点,,牛放中间,食物和饮料建在两边就ok了

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
int edge[405][405];//邻接矩阵
int dis[405];//距源点距离,分层图
int start,end;
int m,n;//N:点数;M,边数
int bfs(){
   memset(dis,-1,sizeof(dis));//以-1填充
   dis[0]=0;
   queue<int>q;
   q.push(start);
   while(!q.empty()){
        int u=q.front();
        q.pop();
        for(int i=0;i<=n;i++){
            if(dis[i]<0&&edge[u][i]){
                dis[i]=dis[u]+1;
                q.push(i);

            }
        }
   }
   if(dis[n]>0)
    return 1;
   else
    return 0;//汇点的DIS小于零,表明BFS不到汇点
}
//Find代表一次增广,函数返回本次增广的流量,返回0表示无法增广
int find(int x,int low){//Low是源点到现在最窄的(剩余流量最小)的边的剩余流量
    int a=0;
    if(x==n)
        return low;//是汇点
    for(int i=0;i<=n;i++){
        if(edge[x][i]>0&&dis[i]==dis[x]+1&&//联通,,是分层图的下一层
           (a=find(i,min(low,edge[x][i])))){//能到汇点(a <> 0)
            edge[x][i]-=a;
            edge[i][x]+=a;
            return a;
           }

    }
    return 0;
}
int main(){
    int a,b,c;
   while(scanf("%d%d%d",&a,&b,&c)!=EOF){

        n=a+a+b+c+1;
       memset(edge,0,sizeof(edge));
       for(int i=1;i<=b;i++)
        edge[0][i]=1;
       for(int i=a+a+b+1;i<=a+a+b+c;i++)
        edge[i][n]=1;
           int u;
          int sum1,sum2;
       for(int i=1;i<=a;i++){
       //   int u,v,w;

          scanf("%d%d",&sum1,&sum2);
          for(int j=1;j<=sum1;j++){
                scanf("%d",&u);
            edge[u][i+b]=1;
          }
          for(int j=1;j<=sum2;j++){
               scanf("%d",&u);
               edge[b+a+i][a+a+b+u]=1;
          }

       }
       for(int i=1;i<=a;i++){
          edge[i+b][i+b+a]=1;

       }
       start=0;
       end=n;
       int ans=0;
       while(bfs()){//要不停地建立分层图,如果BFS不到汇点才结束
        ans+=find(0,0x7fffffff);//一次BFS要不停地找增广路,直到找不到为止
       }
       printf("%d\n",ans);
   }
   return 0;
}
时间: 2024-07-31 11:37:35

POJ 3281 网络流dinic算法的相关文章

hdu 3572 Task Schedule(网络流 dinic算法)

Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3412    Accepted Submission(s): 1197 Problem Description Our geometry princess XMM has stoped her study in computational geometry t

POJ 1273 Drainage Ditches(网络流dinic算法模板)

POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdio.h> #include <algorithm> #include <queue> #include <string.h> /* POJ 1273 dinic算法模板 边是有向的,而且存在重边,且这里重边不是取MAX,而是累加和 */ using namespace

POJ 3281 网络流 拆点保证本身只匹配一对食物和饮料

如何建图? 最开始的问题就是,怎么表示一只牛有了食物和饮料呢? 后来发现可以先将食物与牛匹配,牛再去和饮料匹配,实际上这就构成了三个层次. 起点到食物层边的容量是1,食物层到奶牛层容量是1,奶牛层到饮料层容量是1,饮料层到终点容量是1. 但是后来发现有一组hack数据: 2 3 3 3 3 1 2 3 1 2 3 3 3 1 2 3 1 2 3 我们发现一头奶牛居然吃了多个套餐,所以要解决这个只需要将自己与自己建立一条容量是1的边就行了. #include <cstdio> #include

POJ 3281 网络流(dinic邻接矩阵、单路增广、多路增广)

思路:刚开始看题就想到怎么建图了,源点连向所有的食物,食物连牛,牛连饮料,饮料连汇点,所有的流量都是1.不过这样建图好后,WA了.原来是一头牛只能单一匹配一组食物和饮料,所以牛得拆点,牛之间得相连,流量为1,以保证单一匹配食物和饮料. 邻接矩阵dinic单路的代码: #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<map> #in

网络流Dinic算法

详细待.. 紫书模板 const int maxn = 1e5+200; const int inf = 0x3f3f3f3f; struct Edge { int from, to, cap, flow; Edge (int u, int v, int c, int f) : from(u), to(v), cap(c), flow(f) { } }; struct Dinic { int n, m, s, t; vector<Edge> edges; vector<int> G

B - Dining POJ - 3281 网络流

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

POJ 3281 网络流 拆点 Dining

题意: 有F种食物和D种饮料,每头牛有各自喜欢的食物和饮料,而且每种食物或者饮料只能给一头牛. 求最多能有多少头牛能同时得到它喜欢的食物或者饮料. 分析: 把每个牛拆点,中间连一条容量为1的边,保证一头牛不会被多个食物或者饮料分配. 然后把饮料和牛连边,食物和另外一边的牛连边,最后增加一个源点和汇点跑最大流. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <alg

POJ 2135 Farm Tour (dinic算法,网络流)

构图方法: 注意题目中的边为无向边.新建源点s 和 汇点t 每两条道路连一条容量为1,费用为w的边.s到1连一条容量为1,费用为0 的边,n到 t 连一条容量为1,费用为0 的边,求最大流. #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <queue> #include

[POJ 1273]Drainage Ditches(Edmond-Krap算法和Dinic算法求最大流)

自NOIP 2014结束之后将近一个星期没撸题了,现在开始搞省选,发个水水的裸网络流题解吧. 题目链接:http://poj.org/problem?id=1273 裸网络流,模板题. 1.Edmond_Karp算法 #include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <algorithm> #include <que