网络最大流:isap

据说isap比dinic快。

参考:https://www.cnblogs.com/owenyu/p/6852664.html
我太懒了

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#define ll long long
using namespace std;
const int N = 205 ,M = 5005,inf=0x7fffffff;
ll head[N],n,m,s,t,ecnt=1,cur[N],gap[N];
queue<ll>q;
struct Edge{
	ll nxt,to,val;
}e[M<<1];
void add(int bg,int ed,ll val) {
	e[++ecnt].nxt=head[bg],e[ecnt].to=ed,e[ecnt].val=val,head[bg]=ecnt;
}
ll h[N];
void bfs() {
	q.push(t);
	memset(h,-1,sizeof h);
	h[t]=0;
	gap[0]=1;
	while(!q.empty()) {
		int now=q.front();
		q.pop();
		for(int i=head[now];i;i=e[i].nxt) {
			if(h[e[i].to]==-1) h[e[i].to]=h[now]+1,q.push(e[i].to),gap[h[e[i].to]]++;
		}
	}
	return;
}
ll dfs(int x,ll f) {
	if(x==t) return f;
	ll used=0,tp;
	for(int i=cur[x];i;i=e[i].nxt) {
		int v=e[i].to;
		if(h[v]==h[x]-1&&e[i].val) {
			tp=dfs(v,min(e[i].val,f-used));
			e[i].val-=tp;
			e[i^1].val+=tp;
			if(e[i].val) cur[x]=i;
			used+=tp;
			if(used==f) return f;
		}
	}
	gap[h[x]]--;
	if(gap[h[x]]==0) h[s]=n+1;
	h[x]++;
	gap[h[x]]++;
	return used;
}
void isap() {
	ll sum=0;
	bfs();
	while(h[s]<n) {
		memcpy(cur,head,sizeof cur);
		sum+=dfs(s,inf);
	}
	cout<<sum<<endl;
}
int main() {
	cin>>n>>m>>s>>t;
	for(int i=1,x,y,z;i<=m;i++) {
		cin>>x>>y>>z;
		add(x,y,z);
		add(y,x,0);
	}
	isap();
	return 0;
}
原文地址:https://www.cnblogs.com/sdfzhsz/p/13728673.html