POJ - 3189 Steady Cow Assignment(二分+二分图多重匹配)
Steady Cow Assignment
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7289 | Accepted: 2506 |
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.
Source
大体意思就是牛对牛舍有一个评级,每个牛舍有最大的容纳量,问将这些牛安排进宿舍后,最小的(最大的牛满意度-最小的牛满意度+1)的值是多少。
这道题的话,先是要通过二分设这个值的大小是多少,然后遍历所有符合这个差值的插入空间。(就是把所有可能的牛满意度的区间遍历并及检验是否有一个区间成功);只要有一个区间成功,那么这个值就表示成功了;
#include<vector>
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n, m;
bool vis[25];
int sum[25];
vector<int>use[25];
int G[1005][25];
bool find(int x,int st,int en) {
for (int s = st; s <= en; s++) {
int v = G[x][s];
if (!vis[v]) {
vis[v] = 1;
if (use[v].size() < sum[v]) {
use[v].push_back(x);
return 1;
}
for (int s = 0; s < use[v].size(); s++)
if (find(use[v][s], st, en)){
use[v][s] = x; return 1;
}
}
}
return 0;
}
bool check(int x) {
for (int s = m; s >= x; s--) {//枚举终点;
for (int w = 0; w <= m; w++)use[w].clear();
int spot = 1;
for (int w = 1; w <= n; w++) {
memset(vis, 0, sizeof(vis));
if (!find(w, s - x + 1, s)) {
spot = 0; break;
}
}
if (spot) {
/* cout << s - x + 1 << " " << s << " " << x <<" "<<m<< endl;
for (int w = 1; w <= m; w++) {
cout << w << " : ";
for (int q = 0; q < use[w].size(); q++)
cout << use[w][q] << " ";
cout << endl;
}*/
return 1;
}
}
return 0;
}
int main()
{
while (~scanf("%d%d", &n, &m)) {
for (int s = 1; s <= n; s++)
for (int w = 1; w <= m; w++)
scanf("%d", &G[s][w]);
for (int s = 1; s <= m; s++)
scanf("%d", &sum[s]);
int li = 0, ri = m;
while (li < ri) {
int mid = (ri + li) >> 1;
if (check(mid))ri = mid;
else li = mid + 1;
}
cout << ri << endl;
}
}