zoukankan      html  css  js  c++  java
  • 最大流模版 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;
    }
    
  • 相关阅读:
    「AHOI2018 初中组」根式化简(分解质因数+推性质)
    「TJOI2018」智力竞赛(二分+DAG最小可相交路径覆盖)
    「LibreOJ NOI Round #1」动态几何问题(mobius反演+分块)
    「LibreOJ NOI Round #1」北校门外的回忆(找性质+倍增+线段树)
    USACO 2020 Open Contest, Platinum(exercise)(min-max容斥+计数dp)
    「LibreOJ β Round」ZQC 的截图(随机+hash)
    BM算法模板
    「THUSCH 2017」如果奇迹有颜色(burnside引理+状压dp+打表+BM+常系数齐次线性递推)
    「THUSCH 2017」换桌(zkw费用流)—对几种费用流算法的总结
    iOS学习笔记42-Swift(二)函数和闭包
  • 原文地址:https://www.cnblogs.com/Mr-WolframsMgcBox/p/8310909.html
Copyright © 2011-2022 走看看