ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

P2891 [USACO07OPEN]吃饭Dining 最大流

2019-01-30 09:02:06  阅读:305  来源: 互联网

标签:dep int 0066ff USACO07OPEN change Dining ls P2891 color


\(\color{#0066ff}{ 题目描述 }\)

有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料。现在有n头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物和饮料。(1 <= f <= 100, 1 <= d <= 100, 1 <= n <= 100)

$\color{#0066ff}{ 输入格式 } $

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.

\(\color{#0066ff}{输出格式}\)

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

\(\color{#0066ff}{输入样例}\)

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

\(\color{#0066ff}{输出样例}\)

3

\(\color{#0066ff}{数据范围与提示}\)

1 <= f <= 100, 1 <= d <= 100, 1 <= n <= 100

\(\color{#0066ff}{ 题解 }\)

最大流

图从左至右依次为s,食物,牛,牛,饮料,t

s向所有食物连容量为1的边,所有饮料向t连容量为1的边

每头牛之间连容量为1的边

然后食物与左边的牛,右边的牛与饮料,按照输入连容量为1的边

这样保证了题目的两个条件

因为可能会有一头牛同时喜欢多种食物和饮料,而我们只能算一次,一头牛最多有1的流量!

#include<bits/stdc++.h>
#define LL long long
LL in() {
    char ch; LL x = 0, f = 1;
    while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
    for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
    return x * f;
}
const int maxn = 1e4;
struct node {
    int to, dis;
    node *nxt, *rev;
    node(int to = 0, int dis = 0, node *nxt = NULL, node *rev = NULL)
        : to(to), dis(dis), nxt(nxt), rev(rev) {}
    void *operator new(size_t) {
        static node *S = NULL, *T = NULL;
        return (S == T) && (T = (S = new node[1024]) + 1024), S++;
    }
}*head[maxn], *cur[maxn];
int dep[maxn];
int n, s, t, na, nb;
void add(int from, int to, int dis) {
    head[from] = new node(to, dis, head[from], NULL);
}
void link(int from, int to, int dis) {
    add(from, to, dis), add(to, from, 0);
    (head[from]->rev = head[to])->rev = head[from];
}
bool bfs() {
    for(int i = s; i <= t; i++) dep[i] = 0, cur[i] = head[i];
    std::queue<int> q;
    q.push(s);
    dep[s] = 1;
    while(!q.empty()) {
        int tp = q.front(); q.pop();
        for(node *i = head[tp]; i; i = i->nxt) 
            if(!dep[i->to] && i->dis)
                dep[i->to] = dep[tp] + 1, q.push(i->to);
    }
    return dep[t];
}
int dfs(int x, int change) {
    if(x == t || !change) return change;
    int flow = 0, ls;
    for(node *i = cur[x]; i; i = i->nxt) {
        cur[x] = i;
        if(dep[i->to] == dep[x] + 1 && (ls = dfs(i->to, std::min(change, i->dis)))) {
            change -= ls;
            flow += ls;
            i->dis -= ls;
            i->rev->dis += ls;
            if(!change) break;
        }
    }
    return flow;
}
int dinic() {
    int flow = 0;
    while(bfs()) flow += dfs(s, 0x7fffffff);
    return flow;
}
//na n n nb
int main() {
    n = in(), na = in(), nb = in();
    s = 0, t = n + n + na + nb + 1;
    for(int i = 1; i <= na; i++) link(s, i, 1);
    for(int i = 1; i <= nb; i++) link(n + na + n + i, t, 1);
    for(int i = 1; i <= n; i++) link(na + i, na + n + i, 1);
    for(int i = 1; i <= n; i++) {
        int ka = in(), kb = in();
        while(ka --> 0) link(in(), na + i, 1);
        while(kb --> 0) link(na + n + i, n + n + na + in(), 1);
    }
    printf("%d\n", dinic());
    return 0;
}

标签:dep,int,0066ff,USACO07OPEN,change,Dining,ls,P2891,color
来源: https://www.cnblogs.com/olinr/p/10336647.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有