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
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
这个是个最大流问题,求最多能能喂养多少头牛,每个牛有食物和水。
构建流网络图:
如图所示,分别是 S ,F(1—f),N(1-f),D(1-D),T;
我们把途中 食物-> 牛-> 水 称为一个食物链,根据题意是一条食物链就可以养一头牛,求最多可以养活多少头牛,但是这个流网络存在问题,就是可能一头牛在两条食物链上。
所以牛这个结点需要分解成两个结点,容量为1.
当流网络建好了,剩下的就是基本的最大流问题,反复加入路径,求残留网络是否存在增广路径,一直到不存在路径为止。