码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3189 Steady Cow Assignment(最大流)

时间:2014-11-07 23:31:18      阅读:412      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   os   sp   for   

POJ 3189 Steady Cow Assignment

题目链接

题意:一些牛,每个牛心目中都有一个牛棚排名,然后给定每个牛棚容量,要求分配这些牛给牛棚,使得所有牛对牛棚的排名差距尽量小

思路:这种题的标准解法都是二分一个差值,枚举下界确定上界,然后建图判断,这题就利用最大流进行判断,值得一提的是dinic的效率加了减枝还是是卡着时间过的,这题理论上用sap或者二分图多重匹配会更好

代码:

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

const int MAXNODE = 1025;
const int MAXEDGE = 200005;

typedef int Type;
const Type INF = 0x3f3f3f3f;

struct Edge {
	int u, v;
	Type cap, flow;
	Edge() {}
	Edge(int u, int v, Type cap, Type flow) {
		this->u = u;
		this->v = v;
		this->cap = cap;
		this->flow = flow;
	}
};

struct Dinic {
	int n, m, s, t;
	Edge edges[MAXEDGE];
	int first[MAXNODE];
	int next[MAXEDGE];
	bool vis[MAXNODE];
	Type d[MAXNODE];
	int cur[MAXNODE];
	vector<int> cut;

	void init(int n) {
		this->n = n;
		memset(first, -1, sizeof(first));
		m = 0;
	}
	void add_Edge(int u, int v, Type cap) {
		edges[m] = Edge(u, v, cap, 0);
		next[m] = first[u];
		first[u] = m++;
		edges[m] = Edge(v, u, 0, 0);
		next[m] = first[v];
		first[v] = m++;
	}

	bool bfs() {
		memset(vis, false, sizeof(vis));
		queue<int> Q;
		Q.push(s);
		d[s] = 0;
		vis[s] = true;
		while (!Q.empty()) {
			int u = Q.front(); Q.pop();
			for (int i = first[u]; i != -1; i = next[i]) {
				Edge& e = edges[i];
				if (!vis[e.v] && e.cap > e.flow) {
					vis[e.v] = true;
					d[e.v] = d[u] + 1;
					Q.push(e.v);
				}
			}
		}
		return vis[t];
	}

	Type dfs(int u, Type a) {
		if (u == t || a == 0) return a;
		Type flow = 0, f;
		for (int &i = cur[u]; i != -1; i = next[i]) {
			Edge& e = edges[i];
			if (d[u] + 1 == d[e.v] && (f = dfs(e.v, min(a, e.cap - e.flow))) > 0) {
				e.flow += f;
				edges[i^1].flow -= f;
				flow += f;
				a -= f;
				if (a == 0) break;
			}
		}
		return flow;
	}

	Type Maxflow(int s, int t) {
		this->s = s; this->t = t;
		Type flow = 0;
		while (bfs()) {
			for (int i = 0; i < n; i++)
				cur[i] = first[i];
			flow += dfs(s, INF);
		}
		return flow;
	}

	void MinCut() {
		cut.clear();
		for (int i = 0; i < m; i += 2) {
			if (vis[edges[i].u] && !vis[edges[i].v])
				cut.push_back(i);
		}
	}
} gao;

const int N = 1005;
const int M = 25;

int n, b, g[N][M], s[M];

bool build(int x, int y) {
	gao.init(n + b + 2);
	for (int i = 1; i <= n; i++) {
		gao.add_Edge(0, i, 1);
		for (int j = x; j <= y; j++)
			gao.add_Edge(i, g[i][j] + n, 1);
	}
	for (int i = 1; i <= b; i++)
		gao.add_Edge(i + n, n + b + 1, s[i]);
	return gao.Maxflow(0, n + b + 1) == n;
}

int main() {
	while (~scanf("%d%d", &n, &b)) {
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= b; j++)
				scanf("%d", &g[i][j]);
		for (int i = 1; i <= b; i++)
			scanf("%d", &s[i]);
		int ans = 100;
		for (int i = 1; i <= b; i++) {
			for (int j = b; j >= i; j--) {
				if (ans <= j - i + 1) continue;
				if (build(i, j)) ans = min(ans, j - i + 1);
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}


POJ 3189 Steady Cow Assignment(最大流)

标签:style   blog   http   io   color   ar   os   sp   for   

原文地址:http://blog.csdn.net/accelerator_/article/details/40903157

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!