POJ 3281 -- Dining(最大流,拆点建图)
題目鏈接
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 3Sample Output
3AC
建圖思路:
題目問的是最多有幾頭牛可以同時得到自己想要的食物和水,如果一頭牛可以同時可以得到兩對合適的食物和水,這樣就浪費了,所以要把牛拆開,牛和自己建一條權(quán)值為1的邊,這樣保證一頭牛如果可以的話只能得到一對食物和水源,從而使得越多的牛可以得到對應(yīng)的食物和水- 源點和所有食物建立一條權(quán)值為1的邊
- 食物和牛建立權(quán)值為1的邊
- 牛的副本和對應(yīng)的水建立權(quán)值為1的邊
- 水和匯點建立權(quán)值為1的邊
- 牛和牛的副本建立權(quán)值為1的邊
建好圖之后,跑一遍最大流,得到的就是最多牛的個數(shù)
總結(jié)
以上是生活随笔為你收集整理的POJ 3281 -- Dining(最大流,拆点建图)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU Problem - 6383 p
- 下一篇: POJ 3436 -- ACM Comp