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

POJ 3155 Hard Life(最大密度子图)

时间:2014-11-12 22:59:02      阅读:229      评论:0      收藏:0      [点我收藏+]

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

POJ 3155 Hard Life

题目链接

最大密度子图模板题

代码:

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

const int MAXNODE = 1105;
const int MAXEDGE = 100005;

typedef double Type;
const Type INF = 1e15;

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 double eps = 1e-8;

int n, m, u[N], v[N], s, t, du[N], sum;

double build(double g) {
	gao.init(n + 2);
	for (int i = 1; i <= n; i++) {
		gao.add_Edge(s, i, 1.0 * m);
		gao.add_Edge(i, t, m + 2 * g - du[i]);
	}
	for (int i = 0; i < m; i++) {
		gao.add_Edge(u[i], v[i], 1.0);
		gao.add_Edge(v[i], u[i], 1.0);
	}
	return n * m - gao.Maxflow(s, t);
}

void dfs(int u) {
	gao.vis[u] = true;
	if (u >= 1 && u <= n) sum++;
	for (int i = gao.first[u]; i + 1; i = gao.next[i]) {
		int v = gao.edges[i].v;
		if (gao.edges[i].flow < gao.edges[i].cap && !gao.vis[v]) dfs(v);
	}
}

int main() {
	while (~scanf("%d%d", &n, &m)) {
		if (m == 0) {
			printf("1\n1\n");
			continue;
		}
		s = 0, t = n + 1;
		memset(du, 0, sizeof(du));
		for (int i = 0; i < m; i++) {
			scanf("%d%d", &u[i], &v[i]);
			du[u[i]]++; du[v[i]]++;
		}
		double l = 0, r = m;
		while (r - l >= 1.0 / n / n) {
			double mid = (l + r) / 2;
			if (build(mid) < eps) r = mid;
			else l = mid;
		}
		build(l);
		gao.Maxflow(s, t);
		memset(gao.vis, false, sizeof(gao.vis));
		sum = 0;
		dfs(s);
		printf("%d\n", sum);
		for (int i = 1; i <= n; i++)
			if (gao.vis[i]) printf("%d\n", i);
	}
	return 0;
}


POJ 3155 Hard Life(最大密度子图)

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

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

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