题解:P9902 『PG2』模拟最大流
_CuteRalsei_ · · 题解
更多卡常!请点击这里。
关于我尝试 HLPP 过这题,然而并不会 gap 优化(其实是我写了然后负优化)怎么办?
你大概需要在 HLPP 模板里达到
优化 1
首先:大家需要知道:vector 存图除了 push_back 慢一点,不比链式前向星慢。
因此我们直接对图离线,记录度数,然后 reserve(deg[u])。
优化 2
HLPP 写优先队列是大常数的行为,应该写桶维护堆。
均摊分析可知复杂度正确(如果我没弄错的话)。
不过桶还是比较慢,推荐使用 bitset。
具体地,将 _Find_next()。
这样常数很小。
struct bueue{
int ma=-1,siz=0;
vector<int> t[N];
bitset<N> vis;
void push(int x,int y){
if(x>n)return;
siz++;
t[x].push_back(y);
vis[n-x]=1;
ma=max(ma,x);
}
void pop(){
siz--;
t[ma].pop_back();
if(!t[ma].size()){
vis[n-ma]=0;
ma=n-vis._Find_next(n-ma);
}
}
int size(){return siz;}
int top(){return t[ma].back();}
void clear(int n){
siz=0,ma=-1;
vis.reset();
for(int i=0;i<=n;i++)t[i].clear();
}
}pq;
优化 3
relabel 优化还是要加的。
每
:::success[code]
#include<bits/stdc++.h>
using namespace std;
/*
I LOVE HLPP
*/
const int N=8e4+1,M=1e6+1;
struct node{int v,w,fb;};
vector<node> G[N];
int h[N],ex[N],n,m,s,t,tot=2,cur[N],deg[N],tmph[N];
struct bueue{
int ma=-1,siz=0;
vector<int> t[N];
bitset<N> vis;
void push(int x,int y){
if(x>n)return;
siz++;
t[x].push_back(y);
vis[n-x]=1;
ma=max(ma,x);
}
void pop(){
siz--;
t[ma].pop_back();
if(!t[ma].size())vis[n-ma]=0;
ma=n-vis._Find_first();
}
int size(){return siz;}
int top(){return t[ma].back();}
void clear(int n){
siz=0,ma=-1;
vis.reset();
for(int i=0;i<=n;i++)t[i].clear();
}
}pq;
void relabel(){
queue<int> q;
q.push(t);
for(int i=1;i<=n;i++)tmph[i]=-1;
tmph[t]=0;
while(q.size()){
int u=q.front();
q.pop();
for(auto [v,w,fb]:G[u]){
if(G[v][fb].w&&tmph[v]==-1){
tmph[v]=tmph[u]+1;
q.push(v);
}
}
}
for(int i=1;i<=n;i++)h[i]=tmph[i]==-1?n+1:tmph[i];
h[s]=n;
for(int i=1;i<=n;i++)cur[i]=0;
pq.clear(n);
for(int i=1;i<=n;i++){
if(ex[i]&&i!=s&&i!=t)pq.push(h[i],i);
}
}
int hlpp(){
queue<int> q;
q.push(t);
while(q.size()){
int u=q.front();
q.pop();
for(auto [v,w,fb]:G[u]){
if(!h[v]){
h[v]=h[u]+1;
q.push(v);
}
}
}
// cout<<"init"<<endl;
h[s]=n;
for(auto &[v,w,fb]:G[s]){
ex[v]+=w;
G[v][fb].w+=w;
w=0;
if(v!=s&&v!=t){//s,t不能进
cur[v]=0;
pq.push(h[v],v);
}
}
int cnt=0;
const int ti=n;
while(pq.size()){
register int u=pq.top(),mi=0x3f3f3f3f;//mi:最小还有残余
pq.pop();
int i=cur[u],sz=G[u].size();
while(i<sz){
auto &[v,w,fb]=G[u][i];
if(h[v]==h[u]-1&&w){//推
int use=min(w,ex[u]);
w-=use;
G[v][fb].w+=use;
ex[u]-=use;
ex[v]+=use;
if(ex[v]==use&&v!=s&&v!=t){
cur[v]=0;
pq.push(h[v],v);
}
}
if(!ex[u]){
cur[u]=i;
break;
}
if(w)mi=min(mi,h[v]);
cur[u]=++i;
}
if(ex[u]){
h[u]=mi+1;//relable
cur[u]=0;
if(h[u]<=n)pq.push(h[u],u);
}
cnt++;
if(cnt==n)relabel(),cnt=0;
}
return ex[t];
}
#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#else
#define getchar _getchar_nolock
#define putchar _putchar_nolock
#endif
inline int read(){
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=(x << 1) + (x << 3)+ch-48;ch=getchar();}
return x*f;
}
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
n=read(),m=read(),read();
s=1,t=n;
vector<array<int,3>> edges;
edges.reserve(m);
while(m--){
int u,v,w;
u=read(),v=read(),w=read();
deg[u]++;deg[v]++;
edges.push_back({u,v,w});
}
for(int i=1;i<=n;i++)G[i].reserve(deg[i]);
for(auto [u,v,w]:edges){
G[u].push_back({v,w,G[v].size()});
G[v].push_back({u,0,G[u].size()-1});
}
cout<<hlpp();
return 0;
}
:::