萌新求助跑酷大神解惑hlpp(欢乐跑跑)(雾
查看原帖
萌新求助跑酷大神解惑hlpp(欢乐跑跑)(雾
576817
Lyrella楼主2024/8/12 16:55
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll rd(){
	ll x = 0, f = 1; char c = getchar();
	while(! isdigit(c)){if(c == '-')f = - f; c = getchar();}
	while(isdigit(c)){x = (x << 3) + (x << 1) + (c ^ 48); c = getchar();}
	return x * f;
}

const int N = 1205, M = 1.2e5 + 5, INF = 0x3f3f3f3f;
int n, m, s, t, hd[N], cnt = 1;
struct edge{
	int nxt, to; ll w;
}e[M << 1];
void add(int u, int v, ll w){
	e[++cnt] = (edge){hd[u], v, w}; hd[u] = cnt;
}

int h[N], gap[N], height;
ll exc[N];
stack < int > b[N];
bool pushin(int u){
	bool op = u == s;
	for(int i = hd[u]; i; i = e[i].nxt){
		int v = e[i].to; ll w = e[i].w;
		if(! w or ! op and h[u] != h[v] + 1 or h[u] == INF)continue;
		ll k = op ? w : min(w, exc[u]);
		if(v != s and v != t and ! exc[v])b[h[v]].push(v), height = max(height, h[v]);
		exc[u] -= k; exc[v] += k; e[i].w -= k; e[i ^ 1].w += k;
		if(! exc[u])return 0;
	}
	return 1;
}
void relabel(int u){
	h[u] = INF;
	for(int i = hd[u]; i; i = e[i].nxt)if(e[i].w)h[u] = min(h[u], h[e[i].to]);
	if(++h[u] < n){
		b[h[u]].push(u);
		height = max(height, h[u]);
		++gap[h[u]];
	}
}
bool bfs(){
	memset(h, 0x3f, sizeof h);
	queue < int > q;
	q.push(t); h[t] = 0;
	while(! q.empty()){
		int u = q.front(); q.pop();
		for(int i = hd[u]; i; i = e[i].nxt){
			int v = e[i].to;
			if(e[i ^ 1].w and h[v] > h[u] + 1)h[v] = h[u] + 1, q.push(v);
		}
	}
	return h[s] != INF;
}
int sel(){
	while(b[height].empty() and height > - 1)--height;
	return ~ height ? b[height].top() : 0;
}
ll HLPP(){
	if(! bfs())return 0;
	memset(gap, 0, sizeof gap);
	for(int i = 1; i <= n; ++i)if(h[i] != INF)++gap[h[i]];
	h[s] = n; pushin(s);
	int u;
	while(u = sel()){
		b[height].pop();
		if(pushin(u)){
			if(! --gap[h[u]])
				for(int i = 1; i <= n; ++i)if(i != s and i != t and h[i] > h[u] and h[i] < n + 1)h[i] = n + 1;
			relabel(u);
		}
	}
	return exc[t];
}

signed main(){
	n = rd(); m = rd(); s = rd(); t = rd();
	for(int i = 1; i <= m; ++i){
		int u = rd(), v = rd(), w = rd();
		add(u, v, w); add(v, u, 0);
	}
	printf("%lld", HLPP());
	return 0;
}

这份代码过不了hack数据,但是如果将 HLPP 中的if(! bfs())return 0; 改为 if(! bfs)return 0; 它就能过。 本萌新很疑惑,求跑酷大神解惑,小号悬一关

2024/8/12 16:55
加载中...