程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ 3155 Hard Life(最大密度子圖)

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

編輯:C++入門知識

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


POJ 3155 Hard Life

題目鏈接

最大密度子圖模板題

代碼:

#include 
#include 
#include 
#include 
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 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 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;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved