跳到正文

个人记录

定期算法总结(二)

dream_NOI约 8 分钟
原文
文章信息与版本

文章信息

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

版本

  1. v1当前

算法没总结完,抱歉…

网络流(Dinic):

C++
#include<bits/stdc++.h>
using namespace std;
const int maxn=210;
const int maxm=2010;
const int INF=2147483647/2;
struct Edge{
	int to;
	int next;
	int cap;
	int flow;
}e[maxm*2];

int n,S,T,m,x;
int head[maxn],cnt=0;
long long maxflow=0;
int dep[maxn],cur[maxn];

void init(int s,int t){
	S=s;
	T=t;
	memset(head,-1,sizeof head);
}

void add_edge(int u,int v,int w){
	e[cnt].to=v;
	e[cnt].next=head[u];
	e[cnt].cap=w;
	e[cnt].flow=0;
	head[u]=cnt++;
}

bool bfs(){
	queue<int> q;
	memset(dep,0,sizeof(int)*(n+1));
	dep[S]=1;
	q.push(S);
	while(q.size()){
		int u=q.front();
		q.pop();
		for(int i=head[u];i!=-1;i=e[i].next){
			int v=e[i].to;
			if((!dep[v])&&(e[i].cap>e[i].flow)){
				dep[v]=dep[u]+1;
				q.push(v);
			}
		}
	}
	return dep[T];
}

int dfs(int u,int flow){
	if((u==T)||(!flow)){
		return flow;
	}
	int ret=0;
	for(int& i=cur[u];i!=-1;i=e[i].next){
		int v=e[i].to,d;
		if((dep[v]==dep[u]+1)&&(d=dfs(v,min(flow-ret,e[i].cap-e[i].flow)))){
			ret+=d;
			e[i].flow+=d;
			e[i^1].flow-=d;
			if(ret==flow) return ret;
		}
	}
	return ret;
}

void dinic(){
	while(bfs()){
		memcpy(cur,head,sizeof(int)*(n+1));
		maxflow+=dfs(S,INF);
	}
}

int main(){
	cin>>n>>m>>x;
	init(1,n);
	for(int i=1;i<=m;i++){
		int u,v,c;
		cin>>u>>v>>c;
		add_edge(u,v,c);
		add_edge(v,u,0);
	}
	dinic();
	if(maxflow==0){
		cout<<"Orz Ni Jinan Saint Cow!";
		return 0;
	}
	cout<<maxflow<<" ";
	if(x%maxflow==0){
		cout<<(x/maxflow);
	}else{
		cout<<(x/maxflow)+1;
	}
	return 0;
}

还有EK算法(很简单就不写了)

二分图

C++
#include<bits/stdc++.h>
using namespace std;
const int maxn=10010;
const int maxm=200010;
int n,m,sum[3],cnt,ans;
int h[maxn],used[maxn];
queue<int> q;
struct Edge{
	int to;
	int next;
}e[maxm];
void add(int x,int y){
	cnt++;
	e[cnt].to=y;
	e[cnt].next=h[x];
	h[x]=cnt;
}
bool bfs(int start){
	used[start]=1;
	sum[1]=1,sum[2]=0;
	q.push(start);
	while(!q.empty()){
		int u=q.front();
		q.pop();
		for(int i=h[u];i;i=e[i].next){
			int v=e[i].to;
			if(used[u]==used[v]){
				return 1;
			}
			if(used[v]==0){
				used[v]=used[u]%2+1;
				sum[used[v]]++;
				q.push(v);
			}
		}
	}
	return 0;
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int x,y;
		cin>>x>>y;
		add(x,y);
		add(y,x);
	}
	for(int i=1;i<=n;i++){
		if(used[i]){
			continue;
		}
		if(bfs(i)){
			cout<<"Impossible";
			return 0;
		}
		ans+=min(sum[1],sum[2]);
	}
	cout<<ans;
	return 0;
}

基环树

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1000011;
int n,pos;
ll ans,s[maxn];
struct Edge{
	int to,w,next;
}e[maxn*2];
int cnt,head[maxn];
int ui,vi,E;
bool suc;
bool vis[maxn];
bool used[maxn];
ll f[maxn][2],g[maxn][2];
bool vis1[maxn],vis2[maxn];
void add_edge(int u,int v){
	e[++cnt].to=v;
	e[cnt].next=head[u];
	head[u]=cnt;
}
void fg(int w){
	used[w]=true;
	int now=head[w];
	while(now){
		if(!used[e[now].to]){
			fg(e[now].to);
		}
		now=e[now].next;
	}
}
void find_circle(int w,int fa){
	if(suc) return ;
	int now=head[w];
	vis[w]=true;
	while(now){
		if(!vis[e[now].to]){
			find_circle(e[now].to,w);
		}else if(e[now].to!=fa){
			ui=w;
			vi=e[now].to;
			E=now;
			suc=true;
			return ;
		}
		now=e[now].next;
	}
}
void dfs1(int w){
	int now=head[w];
	f[w][1]=s[w];
	vis1[w]=true;
	while(now){
		if(!vis1[e[now].to]&&(now^1)!=E){
			dfs1(e[now].to);
			f[w][0]+=max(f[e[now].to][0],f[e[now].to][1]);
			f[w][1]+=f[e[now].to][0];
		}
		now=e[now].next;
	}
}
void dfs2(int w){
	int now=head[w];
	g[w][1]=s[w];
	vis2[w]=true;
	while(now){
		if(!vis2[e[now].to]&&(now^1)!=E){
			dfs2(e[now].to);
			g[w][0]+=max(g[e[now].to][0],g[e[now].to][1]);
			g[w][1]+=g[e[now].to][0];
		}
		now=e[now].next;
	}
}
int main(){
	cnt=1;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>s[i]>>pos;
		add_edge(i,pos);
		add_edge(pos,i);
	}
	for(int i=1;i<=n;i++){
		if(used[i]) continue;
		fg(i);
		suc=false;
		find_circle(i,0);
		dfs1(ui);
		dfs2(vi);
		ans+=max(f[ui][0],g[vi][0]);
	}
	cout<<ans;
	return 0;
}
原文