模板——Dinic

震惊,照着SYCstudio的教程打出来的Dinic竟被呆滞怒斥是EK,惊了,打开教程的方式可能跟呆滞不太一样。

#include<bits/stdc++.h>
using namespace std;
const int P=1000000007;
int n,m,s,t,jump[100005]={0},num=1,deep[100005];
struct edge{
	int to,val,jump;
}a[200005];
void add(int x,int y,int z){
	num++;
	a[num].jump=jump[x];
	jump[x]=num;
	a[num].to=y;
	a[num].val=z;
}
bool bfs(){
	queue<int> q;
	while (!q.empty()) q.pop();
	for (int i=1; i<=n; i++) deep[i]=0;
	deep[s]=1;
	q.push(s);
	while (!q.empty()){
		int pos=q.front();
		q.pop();
		for (int i=jump[pos]; i; i=a[i].jump){
			if (!deep[a[i].to] && a[i].val>0){
				deep[a[i].to]=deep[pos]+1;
				q.push(a[i].to);
			}
		}
	}
	return deep[t]!=0;
}
int dfs(int pos,int len){
	if (pos==t) return len;
	int used=0,w=0;
	for (int i=jump[pos]; i; i=a[i].jump){
		if (deep[a[i].to]==deep[pos]+1 && a[i].val>0){
			w=len-used;
			int le=dfs(a[i].to,min(w,a[i].val));
			a[i].val-=le;
			a[i^1].val+=le;
			used+=le;
			if(used==len) return len;
		}
	}
        if (!s) deep[pos]=0;
	return used;
}
int main(){
	int ans=0;
	scanf("%d%d%d%d",&n,&m,&s,&t);
	for (int i=1,x,y,z; i<=m; i++){
		scanf("%d%d%d",&x,&y,&z);
		add(x,y,z);
		add(y,x,0);
	}
	int d;
	while (bfs()){
		ans+=dfs(s,P);
	}
	printf("%d",ans);
	return 0;
}

  

posted @ 2018-03-04 10:55  |斗蜂|  阅读(215)  评论(0编辑  收藏  举报