首页 > 其他 > 详细

POJ 3469 Dual Core CPU(最小割)

时间:2014-11-06 23:37:05      阅读:562      评论:0      收藏:0      [点我收藏+]

POJ 3469 Dual Core CPU

题目链接

题意:有a,b两台机器,有n个任务,在a机器和b机器需要不同时间,给定m个限制,如果u, v在不同机器需要额外开销,问最小开销

思路:最小割,源点为a机器,汇点为b机器,这样的话求出最小割,就是把点分成两个集合的最小代价,然后如果u, v在不同机器需要开销,则连u,v v,u两条边,容量为额外开销,这样如果这条边是割边,则a,b会在不同集合,并且代价就会被加上去

代码:

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

const int MAXNODE = 20005;
const int MAXEDGE = 1000005;

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;

int n, m;

int main() {
	while (~scanf("%d%d", &n, &m)) {
		gao.init(n + 2);
		int a, b, w;
		for (int i = 1; i <= n; i++) {
			scanf("%d%d", &a, &b);
			gao.add_Edge(0, i, a);
			gao.add_Edge(i, n + 1, b);
		}
		while (m--) {
			scanf("%d%d%d", &a, &b, &w);
			gao.add_Edge(a, b, w);
			gao.add_Edge(b, a, w);
		}
		printf("%d\n", gao.Maxflow(0, n + 1));
	}
	return 0;
}


POJ 3469 Dual Core CPU(最小割)

原文:http://blog.csdn.net/accelerator_/article/details/40869939

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!