WOJ1223-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).

输入格式

  • Line 1: Three space-separated integers: N, F, and D

  • Lines 2..N+1: Each line i starts with a two integers F_i and D_i, the number of dishes that cow i likes and the number of drinks that cow i likes. The next F_i integers denote the dishes that cow i will eat, and the D_i integers following that denote the drinks that cow i will drink.

输出格式

  • 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

样例输入

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

样例输出

3

提示

INPUT DETAILS:

Cow 1: foods from {1,2}, drinks from {1,3}
Cow 2: foods from {2,3}, drinks from {1,2}
Cow 3: foods from {1,3}, drinks from {1,2}
Cow 4: foods from {1,3}, drinks from {3}

OUTPUT DETAILS:

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.


#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using std::min;
using std::sort;
using std::pair;
using std::swap;
using std::queue;
using std::vector;
using std::multimap;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 1; i <= (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int Max_N = 1100;
const int INF = 0x3f3f3f3f;
struct Ford_Fulkerson {
    struct edge { int to, cap, next, rev; }G[Max_N << 2];
    int  N, F, D, tot, head[Max_N];
    bool vis[Max_N], Food[Max_N][Max_N], Drink[Max_N][Max_N];
    inline void init(int n, int f, int d) {
        tot = 0;
        this->N = n, this->F = f, this->D =d;
        cls(head, -1), cls(Food, false), cls(Drink, false);
    }
    inline void add_edge(int u, int v, int cap = 1) {
        G[tot] = (edge){ v, cap, head[u], tot + 1 }; head[u] = tot++;
        G[tot] = (edge){ u,   0, head[v], tot - 1 }; head[v] = tot++;
    }
    inline void built() {
        int x, y, v;
        rep(i, N) {
            scanf("%d %d", &x, &y);
            while(x--) {
                scanf("%d", &v);
                Food[i][v] = true;
            }
            while(y--) {
                scanf("%d", &v);
                Drink[i][v] = true;
            }
        }
    }
    inline int dfs(int u, int t, int f) {
        if(u == t) return f;
        vis[u] = true;
        for(int i = head[u]; ~i; i = G[i].next) {
            edge &e = G[i];
            if(e.cap > 0 && !vis[e.to]) {
                int d = dfs(e.to, t, min(e.cap, f));
                if(d > 0) {
                    e.cap -= d;
                    G[e.rev].cap +=d;
                    return d;
                }
            }
        }
        return 0;
    }
    inline void max_flow(int s, int t) {
        int flow = 0;
        while(true) {
            cls(vis, false);
            int f = dfs(s, t, INF);
            if(!f) break;
            flow += f;
        }
        printf("%d\n", flow);
    }
    inline void solve() {
        int s = 1, t = s + F + 2 * N + D + 1;
        rep(i, F) {
            add_edge(s, s + i);
        }
        rep(i, D) {
            add_edge(s + F + 2 * N + i, t);
        }
        rep(i, N) {
            add_edge(s + F + i, s + F + N + i, 1);
            rep(j, F) {
                if(Food[i][j]) add_edge(s + j, s + F + i);
            }
            rep(j, D) {
                if(Drink[i][j]) add_edge(s + F + N + i, s + F + 2 * N + j);
            }
        }
        max_flow(s, t);
    }
}go;
int main() {
    int n, f, d;
    while(~scanf("%d %d %d", &n, &f, &d)) {
        go.init(n ,f ,d);
        go.built();
        go.solve();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值