网络流_EK模板

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

const int maxn = 205;
const int inf = 0x3f3f3f3f;

int n, m, s, t, maxflow;
int h[maxn], e[maxn], ne[maxn], w[maxn], pre[maxn], incf[maxn], idx;
bool vis[maxn];

inline void add(int a, int b, int c) {
	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
	e[idx] = a, w[idx] = 0, ne[idx] = h[b], h[b] = idx ++;
}

inline bool bfs(void) {
	memset(vis, false, sizeof vis);
	queue<int> q;
	q.push(s);
	vis[s] = true;
	incf[s] = inf;
	
	while(q.size()) {
		int u = q.front(); q.pop();
		
		for(int i = h[u]; i != -1; i = ne[i]) {
			if(w[i]) {
				int v = e[i];
				if(vis[v]) continue;
				incf[v] = min(incf[u], w[i]);
				pre[v] = i;
				q.push(v);
				vis[v] = true;
				if(v == t) return true;
			}
		}
	}
	
	return false;
}

inline void update(void) {
	int x = t;
	while(x != s) {
		int i = pre[x];
		w[i] -= incf[t];
		w[i ^ 1] += incf[t];
		x = e[i ^ 1];
	}
	
	maxflow += incf[t];
}

int main(void) {
//	freopen("in.txt", "r", stdin);
	
	scanf("%d%d", &m, &n);
	memset(h, -1, sizeof h);
	s = 1, t = n;
	for(int i = 1; i <= m; i ++) {
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		add(a, b, c);
	}
	
	while(bfs()) update();
	
	printf("%d\n", maxflow);
	
//	fclose(s/tdin);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值