首页 > 代码库 > POJ 3204 Ikki's Story I - Road Reconstruction(最大流)

POJ 3204 Ikki's Story I - Road Reconstruction(最大流)

POJ 3204 Ikki‘s Story I - Road Reconstruction

题目链接

题意:给定一个有向图,求出最大流后,问哪些边增加容量后,可以使最大流增加

思路:对于一个可以增加的,必然原来就是满流,并且从源点到汇点,的一条路径上,都是还有残留容量的,这样只要从源点和汇点分别出发dfs一遍,标记掉经过点,然后枚举满流边,如果两端都是标记过的点,这个边就是可以增加的

代码:

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

const int MAXNODE = 505;
const int MAXEDGE = 100005;

typedef int Type;
const Type INF = 0x3f3f3f3f;

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

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);
		}
	}
	
	bool mark[MAXNODE][2];

	void find(int u, int tp) {
		mark[u][tp] = true;
		for (int i = first[u]; i + 1; i = next[i]) {
			int v = edges[i].v;
			if (mark[v][tp]) continue;
			if (tp == 0 && i % 2) continue;
			if (tp && i % 2 == 0) continue;
			if (edges[i^tp].cap == edges[i^tp].flow) continue;
			find(v, tp);
		}
	}
	int solve() {
		Maxflow(0, n - 1);
		memset(mark, false, sizeof(mark));
		find(0, 0);
		find(n - 1, 1);
		int ans = 0;
		for (int i = 0; i < m; i += 2)
			if (edges[i].cap == edges[i].flow && mark[edges[i].u][0] && mark[edges[i].v][1]) ans++;
		return ans;
	}
} gao;

int n, m;

int main() {
	while (~scanf("%d%d", &n, &m)) {
		gao.init(n);
		int u, v, w;
		while (m--) {
			scanf("%d%d%d", &u, &v, &w);
			gao.add_Edge(u, v, w);
		}
		printf("%d\n", gao.solve());
	}
	return 0;
}


POJ 3204 Ikki's Story I - Road Reconstruction(最大流)