pku 3189 網絡流|二分圖多重匹配+枚舉區間

 
                                              Steady Cow Assignment
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3497 Accepted: 1199

Description

Farmer John's N (1 <= N <= 1000) cows each reside in one of B (1 <= B <= 20) barns which, of course, have limited capacity. Some cows really like their current barn, and some are not so happy.

FJ would like to rearrange the cows such that the cows are as equally happy as possible, even if that means all the cows hate their assigned barn.

Each cow gives FJ the order in which she prefers the barns. A cow's happiness with a particular assignment is her ranking of her barn. Your job is to find an assignment of cows to barns such that no barn's capacity is exceeded and the size of the range (i.e., one more than the positive difference between the the highest-ranked barn chosen and that lowest-ranked barn chosen) of barn rankings the cows give their assigned barns is as small as possible.

Input

Line 1: Two space-separated integers, N and B

Lines 2..N+1: Each line contains B space-separated integers which are exactly 1..B sorted into some order. The first integer on line i+1 is the number of the cow i's top-choice barn, the second integer on that line is the number of the i'th cow's second-choice barn, and so on.

Line N+2: B space-separated integers, respectively the capacity of the first barn, then the capacity of the second, and so on. The sum of these numbers is guaranteed to be at least N.

Output

Line 1: One integer, the size of the minumum range of barn rankings the cows give their assigned barns, including the endpoints.

Sample Input

6 4
1 2 3 4
2 3 1 4
4 2 3 1
3 1 2 4
1 3 4 2
1 4 2 3
2 1 3 2

Sample Output

2

Hint

Explanation of the sample:

Each cow can be assigned to her first or second choice: barn 1 gets cows 1 and 5, barn 2 gets cow 2, barn 3 gets cow 4, and barn 4 gets cows 3 and 6.
 
題目意思不好描述,關鍵是紅色部分的那句話,大概意思安排牛與牛棚的一對多關係,使得每隻牛對他入住的牛棚的喜歡程度的最大值與最小值差最小。
枚舉牛與牛棚喜歡程度的區間 [ left , right ] ,開始的時候 left = right =1. 如果說
[ left ,right ]滿足條件,則 left++,繼續尋找最優解,否則 right++,繼續判定。
那麼如何是否滿足條件呢?
兩種方法:
(1):網絡流,根據區間 [ left ,right ] 建圖,如果滿流,滿足。否則,不滿足;
(2):二分圖的多重匹配,在二分圖的最大匹配中是一對一的關係,而這裏是一對多的關係,可以借鑑最大匹配中find增廣路的過程,只不過這裏判斷是否找到增廣路不同,這裏判斷爲if(sum[v]<cap[v]||search(y[i]) (i,v)是匹配邊) ,sum[v]是v這個點的已經匹配的點數。二分圖多重匹配
發佈了60 篇原創文章 · 獲贊 4 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章