求T的原因
查看原帖
求T的原因
540333
__xzm__楼主2022/12/2 20:46

费用流板子题过不去了(

#include <bits/stdc++.h>

#define int long long

using namespace std;

const int N = 1010, M = 100010;
const int inf = 1e15;

int head[N], nxt[M], to[M], idx;
int wc[M], wf[M];

void init()
{
	idx = 0;
	memset(head, -1, sizeof head);
}

void add(int a, int b, int c, int f)
{
	nxt[idx] = head[a], head[a] = idx;
	to[idx] = b, wc[idx] = c, wf[idx] = f, idx++;
	
	nxt[idx] = head[b], head[b] = idx;
	to[idx] = a, wc[idx] = -c, wf[idx] = 0, idx++;
}

int n, m, S, T;
int dist[N], pre[N], prei[N];
bool inque[N];

bool spfa()
{
	for (int i = 1; i <= n; i++) dist[i] = inf;
	dist[S] = 0;
	
	queue <int> q;
	inque[S] = true;
	q.push(S);
	
	while (!q.empty())
	{
		int f = q.front(); q.pop();
		
		inque[f] = false;
		
		for (int i = head[f]; ~i; i = nxt[i])
			if (wf[i] && dist[f]+wc[i] < dist[to[i]])
			{
				dist[to[i]] = dist[f]+wc[i];
				pre[to[i]] = f, prei[to[i]] = i;
				if (!inque[to[i]])
				{
					inque[to[i]] = true;
					q.push(to[i]);
				}
			}
	}
	
	return (dist[T] != inf);
}

int dfs()
{
	int p = T, res = inf;
	while (p != S)
	{
		int i = prei[p];
		p = pre[p];
		res = min(res, wf[i]);
	}
	
	p = T;
	while (p != S)
	{
		int i = prei[p];
		p = pre[p];
		wf[i] -= res, wf[i^1] += res;
	}
	
	return res;
}

int dinic()
{
	int cost = 0, flow = 0;
	while (spfa())
	{
		int t = dfs();
		flow += t, cost += t*dist[T];
	}
	
	printf("%lld %lld\n", flow, cost);
}

signed main()
{
	init();
	
	scanf("%lld%lld", &n, &m);
	S = 1, T = n;
	
	while (m--)
	{
		int a, b, f, c;
		scanf("%lld%lld%lld%lld", &a, &b, &f, &c);
		add(a, b, c, f);
	}
	
	dinic();
	
	return 0;
}
2022/12/2 20:46
加载中...