求调P2740最大流板子
  • 板块学术版
  • 楼主happybob
  • 当前回复1
  • 已保存回复1
  • 发布时间2022/3/10 21:56
  • 上次更新2023/10/28 06:53:33
查看原帖
求调P2740最大流板子
332914
happybob楼主2022/3/10 21:56

RT,最后一个点 WA,EK。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;

#define int long long

const int N = 305, INF = 1e18;

int n, m, sp, tp;

int gra[N][N], flow[N], pre[N];

int bfs(int s, int t)
{
	memset(flow, 0, sizeof flow);
	memset(pre, 0x3f, sizeof pre);
	flow[s] = INF;
	pre[s] = 0;
	queue<int> q;
	q.push(s);
	while (q.size())
	{
		int u = q.front();
		q.pop();
		for (int i = 1ll; i <= n; i++)
		{
			if (i != u && gra[u][i] > 0 && pre[i] == pre[0])
			{
				pre[i] = u;
				q.push(i);
				flow[i] = min(flow[u], gra[u][i]);
			}
		}
	}
	if (pre[t] == pre[0]) return -1ll;
	return flow[t];
}

int maxflow(int s, int t)
{
	int res = 0;
	while (true)
	{
		int k = bfs(s, t);
		if (k == -1ll) break;
		int l = t;
		while (l != s)
		{
			int fa = pre[l];
			gra[fa][l] -= k;
			gra[l][fa] += k;
			l = fa;
		}
		res += k;
	}
	return res;
}

signed main()
{
	scanf("%lld%lld", &n, &tp);
	for (int i = 1ll; i <= n; i++)
	{
		int u, v, w;
		scanf("%lld%lld%lld", &u, &v, &w);
		gra[u][v] += w;
	}
	sp = 1ll;
	printf("%lld\n", maxflow(sp, tp));
	return 0;
}
2022/3/10 21:56
加载中...