求助费用流
  • 板块学术版
  • 楼主happybob
  • 当前回复4
  • 已保存回复4
  • 发布时间2022/8/20 19:20
  • 上次更新2023/10/27 14:25:21
查看原帖
求助费用流
332914
happybob楼主2022/8/20 19:20

rt,WA + TLE,63 pts

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

#define int long long

const int N = 1e5 + 5;

int idx, e[N], c[N], h[N], ne[N], wp[N], fans = 0;
int n, m, s, t;

void add(int u, int v, int w1, int w2)
{
	e[idx] = v, c[idx] = w1, wp[idx] = w2, ne[idx] = h[u], h[u] = idx++;
	e[idx] = u, c[idx] = 0, wp[idx] = 0, ne[idx] = h[v], h[v] = idx++;
}

int cur[N], dis[N];
bool inq[N];

int spfa()
{
	memset(dis, 0x3f, sizeof dis);
	memset(inq, 0, sizeof inq);
	//memset(cur, -1, sizeof cur);
	queue<int> q;
	q.push(s);
	cur[s] = h[s];
	dis[s] = 0;
	inq[s] = 1;
	while (q.size())
	{
		int u = q.front();
		q.pop();
		inq[u] = 0;
		for (int i = h[u]; ~i; i = ne[i])
		{
			int j = e[i];
			if (c[i] > 0 && dis[j] > dis[u] + c[i])
			{
				dis[j] = dis[u] + c[i];
				cur[j] = h[j];
				if (!inq[j])
				{
					q.push(j);
					inq[j] = 1;
				}
			}
		}
	}
	return dis[t] != 0x3f3f3f3f3f3f3f3f;
}

int dfs(int u, int lim)
{
	if (u == t) return lim;
	int ret1 = 0;
	for (int i = cur[u]; ~i && ret1 < lim; i = ne[i])
	{
		cur[u] = i;
		int j = e[i];
		if (c[i] > 0 && dis[j] == dis[u] + c[i])
		{
			int p = dfs(j, min(c[i], lim - ret1));
			if (!p)
			{
				dis[j] = LLONG_MAX;
			}
			else
			{
				ret1 += p;
				fans += wp[i] * p;
				c[i] -= p;
				c[i ^ 1] += p;
			}
		}
	}
	return ret1;
}

int dinic()
{
	int ret = 0;
	while (spfa())
	{
		int g;
		while (true)
		{
			//printf("%lld\n", 44);
			g = dfs(s, LLONG_MAX);
			if (!g) break;
			ret += g;
		}
	}
	return ret;
}

signed main()
{
	memset(h, -1, sizeof h);
	scanf("%lld%lld%lld%lld", &n, &m, &s, &t);
	for (int i = 1; i <= m; i++)
	{
		int u, v, w, w2;
		scanf("%lld%lld%lld%lld", &u, &v, &w, &w2);
		add(u, v, w, w2);
	}
	int ans = dinic();
	printf("%lld %lld\n", ans, fans);
	return 0;
}
2022/8/20 19:20
加载中...