题目链接

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

AC

  • 建图思路:
    题目问的是最多有几头牛可以同时得到自己想要的食物和水,如果一头牛可以同时可以得到两对合适的食物和水,这样就浪费了,所以要把牛拆开,牛和自己建一条权值为1的边,这样保证一头牛如果可以的话只能得到一对食物和水源,从而使得越多的牛可以得到对应的食物和水

    1. 源点和所有食物建立一条权值为1的边
    2. 食物和牛建立权值为1的边
    3. 牛的副本和对应的水建立权值为1的边
    4. 水和汇点建立权值为1的边
    5. 牛和牛的副本建立权值为1的边

    建好图之后,跑一遍最大流,得到的就是最多牛的个数

#include <iostream>
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#define N 300005
#include <cstring>
#define ll long long
#define P pair<int, int>
#define mk make_pair
using namespace std;int a[405][405], pre[405];
bool vis[405];
int inf = 0x3f3f3f3f;
int n, f, d;
// EK模板
bool bfs(int s, int e) {memset(pre, -1, sizeof(pre));memset(vis, false, sizeof(vis));vis[s] = true;pre[s] = s;queue<int> que;que.push(s);while (!que.empty()) {int t = que.front();que.pop();for (int i = 0; i <= n + n + f + d + 1; ++i) {if (vis[i] || a[t][i] == 0) continue;vis[i] = true;pre[i] = t;if (i == e) return true;que.push(i);}}return false;
}ll solve(int s, int e) {ll sum = 0;while (bfs(s, e)) {int MIN = inf;for (int i = e; i != s; i = pre[i]) {MIN = min(MIN, a[pre[i]][i]);}for (int i = e; i != s; i = pre[i]) {a[pre[i]][i] -= MIN;a[i][pre[i]] += MIN;}   sum += MIN;}return sum;
}int main() {
#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);
#endifscanf("%d%d%d" ,&n, &f, &d);for (int i = 1; i <= n; ++i) {int l, r, t;scanf("%d%d",&l, &r);// food 和 cow 建边 for (int j = 0; j < l; ++j) {scanf("%d", &t);a[t][f + i] = 1;  }// cow 和 water 建边 for (int j = 0; j < r; ++j) {scanf("%d", &t);a[f + i + n][t + f + n + n] = 1;}}// 源点 和 食物建边 for (int i = 1; i <= f; ++i) {a[0][i] = 1;}// 水 和 汇点建边 for (int i = 1; i <= d; ++i) {a[i + f + n + n][n + f + d + 1 + n] = 1;}// 牛和牛之间建边 for (int i = 1; i <= n; ++i) {a[f + i][f + n + i] = 1;}// 跑一边EK ll ans = solve(0, n + n + f + d + 1);printf("%d\n", ans);return 0;
} 

POJ 3281 -- Dining(最大流,拆点建图)相关推荐

  1. POJ.3281 dining 最大流+拆点

    POJ.3281 dining 最大流+拆点 思路清晰为啥一直WA呢 #include <iostream> #include <cstring> #include <v ...

  2. POJ - 3281 Dining(最大流+思维建边)

    题目链接:点击查看 题目大意:给出n头奶牛,f种食物,d种饮料,每只奶牛可以选择数种食物和饮料,但每种食物和饮料只有一份,现在问最多能让多少头奶牛同时满足食物和饮料的条件 题目分析:最大流的题目,不过 ...

  3. poj 3281 Dining 最大流

    题目链接 一开始的想法就是food-cow-water,但是wa了,看了别人的思路知道每一只牛牛用一条边隔开,然后就达到了限流的效果. 所以 食物-->牛牛---->牛牛---->w ...

  4. 【POJ - 3281】Dining(拆点建图,网络流最大流)

    题干: Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she wi ...

  5. 解题报告:POJ 3281 Dining(最大流 / “三分图”建图)

    B.POJ 3281 DiningDiningDining(最大流/建图模板)[省选/NOI- ] 有 F 种食物和 D 种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一 种食物和一种饮料.现 ...

  6. POJ 3281 (最大流+匹配+拆点)

    题目链接:http://poj.org/problem?id=3281 题目大意:有一些牛,一堆食物,一堆饮料.一头牛要吃一份食物喝一份饮料才算满足,而且牛对某些食物和饮料才有好感,问最多有多少头牛是 ...

  7. POJ 2226 Muddy Fields 最小点覆盖+加建图(好题)

    题目链接 题目一看就是最小点覆盖,这道题与POJ - 3041 算是一类题,但是3041算是一道十分裸的,因为删除的是整行或者整列,所以图其实是现成的,但是本题的难点就在如何建图. 思路:首先还是尽量 ...

  8. POJ - 1847 Tram 最短路,思维建图

    题目链接 POJ-1847 题意 给定n节点,节点之间有道路相连,但是每个节点都有个开关,只有开关指向的节点才能通行,你可以搬动开关.给定起点终点,求最少搬动开关次数. 解法 建图,对于每个节点,初始 ...

  9. BZOJ 3144 [HNOI2013]切糕 (最大流+巧妙的建图)

    题面:洛谷传送门 BZOJ传送门 最大流神题 把点权转化为边权,切糕里每个点$(i,j,k)$向$(i,j,k+1)$连一条流量为$v(i,j,k)$的边 源点$S$向第$1$层的点连边,第$R+1$ ...

最新文章

  1. 从科幻灵感开始的一场数据存储基础设施实验之旅
  2. [你必须知道的.NET]第十一回:参数之惑---传递的艺术(上)
  3. char几个字节java_java中的char占几个字节实例分析
  4. 如何在PowerPoint2007制造课件免费ppt模板下载
  5. SAP Spartacus 读取payment detail数据的API
  6. sap.ca.ui.utils.busydialog - scenario1 - opportunity opened
  7. slf4j绑定器_用于ADFLogger的SLF4J绑定–缺少的部分
  8. 使用C#和MSMQ开发消息处理程序
  9. ORA-00257归档日志写满的解决方法 - xwdreamer - 博客园
  10. c语言p1-melepeo,C语言做个学生选课系统 -电脑资料
  11. 模拟 Coder-Strike 2014 - Round 1 A. Poster
  12. c#file过滤多种格式_[C#].NET/C#应用程序开发中使用Directory.GetFiles()过滤多种文件扩展名类型有哪些方法?...
  13. linux中备份mysql数据库命令_linux备份mysql数据库命令
  14. MTPA 永磁同步电机 计算
  15. java学生健康体检档案管理系统ssm313hf
  16. 运营周期爆发期是什么?
  17. SpringBoot html转pdf 支持中文、图片水印+文字水印、页眉页脚 flying-saucer-pdf-itext5 + freemarker
  18. c语言程序设计高速公路超速处罚,高速超速违章处理流程
  19. Android——猜数字小游戏
  20. 已知两个矩形的长和宽,用面向对象的概念编程求它们的面积和周长。假设矩形 1 的长和宽分别为 20 和 50;矩形 2 的长和宽分别为 3.6 和 4.5。(先定义矩形类再实例化两个对象)

热门文章

  1. wireshark抓包分析tcp连接与断开
  2. [转]系统吞吐量(TPS)、用户并发量、性能测试概念和公式---学习
  3. SqlHelper详解(转载)
  4. [基础题] 6.(*)按如下要求编写Java程序: (1)编写一个接口:OneToN,只含有一个方法int dispose(int n)
  5. 网络安全-windowserver搭建DHCP服务器
  6. [Python人工智能] 二十四.易学智能GPU搭建Keras环境实现LSTM恶意URL请求分类
  7. Python简单实现基于VSM的余弦相似度计算
  8. LeetCode Algorithm 606. 根据二叉树创建字符串
  9. django.core.exceptions.ImproperlyConfigured: SQLite 3.8.3 or later is required (found 3.7.17).
  10. [学习方法]如何解析源代码