• 周五. 10月 7th, 2022

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

洛谷 P3376 【模板】网络最大流

admin

11月 28, 2021

题目传送门

板子不解释

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#define pd(i) (i % 2 == 1) ? i + 1 : i - 1
#define _max 20000000000000

using namespace std;

int n,m,s,t,tot,hu[500001];
long long dis[500001],ans;
vector<int> d[500001];
struct kkk {
	int fr,to;
	long long rl,ll;
}e[100001];

inline long long min(long long ss,int dd) {
	if(ss < dd) return ss;
	return dd;
}

inline void add(int x,int y,int vv) {
	e[++tot].fr = x;
	e[tot].to = y;
	e[tot].rl = vv;
	d[x].push_back(tot);
	e[++tot].fr = y;
	e[tot].to = x;
	e[tot].rl = 0;
	d[y].push_back(tot);
}

inline bool bfs() {
	memset(dis,-1,sizeof(dis));
	queue<int> q;
	q.push(s);
	dis[s] = 0;
	while(!q.empty()) {
		int u = q.front();
		q.pop();
		for(int i = 0;i < d[u].size(); i++) {
			kkk o = e[d[u][i]];
			if(dis[o.to] == -1 && o.rl > o.ll) {
				dis[o.to] = dis[u] + 1;
				q.push(o.to);
			}
		}
	}
	return dis[t] != -1;
}

inline long long dfs(int u,long long a) {
	if(u == t || a == 0) return a;
	long long aa = 0;
	for(int &i = hu[u];i < d[u].size(); i++) {
		kkk &o = e[d[u][i]];
		if(dis[o.to] == dis[u] + 1) {
			long long f = dfs(o.to,min(a,o.rl - o.ll));
			o.ll += f;
			e[pd(d[u][i])].ll -= f;
			aa += f;
			a -= f;
			if(a == 0) break;
		}
	}
	return aa;
}

int main() {
	scanf("%d%d%d%d",&n,&m,&s,&t);
	for(int i = 1;i <= m; i++) {
		int u,v;
		long long w;
		scanf("%d%d%lld",&u,&v,&w);
		add(u,v,w);
	}
	while(bfs()) {
		memset(hu,0,sizeof(hu));
		ans += dfs(s,_max);
	}
	printf("%lld",ans);
	return 0;
}

发表回复

您的电子邮箱地址不会被公开。