跳到正文

个人记录

斐波那契堆 原始对偶

cnlihuan约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年7月8日
收录
2026年7月10日
更新
2026年11月18日
篇幅
约 7 分钟

版本

  1. v1当前
C++
template <typename _tp,typename _val,int siz=32768>
 class FibonacciHeap {
private:
	struct Node{
		Node*l,*r,*ls,*f;
		_tp x;_val v;
		bool mark;int du;
		Node(Node* _l,Node* _r,Node* _ls,Node* _f,_tp _x,_val _v){
			l=_l,r=_r,ls=_ls,f=_f,x=_x;v=_v;mark=0;du=0;
		}
	};
	int _size;
	Node* _top;
	Node* _pos[siz];
	void _cut(Node* x){
		if(x->r!=NULL)x->r->l=x->l;
		if(x->l!=NULL)x->l->r=x->r;
		x->r=NULL;x->l=NULL; 
	}
	void _split(Node* x){
		if(_top==NULL)_top=x;
		if(x==NULL||x==_top)return;
		
		if(x->f!=NULL){
			x->f->du--;
			if(x->f->ls==x){
				if(x->f->mark)_split(x->f);
				x->f->ls=x->r;	
			}
		}
		
		_cut(x);
		
		x->l=_top->l;x->r=_top;
		if(x->l!=NULL)x->l->r=x;
		_top->l=x;
		x->f=NULL;x->mark=0;
		
	}
	void _eraseRoot(Node* x){
		_cut(x);
		if(_top==x)_top=NULL;
		delete x;
	}
	Node* _tmp[siz];
	void _merge(Node* x,Node* y){
		if(x->x>y->x){
			swap(x,y);
		}
		_cut(y);
		y->f=x;
		x->du++;
		y->r=x->ls;
		if(x->ls!=NULL)x->ls->l=y;
		x->ls=y;
		_tmp[x->du]=x;
	}
public:
	FibonacciHeap(){
		for(int i=0;i<siz;i++){
			_tmp[i]=NULL;
			_pos[i]=NULL;
		}
		_size=0;_top=NULL;
	}
	
	void insert(_tp x,_val v){
		_size++;
		Node* w=new Node(NULL,NULL,NULL,NULL,x,v);
		_pos[v]=w;
		_split(w);
		if(x<_top->x){
			_top=w;
		}
	}
	pair<_tp,_val> top(){
		return make_pair(_top->x,_top->v);
	}
	int size(){
		return _size;
	}
	void pop(){
		_size--;
		Node* w=_top;
		while(w->ls!=NULL)_split(w->ls);
		vector<Node*> v;Node* cur=w;
		while(cur->l!=NULL){
			v.push_back(cur->l);
			cur=cur->l;
		}cur=w;
		while(cur->r!=NULL){
			v.push_back(cur->r);
			cur=cur->r;
		}
		
		_pos[w->v]=NULL;
		_eraseRoot(w);
		for(unsigned int i=0;i<v.size();i++){
			if(_tmp[v[i]->du]!=NULL){
				Node* tmp=_tmp[v[i]->du];
				_tmp[v[i]->du]=NULL;
				_merge(v[i],tmp);
			}else{
				_tmp[v[i]->du]=v[i];
			}
		}
		for(unsigned int i=0;i<v.size();i++){
			_tmp[v[i]->du]=NULL;
			if(v[i]->f!=NULL)continue;
			if(_top==NULL||v[i]->x < _top->x){
				_top=v[i];
			}
		}
	}
	void decrease(_tp k,_val val){
		Node* w=_pos[val];
		if(w==NULL){
			insert(k,val);
			return;
		}
		if(!(k<w->x||k==w->x)){
			cout<<"FUCKCCF!!!";return;
		}
		w->x=k;
		_split(w);
	}
};

namespace PrimalDual{
	const int MAXN=50005;
	const int MAXM=100005;
	const int INF=0x3f3f3f3f3f3f3f3f;
	int S,T,N; int ans,cost;
	int h[MAXN],to[MAXM],nxt[MAXM],tot=1,v[MAXM],w[MAXM];
	void add(int x,int y,int val,int cst){
		tot++;
		to[tot]=y;nxt[tot]=h[x];h[x]=tot;
		v[tot]=val;w[tot]=cst;
		tot++;
		to[tot]=x;nxt[tot]=h[y];h[y]=tot;
		v[tot]=0;w[tot]=-cst;
	}
	int dis[MAXN],e[MAXN],dur[MAXN];
	void clear(){
		ans=0;cost=0;tot=1;
		for(int i=0;i<=N;i++){
			h[i]=0;dis[i]=0;
			e[i]=0;dur[i]=0;
		}
	}
	void dij(int* dis,int* e){
		for(int i=0;i<=N;i++){
			dis[i]=INF;
		}
		FibonacciHeap<int,int,MAXN> q;
		dis[S]=0;q.insert(dis[S],S);
		while(q.size()!=0){
			int x=q.top().second;
			q.pop();
			int y=h[x];
			while(y){
				if(v[y]<=0){
					y=nxt[y];continue;
				}
				int t=to[y];int v=w[y]+e[x]-e[t];y=nxt[y];
				if(dis[t]>dis[x]+v){
					dis[t]=dis[x]+v;
					q.decrease(dis[t],t);
				}
			}
		}
	}
	int vis[MAXN];
	int dfs(int x,int mx){
		if(x==T)return mx;
		int y=h[x];
		vis[x]=1;int res=0;
		while(y){
			int t=to[y];
			if(vis[t]||v[y]<=0||dis[t]!=dis[x]+w[y]+e[x]-e[t]){
				y=nxt[y];continue;
			}
			int val=dfs(t,min(mx,v[y]));
			v[y]-=val;v[y^1]+=val;
			cost+=w[y]*val;res+=val;
			mx-=val;if(mx<0)break;
			y=nxt[y];
		}
		vis[x]=0;
		return res;
	}
	void sol(){
		dij(e,dis);
		
		dij(dis,e);
		while(dis[T]<INF){
			ans+=dfs(S,INF);
			for(int i=0;i<=N;i++){
				e[i]+=dis[i];
			}
			dij(dis,e);
		}
	}
}
原文