P2891 [USACO07OPEN]吃饭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).

翻译

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

输入输出格式

输入格式:

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.

输出格式:

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

输入输出样例

输入样例#1:

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

输出样例#1:

3

算法

网络最大流。这里不详细讲,请大家先掌握。

思路

注意,以下出现的所有边边权皆为1,且其反向边边权为0

我们以牛、食物、饮料为点建图。

像这样:

S(=0)表示额外建的一个起始点,Ri(=i+N+N)表示第i个菜,Di(=i+N+N+F)表示第i种饮料,由于牛只有一头,而网络流处理只经过一个点不方便,我们采用一种神奇方法——拆点!也就是说,把一头牛看做两个点,要匹配这头牛必须经过这头牛两点之间的边,这样就可以控制这头牛只匹配一次。如图,Pi(=i)、Pi'(=i+N)表示第i头牛。

然后建边。如图,将S与所有Ri相连,将所有的Di与T相连,S作为源点,T作为汇点。如果Pi喜欢Rj,就将Pi与Rj相连。如果Pi喜欢Dj,就将Dj与Pi'之间相连。当然,Pi与Pi'之间也要连一条边。

然后就可以套网络最大流辣。最后得出的答案即为满足的牛数。

代码

#include<bits/stdc++.h>
using namespace std;
#define open(s) freopen( s".in", "r", stdin ), freopen( s".out", "w", stdout )
#define MAXN 405
#define MAXM 40005 int N, F, D;
int hd[MAXN], nxt[MAXM << 1], to[MAXM << 1], val[MAXM << 1], tot(1);
int pre[MAXN], e[MAXN], ans, dis[MAXN];
queue<int> Q; int x, y;
int S, T; void Add( int x, int y, int z ){ nxt[++tot] = hd[x]; hd[x] = tot; to[tot] = y; val[tot] = z; } bool BFS(){
while( !Q.empty() ) Q.pop();
memset( dis, 0, sizeof dis );
Q.push(S); dis[S] = 1;
while( !Q.empty() ){
x = Q.front(); Q.pop();
for ( int i = hd[x]; i; i = nxt[i] )
if ( val[i] && !dis[to[i]] ){
dis[to[i]] = dis[x] + 1;
Q.push( to[i] );
if ( to[i] == T ) return 1;
}
}
return 0;
} int DFS( int x, int fl ){
if ( x == T ) return fl;
int res(fl), k;
for ( int i = hd[x]; i && res; i = nxt[i] ){
if ( val[i] && dis[to[i]] == dis[x] + 1 ){
k = DFS( to[i], min( res, val[i] ) );
if ( !k ) dis[to[i]] = 0;
val[i] -= k; val[i^1] += k; res -= k;
}
}
return fl - res;
} int main(){
scanf( "%d%d%d", &N, &F, &D );
S = 0; T = N + N + F + D + 1;
for ( int i = 1; i <= N; ++i ) Add( i, i + N, 1 ), Add( i + N, i, 0 );
for ( int i = 1; i <= F; ++i ) Add( S, i + N + N, 1 ), Add( i + N + N, S, 0 );
for ( int i = 1; i <= D; ++i ) Add( i + N + N + F, T, 1 ), Add( T, i + N + N + F, 0 );
for ( int i = 1; i <= N; ++i ){
int f, d, x; scanf( "%d%d", &f, &d );
for ( int j = 1; j <= f; ++j ) scanf( "%d", &x ), Add( x + N + N, i, 1 ), Add( i, x + N + N, 0 );
for ( int j = 1; j <= d; ++j ) scanf( "%d", &x ), Add( i + N, x + N + N + F, 1 ), Add( x + N + N + F, i + N, 0 );
}
int t;
while( BFS() )
while( ( t = DFS( S, 0x7f7f7f7f ) ) > 0 ) ans += t;
printf( "%d\n", ans );
return 0;
}

「洛谷P2891」[USACO07OPEN]吃饭Dining 解题报告的更多相关文章

  1. 「洛谷P1231」教辅的组成 解题报告

    P1231 教辅的组成 题目背景 滚粗了的HansBug在收拾旧语文书,然而他发现了什么奇妙的东西. 题目描述 蒟蒻HansBug在一本语文书里面发现了一本答案,然而他却明明记得这书应该还包含一份练习 ...

  2. 「区间DP」「洛谷P1043」数字游戏

    「洛谷P1043」数字游戏 日后再写 代码 /*#!/bin/sh dir=$GEDIT_CURRENT_DOCUMENT_DIR name=$GEDIT_CURRENT_DOCUMENT_NAME ...

  3. 「洛谷4197」「BZOJ3545」peak【线段树合并】

    题目链接 [洛谷] [BZOJ]没有权限号嘤嘤嘤.题号:3545 题解 窝不会克鲁斯卡尔重构树怎么办??? 可以离线乱搞. 我们将所有的操作全都存下来. 为了解决小于等于\(x\)的操作,那么我们按照 ...

  4. 「洛谷3338」「ZJOI2014」力【FFT】

    题目链接 [BZOJ] [洛谷] 题解 首先我们需要对这个式子进行化简,否则对着这么大一坨东西只能暴力... \[F_i=\sum_{j<i} \frac{q_iq_j}{(i-j)^2}-\s ...

  5. 「BZOJ2733」「洛谷3224」「HNOI2012」永无乡【线段树合并】

    题目链接 [洛谷] 题解 很明显是要用线段树合并的. 对于当前的每一个连通块都建立一个权值线段树. 权值线段树处理操作中的\(k\)大的问题. 如果需要合并,那么就线段树暴力合并,时间复杂度是\(nl ...

  6. 「洛谷3870」「TJOI2009」开关【线段树】

    题目链接 [洛谷] 题解 来做一下水题来掩饰ZJOI2019考炸的心情QwQ. 很明显可以线段树. 维护两个值,\(Lazy\)懒标记表示当前区间是否需要翻转,\(s\)表示区间还有多少灯是亮着的. ...

  7. 「洛谷5300」「GXOI/GZOI2019」与或和【单调栈+二进制转化】

    题目链接 [洛谷传送门] 题解 按位处理. 把每一位对应的图都处理出来 然后单调栈处理一下就好了. \(and\)操作处理全\(1\). \(or\)操作处理全\(0\). 代码 #include & ...

  8. 「洛谷3469」「POI2008」BLO-Blockade【Tarjan求割点】

    题目链接 [洛谷传送门] 题解 很显然,当这个点不是割点的时候,答案是\(2*(n-1)\) 如果这个点是割点,那么答案就是两两被分开的联通分量之间求组合数. 代码 #include <bits ...

  9. 「洛谷1884」「USACO12FEB」过度种植【离散化扫描线】

    题目链接 [洛谷传送门] 题解 矩阵面积的并模板.(请求洛谷加为模板题) 很明显是要离散化的. 我们将矩阵与\(x\)轴平行的两个线段取出来.并且将这两个端点的\(x1\)和\(x2\)进行离散化. ...

随机推荐

  1. 使用DataX同步MaxCompute数据到TableStore(原OTS)优化指南

    概述 现在越来越多的技术架构下会组合使用MaxCompute和TableStore,用MaxCompute作大数据分析,计算的结果会导出到TableStore提供在线访问.MaxCompute提供海量 ...

  2. protobuf_1

    我使用的是最新版本的protobuf(protobuf-2.6.1),编程工具使用VS2010.简单介绍下google protobuf: google protobuf 主要用于通讯,是google ...

  3. 从 Apache ORC 到 Apache Calcite | 2019大数据技术公开课第一季《技术人生专访》

    摘要: 什么是Apache ORC开源项目?主流的开源列存格式ORC和Parquet有何区别?MaxCompute为什么选择ORC? 如何一步步成为committer和加入PMC的?在阿里和Uber总 ...

  4. 【Activiti工作流引擎】官方快速入门demo

    Activiti官方快速入门demo 地址: https://www.activiti.org/quick-start 0. 版本 activiti 5.22.0 JDK 1.8 1. 介绍 这个快速 ...

  5. 【CSS3 + 原生JS】上升的方块动态背景

    GIF图有点大,网速慢的或将稍等片刻或可浏览本人的制作的demo. Demo : 点击查看 HTML: <!DOCTYPE html> <html lang="en&quo ...

  6. 【知识小结】PHP使用svn笔记总结

    在公司里,我们要养成每天上班前更新代码,下班前提交代码的习惯,并且做好说明. svn更新代码的时候,先右键点击需要更新的项目,在team中进入资源库同步界面,选择incoming mode,显示的文件 ...

  7. Roslyn 静态分析

    本文告诉大家如何使用 Roslyn 分析代码 首先创建一个项目,项目使用.net Framework 4.6.2 ,控制台项目.然后需要安装一些需要的库 Nuget 安装 打开 Nuget 安装下面两 ...

  8. 从零开始学习Kafka

    简介 kafka是一个分布式消息队列.具有高性能.持久化.多副本备份.横向扩展能力.生产者往队列里写消息,消费者从队列里取消息进行业务逻辑.一般在架构设计中起到解耦.削峰.异步处理的作用. Kafka ...

  9. thinkPHP框架中执行原生SQL语句的方法

    这篇文章主要介绍了thinkPHP框架中执行原生SQL语句的方法,结合实例形式分析了thinkPHP中执行原生SQL语句的相关操作技巧,并简单分析了query与execute方法的使用区别,需要的朋友 ...

  10. P1073 奇数还是偶数

    题目描述 给你 \(N(1 \le N \le 1000)\) 个数,你需要判断每个数是奇数还是偶数. 输入格式 输入的第一行包含一个整数 \(N(1 \le N \le 1000)\) . 接下来 ...