C++模板大全(二)

C++模板大全(二)

可通过点击右下角打开目录,快速跳转到你想要的章节。

怕以后找不到的话,可以收藏本文或关注我哦。

模板难度层层递进,跟着本文复习模板,效果会更好哦。(本篇有些内容附简略讲解)

这是上一集:模板大全(一)。

【模板】网络最大流

题目跳转

代码

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
uint8_t buf[1<<20],*p1,*p2;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
ll rd(){
    ll x=0,f=1,ch=gc();
    while(ch!=EOF&&(ch<'0'||ch>'9')){
        if(ch=='-'){
        	f=-1;
		}
        ch=gc();
    }
	if(ch==EOF){
		return x;
	}
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch-'0');
        ch=gc();
    }
    return x*f;
}
uint8_t obuf[1<<20],*op=obuf;
#define fls() (fwrite(obuf,1,op-obuf,stdout),op=obuf,0)
#define pc(ch) (op==obuf+(1<<20)?fls():0,*op++=(ch))
void write(ll x){
    if(x<0){
		x=-x;
		pc('-');
	}
    if(x>9){
    	write(x/10);
	}
    pc(x%10+'0');
}
const ll maxm=5e3+5,maxn=2e2+5,inf=1e18;
ll n,m,s,t,ecnt=1,head[maxn],now[maxn],dis[maxn],ans,mp[maxn][maxn];
queue<ll> q;
struct node{
	ll nxt,to,val;
}e[maxm<<1];
void adde(ll u,ll v,ll w){
	e[++ecnt].nxt=head[u];
	e[ecnt].to=v;
	e[ecnt].val=w;
	head[u]=ecnt;
}
bool bfs(){
	q=queue<ll>();
	for(ll i=1;i<=n;i++){
		dis[i]=inf;
	}
	q.push(s);
	dis[s]=0;
	now[s]=head[s];
	while(!q.empty()){
		ll x=q.front();
		q.pop();
		for(ll i=head[x];i;i=e[i].nxt){
			ll y=e[i].to;
			if(e[i].val>0&&dis[y]==inf){
				now[y]=head[y];
				dis[y]=dis[x]+1;
				if(y==t){
					return true;
				}
				q.push(y);
			}
		}
	}
	return false;
}
ll dfs(ll x,ll sum){
	if(x==t){
		return sum;
	}
	ll k,res=0;
	for(ll i=now[x];i&&sum;i=e[i].nxt){
		now[x]=i;
		ll y=e[i].to;
		if(e[i].val>0&&dis[y]==dis[x]+1){
			k=dfs(y,min(sum,e[i].val));
			if(k==0){
				dis[y]=inf;
			}else{
				e[i].val-=k;
				e[i^1].val+=k;
				res+=k;
				sum-=k;
			}
		}
	}
	return res;
}
int main(){
	n=rd();
	m=rd();
	s=rd();
	t=rd();
	for(ll i=1;i<=m;i++){
		ll u=rd(),v=rd(),w=rd();
		mp[u][v]+=w;
	}
	for(ll i=28;i>=0;i-=4){
		for(ll u=1;u<=n;u++){
			for(ll v=u+1;v<=n;v++){
				if((mp[u][v]>0||mp[v][u]>0)&&(mp[u][v]>=(1<<i)||mp[v][u]>=(1<<i))){
					adde(u,v,mp[u][v]);
					adde(v,u,mp[v][u]);
					mp[u][v]=0;
					mp[v][u]=0;
				}
			}
		}
		while(bfs()){
			ans+=dfs(s,inf);
		}
	}
	write(ans);
    fls();
    return 0;
}

【模板】最大流 加强版 / 预流推进

题目跳转

代码

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
typedef int ll;
uint8_t buf[1<<20],*p1,*p2;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
ll rd(){
    ll x=0,f=1,ch=gc();
    while(ch!=EOF&&(ch<'0'||ch>'9')){
        if(ch=='-'){
        	f=-1;
		}
        ch=gc();
    }
	if(ch==EOF){
		return x;
	}
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch-'0');
        ch=gc();
    }
    return x*f;
}
uint8_t obuf[1<<20],*op=obuf;
#define fls() (fwrite(obuf,1,op-obuf,stdout),op=obuf,0)
#define pc(ch) (op==obuf+(1<<20)?fls():0,*op++=(ch))
void write(ll x){
    if(x<0){
		x=-x;
		pc('-');
	}
    if(x>9){
    	write(x/10);
	}
    pc(x%10+'0');
}
const ll maxm=1.2e5+5,maxn=1.2e3+5,inf=1e9;
ll n,m,s,t,ecnt=1,head[maxn],now[maxn],dis[maxn],ans,mp[maxn][maxn];
queue<ll> q;
struct node{
	ll nxt,to,val;
}e[maxm<<1];
void adde(ll u,ll v,ll w){
	e[++ecnt].nxt=head[u];
	e[ecnt].to=v;
	e[ecnt].val=w;
	head[u]=ecnt;
}
bool bfs(){
	q=queue<ll>();
	for(ll i=1;i<=n;i++){
		dis[i]=inf;
	}
	q.push(s);
	dis[s]=0;
	now[s]=head[s];
	while(!q.empty()){
		ll x=q.front();
		q.pop();
		for(ll i=head[x];i;i=e[i].nxt){
			ll y=e[i].to;
			if(e[i].val>0&&dis[y]==inf){
				now[y]=head[y];
				dis[y]=dis[x]+1;
				if(y==t){
					return true;
				}
				q.push(y);
			}
		}
	}
	return false;
}
ll dfs(ll x,ll sum){
	if(x==t){
		return sum;
	}
	ll k,res=0;
	for(ll i=now[x];i&&sum;i=e[i].nxt){
		now[x]=i;
		ll y=e[i].to;
		if(e[i].val>0&&dis[y]==dis[x]+1){
			k=dfs(y,min(sum,e[i].val));
			if(k==0){
				dis[y]=inf;
			}else{
				e[i].val-=k;
				e[i^1].val+=k;
				res+=k;
				sum-=k;
			}
		}
	}
	return res;
}
int main(){
	n=rd();
	m=rd();
	s=rd();
	t=rd();
	for(ll i=1;i<=m;i++){
		ll u=rd(),v=rd(),w=rd();
		mp[u][v]+=w;
	}
	for(ll i=28;i>=0;i-=4){
		for(ll u=1;u<=n;u++){
			for(ll v=u+1;v<=n;v++){
				if((mp[u][v]>0||mp[v][u]>0)&&(mp[u][v]>=(1<<i)||mp[v][u]>=(1<<i))){
					adde(u,v,mp[u][v]);
					adde(v,u,mp[v][u]);
					mp[u][v]=0;
					mp[v][u]=0;
				}
			}
		}
		while(bfs()){
			ans+=dfs(s,inf);
		}
	}
	write(ans);
    fls();
    return 0;
}

【模板】二分图最大匹配

题目跳转

代码

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
typedef int ll;
uint8_t buf[1<<20],*p1,*p2;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
ll rd(){
    ll x=0,f=1,ch=gc();
    while(ch!=EOF&&(ch<'0'||ch>'9')){
        if(ch=='-'){
        	f=-1;
		}
        ch=gc();
    }
	if(ch==EOF){
		return x;
	}
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch-'0');
        ch=gc();
    }
    return x*f;
}
uint8_t obuf[1<<20],*op=obuf;
#define fls() (fwrite(obuf,1,op-obuf,stdout),op=obuf,0)
#define pc(ch) (op==obuf+(1<<20)?fls():0,*op++=(ch))
void write(ll x){
    if(x<0){
		x=-x;
		pc('-');
	}
    if(x>9){
    	write(x/10);
	}
    pc(x%10+'0');
}
const ll maxm=5.1e4+5,maxn=1e3+5,inf=1e9;
ll n,m,E,s,t,ecnt=1,head[maxn],now[maxn],dis[maxn],ans;
queue<ll> q;
struct node{
	ll nxt,to,val;
}e[maxm<<1];
void adde(ll u,ll v,ll w){
	e[++ecnt].nxt=head[u];
	e[ecnt].to=v;
	e[ecnt].val=w;
	head[u]=ecnt;
	e[++ecnt].nxt=head[v];
	e[ecnt].to=u;
	e[ecnt].val=0;
	head[v]=ecnt;
}
bool bfs(){
	q=queue<ll>();
	for(ll i=0;i<=t;i++){
		dis[i]=inf;
	}
	q.push(s);
	dis[s]=0;
	now[s]=head[s];
	while(!q.empty()){
		ll x=q.front();
		q.pop();
		for(ll i=head[x];i;i=e[i].nxt){
			ll y=e[i].to;
			if(e[i].val>0&&dis[y]==inf){
				now[y]=head[y];
				dis[y]=dis[x]+1;
				if(y==t){
					return true;
				}
				q.push(y);
			}
		}
	}
	return false;
}
ll dfs(ll x,ll sum){
	if(x==t){
		return sum;
	}
	ll k,res=0;
	for(ll i=now[x];i&&sum;i=e[i].nxt){
		now[x]=i;
		ll y=e[i].to;
		if(e[i].val>0&&dis[y]==dis[x]+1){
			k=dfs(y,min(sum,e[i].val));
			if(k==0){
				dis[y]=inf;
			}else{
				e[i].val-=k;
				e[i^1].val+=k;
				sum-=k;
				res+=k;
			}
		}
	}
	return res;
}
int main(){
	n=rd();
	m=rd();
	E=rd();
	t=n+m+1;
	for(ll i=1;i<=n;i++){
		adde(s,i,1);
	}
	for(ll i=1;i<=m;i++){
		adde(n+i,t,1);
	}
	for(ll i=1;i<=E;i++){
		ll u=rd(),v=rd();
		adde(u,n+v,1);
	}
	while(bfs()){
		ans+=dfs(s,inf);
	}
	write(ans);
    fls();
    return 0;
}

【模板】最小费用最大流

题目跳转

先跑一边 Spfa,再把正常网络流的 bfs 改成 dijkstra,dfs正常写。

代码

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
typedef int ll;
typedef pair<ll,ll> pll;
uint8_t buf[1<<20],*p1,*p2;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
ll rd(){
    ll x=0,f=1,ch=gc();
    while(ch!=EOF&&(ch<'0'||ch>'9')){
        if(ch=='-'){
        	f=-1;
		}
        ch=gc();
    }
	if(ch==EOF){
		return x;
	}
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch-'0');
        ch=gc();
    }
    return x*f;
}
uint8_t obuf[1<<20],*op=obuf;
#define fls() (fwrite(obuf,1,op-obuf,stdout),op=obuf,0)
#define pc(ch) (op==obuf+(1<<20)?fls():0,*op++=(ch))
void write(ll x){
    if(x<0){
		x=-x;
		pc('-');
	}
    if(x>9){
    	write(x/10);
	}
    pc(x%10+'0');
}
const ll maxn=5e3+5,maxm=5e4+5,inf=1e9;
ll n,m,s,t,ecnt=1,head[maxn],dis[maxn],now[maxn],h[maxn],ans1,ans2;
bool vis[maxn];
queue<ll> q;
priority_queue<pll,vector<pll>,greater<pll>> pq;
struct node{
	ll nxt,to,lim,val;
}e[maxm<<1];
void adde(ll u,ll v,ll w,ll c){
	e[++ecnt].nxt=head[u];
	e[ecnt].to=v;
	e[ecnt].lim=w;
	e[ecnt].val=c;
	head[u]=ecnt;
	e[++ecnt].nxt=head[v];
	e[ecnt].to=u;
	e[ecnt].lim=0;
	e[ecnt].val=-c;
	head[v]=ecnt;
}
void spfa(){
	for(ll i=0;i<=n;i++){
		h[i]=inf;
	}
	q.push(s);
	vis[s]=true;
	h[s]=0;
	while(!q.empty()){
		ll x=q.front();
		q.pop();
		vis[x]=false;
		for(ll i=head[x];i;i=e[i].nxt){
			ll y=e[i].to;
			if(e[i].lim>0&&h[y]>h[x]+e[i].val){
				h[y]=h[x]+e[i].val;
				if(!vis[y]){
					vis[y]=true;
					q.push(y);
				}
			}
		}
	}
}
bool dijkstra(){
	for(ll i=0;i<=n;i++){
		vis[i]=false;
		dis[i]=inf;
	}
	pq=priority_queue<pll,vector<pll>,greater<pll>>();
	pq.push({0,s});
	dis[s]=0;
	while(!pq.empty()){
		ll x=pq.top().second;
		pq.pop();
		if(!vis[x]){
			vis[x]=true;
			for(ll i=head[x];i;i=e[i].nxt){
				ll y=e[i].to;
				if(e[i].lim>0&&dis[y]>dis[x]+e[i].val+h[x]-h[y]){
					dis[y]=dis[x]+e[i].val+h[x]-h[y];
					pq.push({dis[y],y});
				}
			}
		}
	}
	return dis[t]!=inf;
}
ll dfs(ll x,ll sum){
	if(x==t){
		return sum;
	}
	ll res=0,k;
	vis[x]=true;
	for(ll i=now[x];i&&sum;i=e[i].nxt){
		now[x]=i;
		ll y=e[i].to;
		if(e[i].lim>0&&!vis[y]&&h[y]==h[x]+e[i].val){
			k=dfs(y,min(sum,e[i].lim));
			e[i].lim-=k;
			e[i^1].lim+=k;
			sum-=k;
			res+=k;
			ans2+=k*e[i].val;
		}
	}
	vis[x]=false;
	return res;
}
int main(){
	n=rd();
	m=rd();
	s=rd();
	t=rd();
	for(ll i=1;i<=m;i++){
		ll u=rd(),v=rd(),w=rd(),c=rd();
		adde(u,v,w,c);
	}
	spfa();
	while(dijkstra()){
		for(ll i=0;i<=n;i++){
			vis[i]=false;
			now[i]=head[i];
			if(dis[i]!=inf){
				h[i]+=dis[i];
			}
		}
		ans1+=dfs(s,inf);
	}
	write(ans1);
	pc(' ');
	write(ans2);
    fls();
    return 0;
}

【模板】无源汇上下界可行流

题目跳转

讲解。

先统计下界网络中每个点的流出量和流入量,称 \(\text{下界净流量} = \text{流入量} - \text{流出量}\)。

下界净流量大于 \(0\) 的连源点 \(s\),容量为其下界净流量;小于 \(0\) 的连汇点,容量为其下界净流量的绝对值。

跑 \(s\) 到 \(t\) 的最大流,如果所有附加边都满流,就代表所有点的流量守恒都被满足,也就是说原网络有可行流;如果有附加边没有满流,那么原图就没有可行流。

再跑差值网络,将差值网络跑出来的流量加到下界上,就是一组可行流了。

代码

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
typedef int ll;
uint8_t buf[1<<20],*p1,*p2;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
ll rd(){
    ll x=0,f=1,ch=gc();
    while(ch!=EOF&&(ch<'0'||ch>'9')){
        if(ch=='-'){
        	f=-1;
		}
        ch=gc();
    }
	if(ch==EOF){
		return x;
	}
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch-'0');
        ch=gc();
    }
    return x*f;
}
const ll maxn=1e3+5,maxm=1.1e4+5,inf=1e9;
ll n,m,s,t,ecnt=1,dis[maxn],now[maxn],head[maxn],lo[maxm],d[maxn],ans,cnt;
queue<ll> q;
struct node{
	ll nxt,to,val;
}e[maxm<<1];
void adde(ll u,ll v,ll w){
	e[++ecnt].nxt=head[u];
	e[ecnt].to=v;
	e[ecnt].val=w;
	head[u]=ecnt;
	e[++ecnt].nxt=head[v];
	e[ecnt].to=u;
	e[ecnt].val=0;
	head[v]=ecnt;
}
bool bfs(){
	q=queue<ll>();
	for(ll i=0;i<=t;i++){
		dis[i]=inf;
	}
	q.push(s);
	dis[s]=0;
	now[s]=head[s];
	while(!q.empty()){
		ll x=q.front();
		q.pop();
		for(ll i=head[x];i;i=e[i].nxt){
			ll y=e[i].to;
			if(e[i].val>0&&dis[y]==inf){
				dis[y]=dis[x]+1;
				now[y]=head[y];
				if(y==t){
					return true;
				}
				q.push(y);
			}
		}
	}
	return false;
}
ll dfs(ll x,ll sum){
	if(x==t){
		return sum;
	}
	ll k,res=0;
	for(ll i=now[x];i&&sum;i=e[i].nxt){
		now[x]=i;
		ll y=e[i].to;
		if(e[i].val>0&&dis[y]==dis[x]+1){
			k=dfs(y,min(sum,e[i].val));
			if(k==0){
				dis[y]=inf;
			}else{
				e[i].val-=k;
				e[i^1].val+=k;
				sum-=k;
				res+=k;
			}
		}
	}
	return res;
}
int main(){
	n=rd();
	m=rd();
	t=n+1;
	for(ll i=1;i<=m;i++){
		ll u=rd(),v=rd(),hi;
		lo[i]=rd();
        hi=rd();
		d[u]-=lo[i];
		d[v]+=lo[i];
		adde(u,v,hi-lo[i]);
	}
	for(ll i=1;i<=n;i++){
		if(d[i]>0){
			adde(s,i,d[i]);
			cnt+=d[i];
		}else if(d[i]<0){
			adde(i,t,-d[i]);
		}
	}
	while(bfs()){
		ans+=dfs(s,inf);
	}
	if(ans!=cnt){
		cout<<"No";
	}else{
		cout<<"Yes\n";
		for(ll i=1;i<=m;i++){
			cout<<lo[i]+e[(i<<1)+1].val<<"\n";
		}
	}
    return 0;
}

【模板】有源汇上下界最大流

题目跳转

讲解。

先新建一条从 \(t\) 到 \(s\),限制为 \(0,+\\infty\) 的边。

接下来跑无源汇上下界可行流,注意原图源汇点 \(s\) 和 \(t\) 与附加源汇点 \(s′\) 和 \(t′\) 不同。

将新建的 \((t,s)\) 这条边删去。

然后用原来的源汇在残量网络求解一次最大流,加上可行流即为答案。

代码

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
typedef int ll;
uint8_t buf[1<<20],*p1,*p2;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
ll rd(){
    ll x=0,f=1,ch=gc();
    while(ch!=EOF&&(ch<'0'||ch>'9')){
        if(ch=='-'){
        	f=-1;
		}
        ch=gc();
    }
	if(ch==EOF){
		return x;
	}
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch-'0');
        ch=gc();
    }
    return x*f;
}
uint8_t obuf[1<<20],*op=obuf;
#define fls() (fwrite(obuf,1,op-obuf,stdout),op=obuf,0)
#define pc(ch) (op==obuf+(1<<20)?fls():0,*op++=(ch))
void write(ll x){
    if(x<0){
		x=-x;
		pc('-');
	}
    if(x>9){
    	write(x/10);
	}
    pc(x%10+'0');
}
const ll maxn=1e3+5,maxm=1.1e4+5,inf=1e9;
ll n,m,ss,tt,s,t,ecnt=1,head[maxn],now[maxn],dis[maxn],lo[maxm],cnt,d[maxn],ans,pos;
queue<ll> q;
struct node{
	ll nxt,to,val;
}e[maxm<<1];
void adde(ll u,ll v,ll w){
	e[++ecnt].nxt=head[u];
	e[ecnt].to=v;
	e[ecnt].val=w;
	head[u]=ecnt;
	e[++ecnt].nxt=head[v];
	e[ecnt].to=u;
	e[ecnt].val=0;
	head[v]=ecnt;
}
bool bfs(){
	q=queue<ll>();
	for(ll i=0;i<=n+1;i++){
		dis[i]=inf;
	}
	q.push(s);
	dis[s]=0;
	now[s]=head[s];
	while(!q.empty()){
		ll x=q.front();
		q.pop();
		for(ll i=head[x];i;i=e[i].nxt){
			ll y=e[i].to;
			if(e[i].val>0&&dis[y]==inf){
				dis[y]=dis[x]+1;
				now[y]=head[y];
				if(y==t){
					return true;
				}
				q.push(y);
			}
		}
	}
	return false;
}
ll dfs(ll x,ll sum){
	if(x==t){
		return sum;
	}
	ll k,res=0;
	for(ll i=now[x];i&&sum;i=e[i].nxt){
		now[x]=i;
		ll y=e[i].to;
		if(e[i].val>0&&dis[y]==dis[x]+1){
			k=dfs(y,min(sum,e[i].val));
			if(k==0){
				dis[y]=inf;
			}else{
				e[i].val-=k;
				e[i^1].val+=k;
				sum-=k;
				res+=k;
			}
		}
	}
	return res;
}
int main(){
	n=rd();
	m=rd();
	ss=rd();
	tt=rd();
	t=n+1;
	for(ll i=1;i<=m;i++){
		ll u=rd(),v=rd(),hi;
		lo[i]=rd();
		hi=rd();
		d[u]-=lo[i];
		d[v]+=lo[i];
		adde(u,v,hi-lo[i]);
	}
	for(ll i=1;i<=n;i++){
		if(d[i]>0){
			adde(s,i,d[i]);
			cnt+=d[i];
		}else if(d[i]<0){
			adde(i,t,-d[i]);
		}
	}
	adde(tt,ss,inf);
	while(bfs()){
		ans+=dfs(s,inf);
	}
	if(ans!=cnt){
		pc('N');
	}else{
		ans=e[ecnt].val;
		e[ecnt].val=e[ecnt^1].val=0;
		s=ss,t=tt;
		while(bfs()){
			ans+=dfs(s,inf);
		}
		write(ans);
	}
    fls();
    return 0;
}

【模板】有源汇上下界最小流

题目跳转

讲解。

先新建一条从 \(t\) 到 \(s\),限制为 \(0,+\\infty\) 的边。

接下来跑无源汇上下界可行流,注意原图源汇点 \(s\) 和 \(t\) 与附加源汇点 \(s′\) 和 \(t′\) 不同。

将新建的 \((t,s)\) 这条边删去。

然后用原来的源汇在残量网络中从 \(t\) 到 \(s\) 求解一次最大流。

可行流减去残量网络中从 \(t\) 到 \(s\) 的最大流即为答案。

代码

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
typedef int ll;
uint8_t buf[1<<20],*p1,*p2;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
ll rd(){
    ll x=0,f=1,ch=gc();
    while(ch!=EOF&&(ch<'0'||ch>'9')){
        if(ch=='-'){
        	f=-1;
		}
        ch=gc();
    }
	if(ch==EOF){
		return x;
	}
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch-'0');
        ch=gc();
    }
    return x*f;
}
uint8_t obuf[1<<20],*op=obuf;
#define fls() (fwrite(obuf,1,op-obuf,stdout),op=obuf,0)
#define pc(ch) (op==obuf+(1<<20)?fls():0,*op++=(ch))
void write(ll x){
    if(x<0){
		x=-x;
		pc('-');
	}
    if(x>9){
    	write(x/10);
	}
    pc(x%10+'0');
}
const ll maxn=1e3+5,maxm=1.1e4+5,inf=1e9;
ll n,m,ss,tt,s,t,ecnt=1,head[maxn],now[maxn],dis[maxn],lo[maxm],cnt,d[maxn],ans,pos;
queue<ll> q;
struct node{
	ll nxt,to,val;
}e[maxm<<1];
void adde(ll u,ll v,ll w){
	e[++ecnt].nxt=head[u];
	e[ecnt].to=v;
	e[ecnt].val=w;
	head[u]=ecnt;
	e[++ecnt].nxt=head[v];
	e[ecnt].to=u;
	e[ecnt].val=0;
	head[v]=ecnt;
}
bool bfs(){
	q=queue<ll>();
	for(ll i=0;i<=n+1;i++){
		dis[i]=inf;
	}
	q.push(s);
	dis[s]=0;
	now[s]=head[s];
	while(!q.empty()){
		ll x=q.front();
		q.pop();
		for(ll i=head[x];i;i=e[i].nxt){
			ll y=e[i].to;
			if(e[i].val>0&&dis[y]==inf){
				dis[y]=dis[x]+1;
				now[y]=head[y];
				if(y==t){
					return true;
				}
				q.push(y);
			}
		}
	}
	return false;
}
ll dfs(ll x,ll sum){
	if(x==t){
		return sum;
	}
	ll k,res=0;
	for(ll i=now[x];i&&sum;i=e[i].nxt){
		now[x]=i;
		ll y=e[i].to;
		if(e[i].val>0&&dis[y]==dis[x]+1){
			k=dfs(y,min(sum,e[i].val));
			if(k==0){
				dis[y]=inf;
			}else{
				e[i].val-=k;
				e[i^1].val+=k;
				sum-=k;
				res+=k;
			}
		}
	}
	return res;
}
int main(){
	n=rd();
	m=rd();
	ss=rd();
	tt=rd();
	t=n+1;
	for(ll i=1;i<=m;i++){
		ll u=rd(),v=rd(),hi;
		lo[i]=rd();
		hi=rd();
		d[u]-=lo[i];
		d[v]+=lo[i];
		adde(u,v,hi-lo[i]);
	}
	for(ll i=1;i<=n;i++){
		if(d[i]>0){
			adde(s,i,d[i]);
			cnt+=d[i];
		}else if(d[i]<0){
			adde(i,t,-d[i]);
		}
	}
	adde(tt,ss,inf);
	while(bfs()){
		ans+=dfs(s,inf);
	}
	if(ans!=cnt){
		pc('N');
	}else{
		ans=e[ecnt].val;
		e[ecnt].val=e[ecnt^1].val=0;
		s=tt,t=ss;
		while(bfs()){
			ans-=dfs(s,inf);
		}
		write(ans);
	}
    fls();
    return 0;
}

【模板】有负圈的费用流

题目跳转

  • 对于所有负权边,建下界为 \(0\)、上界为 \(f\)、费用为 \(−v\) 的边 \((y,x)\);同时建一条虚拟边:上界为 \(f\)、下界为\(f\),按照上下界网络流套路统计每个点入流量和出流量的差(设为 \(w\))。

  • 对于所有正权边,建下界为 \(0\)、上界为 \(f\)、费用为 \(v\) 的边 \((x,y)\)。

  • 建立超级源点 \(S\) 和 超级汇点 \(T\)。若 \(w_i\gt 0\):加入容量为 \(w_i\) 的边 \((S,i)\);\(w_i\lt 0\):加入容量为 \(−w_i\) 的边 \((i,T)\)。

  • 加入容量为 \(inf\) 的边 \((t,s)\),在图上跑以 \(S\) 为源点,\(T\) 为汇点的最小费用最大流(求解有源汇最小费用可行流)。将可行流流量(最后加入的边 (t,s) 的流量)计入答案流量,将此次最小费用最大流的费用计入答案费用。

  • 撤去最后加入的边 \((t,s)\)。跑一次以 \(s\) 为源点,\(t\) 为汇点的最小费用最大流。将此次最小费用最大流的流量和费用计入答案流量和答案费用。

答案流量为可行流流量与最小费用最大流流量之和,答案费用为强制满流的费用、可行流的费用与最小费用最大流的费用之和。

代码

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
typedef int ll;
typedef pair<ll,ll> pll;
uint8_t buf[1<<20],*p1,*p2;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
ll rd(){
    ll x=0,f=1,ch=gc();
    while(ch!=EOF&&(ch<'0'||ch>'9')){
        if(ch=='-'){
        	f=-1;
		}
        ch=gc();
    }
	if(ch==EOF){
		return x;
	}
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch-'0');
        ch=gc();
    }
    return x*f;
}
uint8_t obuf[1<<20],*op=obuf;
#define fls() (fwrite(obuf,1,op-obuf,stdout),op=obuf,0)
#define pc(ch) (op==obuf+(1<<20)?fls():0,*op++=(ch))
void write(ll x){
    if(x<0){
		x=-x;
		pc('-');
	}
    if(x>9){
    	write(x/10);
	}
    pc(x%10+'0');
}
const ll maxn=2e2+5,maxm=1.04e4+5,inf=1e9;
ll n,m,ss,tt,s,t,ecnt=1,head[maxn],now[maxn],dis[maxn],h[maxn],d[maxn],res1,res2,ans1,ans2;
bool vis[maxn];
queue<ll> q;
priority_queue<pll,vector<pll>,greater<pll>> pq;
struct node{
	ll nxt,to,lim,val;
}e[maxm<<1];
void adde(ll u,ll v,ll w,ll c){
	e[++ecnt].nxt=head[u];
	e[ecnt].to=v;
	e[ecnt].lim=w;
	e[ecnt].val=c;
	head[u]=ecnt;
	e[++ecnt].nxt=head[v];
	e[ecnt].to=u;
	e[ecnt].lim=0;
	e[ecnt].val=-c;
	head[v]=ecnt;
}
void spfa(){
	for(ll i=0;i<=n+1;i++){
		h[i]=inf;
	}
	q.push(s);
	vis[s]=true;
	h[s]=0;
	while(!q.empty()){
		ll x=q.front();
		q.pop();
		vis[x]=false;
		for(ll i=head[x];i;i=e[i].nxt){
			ll y=e[i].to;
			if(e[i].lim>0&&h[y]>h[x]+e[i].val){
				h[y]=h[x]+e[i].val;
				if(!vis[y]){
					vis[y]=true;
					q.push(y);
				}
			}
		}
	}
}
bool dijkstra(){
	for(ll i=0;i<=n+1;i++){
		vis[i]=false;
		dis[i]=inf;
	}
	pq=priority_queue<pll,vector<pll>,greater<pll>>();
	pq.push({0,s});
	dis[s]=0;
	while(!pq.empty()){
		ll x=pq.top().second;
		pq.pop();
		if(!vis[x]){
			vis[x]=true;
			for(ll i=head[x];i;i=e[i].nxt){
				ll y=e[i].to;
				if(e[i].lim>0&&dis[y]>dis[x]+e[i].val+h[x]-h[y]){
					dis[y]=dis[x]+e[i].val+h[x]-h[y];
					pq.push({dis[y],y});
				}
			}
		}
	}
	return dis[t]!=inf;
}
ll dfs(ll x,ll sum){
	if(x==t){
		return sum;
	}
	ll res=0,k;
	vis[x]=true;
	for(ll i=now[x];i&&sum;i=e[i].nxt){
		now[x]=i;
		ll y=e[i].to;
		if(e[i].lim>0&&!vis[y]&&h[y]==h[x]+e[i].val){
			k=dfs(y,min(sum,e[i].lim));
			e[i].lim-=k;
			e[i^1].lim+=k;
			sum-=k;
			res+=k;
			res2+=k*e[i].val;
		}
	}
	vis[x]=false;
	return res;
}
void dinic(){
	res1=res2=0;
	spfa();
	while(dijkstra()){
		for(ll i=0;i<=n+1;i++){
			vis[i]=false;
			now[i]=head[i];
			if(dis[i]!=inf){
				h[i]+=dis[i];
			}
		}
		res1+=dfs(s,inf);
	}
}
int main(){
	n=rd();
	m=rd();
	ss=rd();
	tt=rd();
	t=n+1;
	for(ll i=1;i<=m;i++){
		ll u=rd(),v=rd(),f=rd(),c=rd();
		if(f){
			if(c<0){
				d[u]-=f;
				d[v]+=f;
				ans2+=f*c;
				adde(v,u,f,-c);
			}else{
				adde(u,v,f,c);
			}
		}
	}
	for(ll i=1;i<=n;i++){
		if(d[i]>0){
			adde(s,i,d[i],0);
		}else if(d[i]<0){
			adde(i,t,-d[i],0);
		}
	}
	adde(tt,ss,inf,0);
	dinic();
	ans1=e[ecnt].lim;
	ans2+=res2;
	e[ecnt].lim=e[ecnt^1].lim=0;
	s=ss;
	t=tt;
	dinic();
	ans1+=res1;
	ans2+=res2;
	write(ans1);
	pc(' ');
	write(ans2);
    fls();
    return 0;
}

【模板】二分图最大权完美匹配

题目跳转

代码

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
typedef int ll;
typedef long long lll;
uint8_t buf[1<<20],*p1,*p2;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
inline ll rd(){
    ll x=0,f=1,ch=gc();
    while(ch!=EOF&&(ch<'0'||ch>'9')){
        if(ch=='-'){
        	f=-1;
		}
        ch=gc();
    }
	if(ch==EOF){
		return x;
	}
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch-'0');
        ch=gc();
    }
    return x*f;
}
uint8_t obuf[1<<20],*op=obuf;
#define fls() (fwrite(obuf,1,op-obuf,stdout),op=obuf,0)
#define pc(ch) (op==obuf+(1<<20)?fls():0,*op++=(ch))
inline void write1(lll x){
    if(x<0){
		x=-x;
		pc('-');
	}
    if(x>9){
    	write1(x/10);
	}
    pc(x%10+'0');
}
inline void write2(ll x){
    if(x<0){
		x=-x;
		pc('-');
	}
    if(x>9){
    	write2(x/10);
	}
    pc(x%10+'0');
}
const ll maxn=1e3+5,maxm=2.51e5+5;
const lll inf=1e18;
ll n,s,t,m,head[maxn],cost[maxn][maxn],match[maxn>>1],vec[maxn],pre[maxn];
bool vis[maxn],flow[maxn][maxn];
lll h[maxn],dis[maxn],ans2;
vector<ll> e[maxn];
queue<ll> q;
inline void adde(ll u,ll v,ll c){
	flow[u][v]=1;
	cost[u][v]=c;
	e[u].emplace_back(v);
	flow[v][u]=0;
	cost[v][u]=-c;
	e[v].emplace_back(u);
}
inline void spfa(){
	for(ll i=0;i<=(n<<1)+1;i++){
		h[i]=inf;
	}
	q.push(s);
	vis[s]=true;
	h[s]=0;
	while(!q.empty()){
		ll x=q.front();
		q.pop();
		vis[x]=false;
		for(ll y:e[x]){
			if(flow[x][y]&&h[y]>h[x]+cost[x][y]){
				h[y]=h[x]+cost[x][y];
				if(!vis[y]){
					vis[y]=true;
					q.push(y);
				}
			}
		}
	}
}
inline bool dijkstra(){
	for(ll i=0;i<=(n<<1)+1;i++){
		vis[i]=false;
		dis[i]=inf;
	}
	dis[s]=0;
	ll vcnt=0,x,xid;
	for(ll i=0;i<=t;i++){
		vec[++vcnt]=i;
	}
	for(ll cnt=0;cnt<=t;cnt++){
		x=xid=-1;
		for(ll j=1;j<=vcnt;j++){
			if(x==-1||dis[vec[j]]<dis[x]){
				x=vec[j];
				xid=j;
				if(dis[x]==0){
					break;
				}
			}
		}
		if(x==-1||dis[x]>=inf){
			break;
		}
		vis[x]=true;
		vec[xid]=vec[vcnt--];
		for(ll y:e[x]){
			if(flow[x][y]&&dis[y]>dis[x]+cost[x][y]+h[x]-h[y]){
				dis[y]=dis[x]+cost[x][y]+h[x]-h[y];
				pre[y]=x;
			}
		}
	}
	return dis[t]!=inf;
}
int main(){
	n=rd();
	m=rd();
	t=(n<<1)+1;
	for(ll i=1;i<=m;i++){
		ll u=rd(),v=rd(),c=rd();
		adde(u,v+n,-c);
	}
	for(ll i=1;i<=n;i++){
		adde(s,i,0);
		adde(i+n,t,0);
	}
	spfa();
	while(dijkstra()){
		for(ll i=0;i<=(n<<1)+1;i++){
			if(dis[i]!=inf){
				h[i]+=dis[i];
			}
		}
		ll now=t;
		while(now!=s){
			flow[pre[now]][now]=0;
			flow[now][pre[now]]=1;
			ans2+=cost[pre[now]][now];
			now=pre[now];
		}
	}
    for(ll i=n+1;i<=(n<<1);i++){
		for(ll j:e[i]){
			if(j!=t){
				if(flow[i][j]){
					match[i-n]=j;
					break;
				}
			}
		}
	}
	write1(-ans2);
    pc('\n');
	for(ll i=1;i<=n;i++){
		write2(match[i]);
		pc(' ');
	}
    fls();
    return 0;
}

本文是作者收集的洛谷上的模板题以及自己的代码,将会持续更新,你的点赞与关注将是我更新的最大动力!
如果您觉得有不正确或不妥的地方,欢迎在评论区指正,感谢!
如果觉得对你有帮助,就帮忙点个赞吧!(❁´◡`❁)