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: 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

确保每个f和d都只能经过一次,将牛拆点,分别建f到牛,牛到d的容量为1的路,设置一个总起点0和总汇点2*n+d+f+1,跑一遍网络流

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define lowbit(x) (x&(-x))
#define eps 0.00000001
#define pn printf("\n")
using namespace std;
typedef long long ll;


int n,f,d;
const int MAXN = 100010;
const int MAXM = 400010;
struct Edge
{
    int to,next,cap,flow; 
}edge[MAXM];
set <pair<int,int> > st;
int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN]; void init()
{
    tol = 0;
    memset(head,-1,sizeof(head));
}
//加边,单向图三个参数,双向图四个参数
void addedge(int u,int v,int w,int rw=0) {
    edge[tol].to = v;edge[tol].cap = w;edge[tol].next = head[u];
    edge[tol].flow = 0;head[u] = tol++;
    edge[tol].to = u;edge[tol].cap = rw;edge[tol].next = head[v];
    edge[tol].flow = 0;head[v]=tol++;
}

int sap(int start,int end,int N) {
    memset(gap,0,sizeof(gap));
    memset(dep,0,sizeof(dep));
    memcpy(cur,head,sizeof(head));
    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 != -1; 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 != -1; 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 != -1;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 = head[u]; i != -1;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 main()
{
    init();
    int fi, di, x;
    cin >> n >> f >> d;
    for(int i=1;i<=f;i++) addedge(0, 2*n+i, 1);
    for(int i=1;i<=d;i++) addedge(2*n+f+i, 2*n+f+d+1, 1);
    for(int i=1;i<=n;i++) addedge(i, n+i, 1);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&fi,&di);
        for(int j=0;j<fi;j++)
        {
            scanf("%d",&x);
            addedge(2*n+x, i, 1);
        }
        for(int j=0;j<di;j++)
        {
            scanf("%d",&x);
            addedge(i+n,2*n+f+x,1);
        }
    }
    cout << sap(0, 2*n+f+d+1, 2*n+f+d+2) << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值