#POJ 3189 Steady Cow Assignment (二分枚举 + 最大流)

Steady Cow Assignment

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7655 Accepted: 2649

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

题目大意 : 有N头牛, M个谷仓, 每头牛对M个谷仓有各自的喜欢顺序, 每个谷仓有一定的容量, 让你输出一个值, 表示当所有牛进入的谷仓的喜欢程度的上下限的最小值, 并且满足所有牛都有谷仓可进

思路 : 想题意想了一个小时, 明白以后一发A了,之前做过一个类似的枚举上下限的题, 不过是最短路里的。首先有M个谷仓, 那么答案范围就在1 到 M之间, 直接二分答案的区间长度, 然后枚举出对应长度的所有情况, 每一次都跑最大流判断是否满流, 如果是就r = mid - 1, 否则 l = mid + 1。然后这样建边, 源点为0, 牛为1 到 N, 谷仓为 N + 1到 N + M, 汇点为 N+ M + 1, 源点到牛的流量为1, 表示一头牛只能进一个谷仓, 牛到谷仓的流量为1, 谷仓到汇点的流量为各自的容量, 注意下数据的初始化

Accepted code

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & (-x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }

struct Edge
{
	int v, w, next;
}e[MAXN << 1];
int head[MAXN], lev[MAXN][25], n, m, cnt, T, X;
int dep[MAXN], val[MAXN], cur[MAXN], sp, tp;
bool vis[MAXN];
void init() {
	MEM(head, -1); MEM(vis, 0);
	cnt = 0;
}
void add(int from, int to, int w) {
	e[cnt].v = to; e[cnt].w = w;
	e[cnt].next = head[from]; head[from] = cnt++;

	e[cnt].v = from; e[cnt].w = 0;
	e[cnt].next = head[to]; head[to] = cnt++;
}
bool bfs() {
	queue <int> q; MEM(dep, 0);
	dep[sp] = 1, q.push(sp);
	while (!q.empty()) {
		int now = q.front();
		q.pop();
		for (int i = head[now]; i != -1; i = e[i].next) {
			int vi = e[i].v;
			if (!dep[vi] && e[i].w) {
				dep[vi] = dep[now] + 1;
				q.push(vi);
			}
		}
	}
	if (dep[tp]) return true;
	else return false;
}
int dfs(int x, int dist) {
	if (x == tp || dist == 0) return dist;
	int res = 0, r;
	for (int &i = cur[x]; i != -1; i = e[i].next) {
		int vi = e[i].v;
		if (dep[vi] == dep[x] + 1 && e[i].w) {
			r = dfs(vi, min(e[i].w, dist - res));
			if (r > 0) {
				e[i].w -= r;
				e[i ^ 1].w += r;
				res += r;
				if (res == dist) return dist;
			}
		}
	}
	if (!res) dep[x] = 0;
	return res;
}
int dinic() {
	int flow = 0, ans;
	while (bfs()) {
		for (int i = sp; i <= tp; i++) cur[i] = head[i];
		flow += dfs(sp, INF);
	}
	return flow;
}
bool check(int x) {
	for (int i = 1; i <= m - x + 1; i++) {
		init();
		for (int j = 1; j <= n; j++) add(sp, j, 1);   //源点到牛
		for (int j = 1; j <= m; j++) add(n + j, tp, val[j]); // 谷仓到汇点
		for (int j = 1; j <= n; j++) {
			for (int k = i; k <= i + x - 1; k++)
				add(j, lev[j][k] + n, 1);  // 牛到谷仓
		}
		int scnt = dinic();
		if (scnt == n) return true;    // 满流返回真
	}
	return false;
}

int main()
{
	cin >> n >> m; sp = 0, tp = n + m + 1;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) sc("%d", &lev[i][j]); // 存矩阵
	}
	for (int i = 1; i <= m; i++) sc("%d", &val[i]); // 容量
	int l = 1, r = m, mid;
	int ans = INF;
	while (l <= r) {
		mid = (l + r) >> 1;  // 区间长度即答案
		if (check(mid)) Min(ans, mid), r = mid - 1;  // 满足就记录下来
		else l = mid + 1; 
	}
	cout << ans << endl;
	return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值