跳到正文

算法·理论

【模板】堆模拟费用流

wukaichen888约 6 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 6 分钟

版本

  1. v1当前

造福懒得再写模拟费用流的我(?)

图的最小覆盖集较小,即可使用该做法。

其中,非关键点邻域均为关键点。常数极大,但通用。

C++
int S,T,F,tot1;// 总点数
int w[M],tot2=1,bid[N2][N2];// 总边数
struct edge{int to,id;};vector<edge>V[N]; ll ans,cst[M];
struct E{ll w;int mid;}qwq[N2][N2];
inline bool operator <(E x,E y){if(x.w!=y.w)return x.w<y.w;return x.mid<y.mid;}
inline bool operator ==(E x,E y){return (x.w==y.w)&&(x.mid==y.mid);}
struct ksd{
	priority_queue<E> A,B;
	void ins(E x){A.push(x);}
	void del(E x){B.push(x);}
	E ask(){
		while((!B.empty())&&(A.top()==B.top())) A.pop(),B.pop();
		if(A.empty()) return E{-inf,0}; return A.top();}
	void cl(){while(!A.empty())A.pop();while(!B.empty())B.pop();}
}G[N2][N2];
//   col=1 表示关键点
int col[N],vis[N],las[N],path[N],seg[N],rev[N],tot3;ll dis[N];queue<int>que;
void cl(){
	for(int i=1;i<=tot1;i++) V[i].clear(),col[i]=0;
	for(int i=1;i<=tot3;i++) for(int j=1;j<=tot3;j++) G[i][j].cl();
	tot1=0,tot2=1,tot3=0,ans=0;}
void link1(int u,int v,int v1,int v2){++tot2;w[tot2]=v1,cst[tot2]=v2;V[u].push_back(edge{v,tot2});}
void link(int u,int v,int v1,int v2){link1(u,v,v1,v2),link1(v,u,0,-v2);}
void flow(int x,int y,int h){
	int id; assert(col[x]||col[y]);
	if(col[x]&&col[y]){
		id=bid[seg[x]][seg[y]];assert(w[id]);
		if(w[id]==1)   G[seg[x]][seg[y]].del(E{cst[id],0});
		if(w[id^1]==0) G[seg[y]][seg[x]].ins(E{cst[id^1],0});
		w[id]--,w[id^1]++;return ;}
	if(col[x]){
		for(edge e:V[y]) if(e.to==x){id=e.id^1;break;}
		for(edge e:V[y]) if(e.to!=x){
			if(w[id]==1)   if(w[e.id])   G[seg[x]][seg[e.to]].del(E{cst[id]+cst[e.id],y});
			if(w[id^1]==0) if(w[e.id^1]) G[seg[e.to]][seg[x]].ins(E{cst[id^1]+cst[e.id^1],y});}
		w[id]--,w[id^1]++;return ;}
	if(col[y]){
		for(edge e:V[x]) if(e.to==y){id=e.id;break;}
		for(edge e:V[x]) if(e.to!=y){
			if(w[id]==1)   if(w[e.id^1]) G[seg[e.to]][seg[y]].del(E{cst[id]+cst[e.id^1],x});
			if(w[id^1]==0) if(w[e.id])   G[seg[y]][seg[e.to]].ins(E{cst[id^1]+cst[e.id],x});}
		w[id]--,w[id^1]++;return ;}}
void EK(){
	assert(tot1!=0); assert(F!=0); assert(col[S]); assert(col[T]);
	for(int i=1;i<=tot1;i++) if(col[i]) seg[i]=++tot3,rev[tot3]=i;
	for(int i=1;i<=tot3;i++) for(edge e:V[rev[i]]) if(col[e.to]){
		bid[i][seg[e.to]]=e.id; G[i][seg[e.to]].ins(E{cst[e.id],0});}
	for(int i=1;i<=tot1;i++) if(!col[i])
		for(edge e1:V[i]) if(w[e1.id^1])
		for(edge e2:V[i]) if(w[e2.id]) G[seg[e1.to]][seg[e2.to]].ins(E{cst[e1.id^1]+cst[e2.id],i});
	for(int i=1,len;i<=F;i++){
		for(int j=1;j<=tot3;j++) vis[j]=las[j]=0,dis[j]=-inf;
		for(int j=1;j<=tot3;j++) for(ll p=1;p<=tot3;p++) qwq[j][p]=G[j][p].ask();
		vis[seg[S]]=1; que.push(seg[S]); dis[seg[S]]=0;
		while(!que.empty()){
			int u=que.front(); que.pop(); vis[u]=0;
			for(ll j=1;j<=tot3;j++)
				if(dis[j]<dis[u]+qwq[u][j].w){
					dis[j]=dis[u]+qwq[u][j].w; las[j]=u;
					if(!vis[j]) que.push(j),vis[j]=1;}}
		if(dis[seg[T]]<0) break;
		assert(dis[seg[T]]!=-inf); ans+=dis[seg[T]];
		path[1]=seg[T]; for(len=1;path[len]!=seg[S];len++) path[len+1]=las[path[len]];
		for(int i=len;i>1;i--)
			if(qwq[path[i]][path[i-1]].mid)
				flow(rev[path[i]],qwq[path[i]][path[i-1]].mid,1),
				flow(qwq[path[i]][path[i-1]].mid,rev[path[i-1]],1);
			else flow(rev[path[i]],rev[path[i-1]],1);
	}
}
原文