是写丑了吗?
查看原帖
是写丑了吗?
379768
Wzhone楼主2023/11/9 16:25

HLPP死活卡常过不了 另: gap优化的地方反复优化会不会导致复杂度问题?

附:代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int MAXN = 1300, MAXM = 130000;

struct ed{
	ll to, n, v;
}edge[MAXM * 2];

ll n, m, s, t, cnt = 1;
ll head[MAXN], h[MAXN], lef[MAXN], inque[MAXN], gap[MAXN];

inline void add(int a, int b, ll c){
	edge[++cnt] = {b, head[a], c};
	head[a] = cnt;
	edge[++cnt] = {a, head[b], 0};
	head[b] = cnt;
}

struct cmp{
	bool operator () (int a, int b) const{
		return h[a] < h[b];
	}
};

priority_queue <int, vector<int>, cmp> q;
int to, v;

inline void bfs(){
	for(int i = 1; i <= n; i++) h[i] = -1;
	queue <int> qs;
	qs.push(t);
	h[t] = 0;gap[0] ++;
	while(!qs.empty()){
		int now = qs.front(); qs.pop();
		for(int i = head[now]; i; i = edge[i].n){
			to = edge[i].to, v = edge[i].v;
			if(v == 0 && h[to] == -1){
				h[to] = h[now] + 1;
				++gap[h[to]];
				qs.push(to);
			}
		}
	}
}

bool fs = 0;

inline void relabel(int now){
	gap[h[now]]--;
	if(gap[h[now]] == 0 && !fs){
		fs = 1;
		for(int i = 1; i <= n; i++)
			if(h[i] > h[now]) h[i] = n + 1;
	}
	
	h[now] = 3 * MAXN;
	for(int i = head[now]; i ; i = edge[i].n){
		to = edge[i].to, v = edge[i].v;
		if(v) h[now] = min(h[now], h[to]);
	}
	h[now]++;
	gap[h[now]]++;
}

inline void pushing(int now){
	for(int i = head[now]; i; i = edge[i].n){
		to = edge[i].to, v = edge[i].v;int l = lef[now];
		if(min(v, l) && h[to] + 1 == h[now]){
			edge[i].v -= min(v, l);
			edge[i ^ 1].v += min(v, l);
			lef[now] -= min(v, l);
			lef[to] += min(v, l);
			if(to != s && to != t && !inque[to]) q.push(to), inque[to] = 1;
		}
		if(lef[now] == 0) return;
	}
}

inline ll hlpp(){
	//memset(inque, 0, sizeof inque);
	bfs();
	if(h[s] == -1) return 0;//不连通 
	h[s] = n;
	for(int i = head[s]; i; i = edge[i].n){
		to = edge[i].to, v = edge[i].v;
		if(v){
			edge[i ^ 1].v += edge[i].v;
			edge[i].v = 0;
			lef[to] += v;
			if(to != s && to != t && !inque[to]) q.push(to), inque[to] = 1;
		}
	}
	
	while(!q.empty()){
		int now = q.top(); q.pop();
		inque[now] = 0;
		pushing(now);
		if(lef[now] != 0) relabel(now), q.push(now), inque[now] = 1;
	}
	
	return lef[t];
}

char bi[1 << 23], *p1 = bi, *p2 = bi;
inline char gc(){
	if(p1 == p2) p2 = (p1 = bi) + fread(bi, 1, 1<<21, stdin);
	return *(p1++);
}

inline ll read(){
	ll sum = 0; char c = gc();
	while(c > '9' || c < '0') c = gc();
	while(c >= '0' && c <= '9') sum = (sum << 1) + (sum << 3) + c - '0', c = gc(); 
	return sum;
}

int main(){
	//freopen("1.in","r",stdin); 
	//ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	n = read(); m = read(); s = read(); t = read();
	int x, y, z;
	for(int i = 1; i <= m; i++){
		x = read(); y = read(); z = read();
		add(x, y, z);
	}
	cout << hlpp();
	//cerr << "\n" << clock();
}
2023/11/9 16:25
加载中...