最大流模版 dinic

朴素dinic+多路增广

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXM=100005,MAXN=10005;
int init(){
	int rv=0,fh=1;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-') fh=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		rv=(rv<<1)+(rv<<3)+c-'0';
		c=getchar();
	}
	return rv*fh;
}
queue <int> q;
int n,nume,m,s,t,head[MAXN],dep[MAXN];
struct edge{
	int to,nxt,cap,flow;
}e[MAXM<<1];
void adde(int from,int to,int cap){
	e[++nume].to=to;
	e[nume].nxt=head[from];
	head[from]=nume;
	e[nume].cap=cap;
}
bool bfs(){
	while(!q.empty()) q.pop();
	memset(dep,0,sizeof(dep));
	dep[s]=1;q.push(s);
	while(!q.empty()){
		int u=q.front();q.pop();
		for(int i=head[u];i;i=e[i].nxt){
			int v=e[i].to;
			if(!dep[v]&&e[i].flow<e[i].cap){
				dep[v]=dep[u]+1;
				q.push(v);
			}
		}
	}
	if(dep[t]) return 1;
	else return 0;
}
int dfs(int u,int flow){
	if(u==t) return flow;
	int tot=0;
	for(int i=head[u];i&&tot<flow;i=e[i].nxt){
		int v=e[i].to;
		if((dep[v]==dep[u]+1)&&(e[i].flow<e[i].cap)){
			if(int t=dfs(v,min(flow-tot,e[i].cap-e[i].flow))){
				e[i].flow+=t;
				e[((i-1)^1)+1].flow-=t;
				tot+=t;  //不return ,多路增广
			}
		}
	}
	return tot;
}
int main(){
	n=init();m=init();s=init();t=init();
	for(int i=1;i<=m;i++){
		int u=init(),v=init(),cap=init();
		adde(u,v,cap);
		adde(v,u,0);
	}
	int ans=0;
	while(bfs()){
	    ans+=dfs(s,0x3f3f3f3f);     
	}
	cout<<ans<<endl;
}

当前弧优化+多路增广

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
const int MAXN = 10005;
int init() {
	int rv = 0, fh = 1;
	char c = getchar();
	while(c < '0' || c > '9') {
		if(c == '-') fh = -1;
		c = getchar();
	}
	while(c >= '0' && c <= '9') {
		rv = (rv<<1) + (rv<<3) + c - '0';
		c = getchar();
	}
	return fh * rv;
}
int head[MAXN], n, m, ss, tt, dep[MAXN], cur[MAXN], maxflow, nume;
struct edge{
	int to, nxt, cap, flow;
}e[MAXN * 20];
void adde(int from, int to, int cap) {
	e[++nume].to = to;
	e[nume].cap = cap;
	e[nume].nxt = head[from];
	head[from] = nume;
}
queue<int> q;
bool bfs() {
	memset(dep, 0, sizeof(dep));
	q.push(ss); dep[ss] = 1;
	while(!q.empty()) {
		int u = q.front(); q.pop();
		for(int i = head[u]; i; i = e[i].nxt) {
			int v = e[i].to;
			if(!dep[v] && e[i].flow < e[i].cap) {
				dep[v] = dep[u] + 1;
				q.push(v);
			}
		}
	}
	if(dep[tt]) return 1;
	else return 0;
}
int dfs(int u, int flow) {
	if(u == tt) return flow;
	int tot = 0;
	for(int &i = cur[u]; i && tot < flow; i = e[i].nxt) {
		int v = e[i].to;
		if((dep[v] == dep[u] + 1) && (e[i].flow < e[i].cap)) {
			if(int t = dfs(v, min(flow - tot, e[i].cap - e[i].flow))) {
				e[i].flow += t;
				e[((i - 1) ^ 1) + 1].flow -= t;
				tot += t;
			}
		}
	}
	return tot;
}
void dinic() {
	while(bfs()) {
		for(int i = 1; i <= n; i++) cur[i] = head[i];
		maxflow += dfs(ss, inf);
	}
}
int main() {
	n = init(); m = init(); ss = init(); tt = init();
	for(int i = 1; i <= m; i++) {
		int u = init(), v = init(), cap = init();
		adde(u, v, cap); adde(v, u, 0);
	}
	dinic();
	cout << maxflow << endl;
	return 0;
}
posted @ 2018-01-18 16:35  Mr_Wolfram  阅读(146)  评论(0编辑  收藏  举报