Steady Cow AssignmentTime
limit:MS
Memory Limit:65536KB
64bit IO Format:%i64d &%i64u SubmitStatusPracticePOJ 3189
Description
Farmer John ' s n (1 <= n <=) cows each reside in one of B (1 <= B <=) barns which, of course, with Limi Ted capacity. Some cows really like their current barn, and Some is not so happy.
FJ would like to rearrange the cows such that the cows is as equally happy as possible, even if that means all the cows H Ate their assigned barn.
Each cow gives FJ the order in which she prefers the barns. A cow ' s happiness with a particular assignment was her ranking of her barn. Your job is to find a assignment of cows to barns such this 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 is 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 the 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 being 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 ENDP Oints.
Sample Input
6 41 2 3 42 3 1 44 2 3 13 1 2 41 3 4 21 4 2 32 1 3 2
Sample Output
2
Hint
Explanation of the sample:
Each cow can is assigned to her first or second Choice:barn 1 gets cows 1 and 5, barn 2 gets cow 2, barn 3 gets cow 4, an D Barn 4 gets cows 3 and 6.
Netizen's good, imitate a bit ... , in fact, the network flow is similar
Title Description:
There are n cows, m sheds, and each cow has a degree of affection for each shed. Of course, the shed is also a temper, not the cows want to live in and live in, beyond the maximum capacity of the shed, the shed is rejected. Now to make a home for each cow, this baby is a fair and impartial human, so to find a cow love degree of the smallest scheme (although this may be generally low, because the absolute fairness does not mean reasonable AH), ask the extent of the degree of love to the minimum is how big?
Problem Solving Ideas:
Each shed does not live on a cow, so it is a multiple match. Match the time of the two-point enumeration of the degree of affection of the interval size, according to the interval size to enumerate the beginning and end of the interval, and then run multiple matches to determine whether it is legal. Note that the interval size is calculated. And, uh, when the data is read in, Maps[i][j] is not the i_th cow's favorite value for j_th shed, but the i_th cow's love for Maps[i][j] Sheds is J.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace std;const int maxn = 1010;int maps[maxn][22], vis[22], Used[22][maxn];int link[22], limit[22], N, M, S, E, Mid;bool Find ( int u) {for (int i=1; i<=m; i++) {//multi-match if (!vis[i] && maps[u][i]<e && s<=maps[u][i ]) {Vis[i] = 1; if (Link[i] < Limit[i]) {//i shed is not full used[i][link[i] + +] = u; return true; } for (int j=0; j<limit[i]; j + +)//i The shed is full, looking for the augmented path if (find (Used[i][j])) { USED[I][J] = u; return true; }}} return false;} BOOL Hungry () {for (S=1; s<=m+1-mid; s++) {//enum interval start with end point int ans = 0; E = s + mid; memset (link, 0, sizeof); for (int i=1; i<=n; i++) {memset (Vis, 0, sizeof (VIS)); IF (Find (i)) ans + +; } if (ans = = n) return true; } return false;} int main () {while (scanf ("%d%d", &n, &m)! = EOF) {for (Int. I=1; i<=n; i++) for (int J=1; j<=m; J + +) {scanf ("%d", &mid); Maps[i][mid] = j; } for (int i=1; i<=m; i++) scanf ("%d", &limit[i]); int left = 1, right = m, ans = 0; while (left<=right) {//two-min enumeration interval mid = (right+left)/2; if (hungry ()) {ans = mid; right = Mid-1; } else left = mid + 1; } printf ("%d\n", ans); } return 0;}
Steady Cow Assignment (binary graph multiple match + dichotomy) (network flow)