HihoCoder #1369 : 网络流一·Ford-Fulkerson算法

1.题面

http://hihocoder.com/problemset/problem/1369

2.题意

给你一张图,求从1到n的最大流

3.思路

使用Dinic算法

4.代码

/*****************************************************************
    > File Name: cpp_acm.cpp
    > Author: Uncle_Sugar
    > Mail: [email protected]
    > Created Time: Sun 02 Oct 2016 13:36:50 CST
*****************************************************************/
# include <cstdio>
# include <cstring>
# include <cctype>
# include <cmath>
# include <cstdlib>
# include <climits>
# include <iostream>
# include <iomanip>
# include <set>
# include <map>
# include <vector>
# include <stack>
# include <queue>
# include <algorithm>
using namespace std;

# define rep(i,a,b) for (i=a;i<=b;i++)
# define rrep(i,a,b) for (i=b;i>=a;i--)
# define mset(aim, val) memset((aim), (val), sizeof(aim))


template<class T>void PrintArray(T* first,T* last,char delim=' '){
    for (;first!=last;first++) cout << *first << (first+1==last?'\n':delim);
}

/*
1.see the size of the input data before you select your algorithm 
2.cin&cout is not recommended in ACM/ICPC
3.pay attention to the size you defined, for instance the size of edge is double the size of vertex
*/

const int debug = 1;
//# const int size  = 10 + ; 
const int INF = INT_MAX>>1;
typedef long long ll;

const int MAXN = 100 + 100000;
const int MAXM = 100 + 200000;


struct Edge{
	int to, nxt, f;
};

struct Dinic_MaxFlow{
	int head[MAXN], tot;
	Edge edge[MAXM];
	int level[MAXN];

	void init(){
		tot = 0; mset(head, -1);
	}

	void addedge(int from, int to, int f){
		edge[tot].to = to; edge[tot].f = f;
		edge[tot].nxt = head[from]; head[from] = tot++;

		edge[tot].to = from; edge[tot].f = 0;
		edge[tot].nxt = head[to];   head[to] = tot++;
	}
	
	bool bfs(int s, int t){
		static queue<int> que;
		while (!que.empty()) que.pop();
		mset(level, 0);
		level[s] = 1;
		que.push(s);
		while (!que.empty()){
			int c = que.front();que.pop();
			if (c == t) return true;
			for (int e = head[c]; ~e; e = edge[e].nxt){
				int to = edge[e].to, f = edge[e].f;
				//# cout << "to = " << to << endl;
				if (!level[to] && f){
					level[to] = level[c] + 1;
					que.push(to);
				}
			}
		}
		return false;
	}

	int dfs(int u, int t, int sup){
		if (u == t) return sup;
		int ret = 0;
		for (int e = head[u]; ~e; e = edge[e].nxt){
			int to = edge[e].to, f = edge[e].f;
			//# cout << "to = " << to << endl;
			if (level[to] == level[u] + 1 && f){
				int mi = min(sup - ret, f);
				int tf = dfs(to, t, mi);
				edge[e].f -= tf;
				edge[e^1].f += tf;
				ret += tf;
				if (ret == sup)	return ret;
			}
		}
		return ret;
	}

	int Dinic(int s, int t){
		int ret = 0;
		while (bfs(s, t)) ret += dfs(s, t, INF);
		return ret;
	}
}dinic;


int main(){
	/*std::ios::sync_with_stdio(false);cin.tie(0);*/
	int n, m;
	while (~scanf("%d%d", &n, &m)){
		dinic.init();
		for (int i = 0; i < m; i++){
			int a, b, c;
			scanf("%d%d%d", &a, &b, &c);
			dinic.addedge(a, b, c);
			//# dinic.addedge(b, a, c);
		}
		printf("%d\n", dinic.Dinic(1, n));
		//# cout << dinic.Dinic(1, n) << endl;
		
		
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/sinat_29278271/article/details/52723687