跳到正文

算法·理论

[图论]网络流练习笔记-Isap算法应用

Icyfires18约 47 分钟
原文
文章信息与版本

文章信息

发布
2022年1月22日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 47 分钟
获赞
2

版本

  1. v1当前

Part 0 前言

前置知识:[图论]网络流学习笔记-Isap算法原理

基础过关:P3376 【模板】网络最大流

大多数网络流题目考察主要以网络流模型建构为解题关键,而网络流模板的编写在其次,所以在做网络流题目时,运用恰当的思想和角度尤为重要。初学者可以先拿例题练练手,熟悉并适应网络流应用题的出题方式和考察点。

例题:P2740 [USACO4.2]草地排水Drainage Ditches

模型要素:匹配关系建边,限制条件为权。

难度分配:分析难度和构造难度

Part 1 应用思想

1 - 相关结论

1.最大流最小割定理

Maximum Flow Minimum Cut Theorem:

最大流等于最小割(Max-flow=Min-cut)

在做题的过程中,我们可以通过这个定理转换思想和角度,获得易于表达和理解的解释。

2.二分图定理

最小点覆盖=最大匹配数=最大流;

最少边覆盖=点数 - 最大匹配数;

最大独立集=点数 - 最小覆盖集;

在用二分图跑网络流时,所求的答案可以根据上述定理对最大流进行转化得到,不仅仅适用于二分图,也可用于其他个别网络,视情况而定。

2 - 最大流 - 并列思想

网络流题目中,若出现求解满足并列相关关系的概念陈述,则通常用最大流的思考方式理解题意与建模方式。

在做题过程中,我们以最大流来解释网络与题目的相关含义,并且以边流量和边的连接关系来分析其对于单纯网络和题目模型所带来的价值。

一般来说这种情况的题目都较于好理解,点边与其权值的意义都表述的较为直接,比如以下几道题思考难度不大,可以先帮助参悟这种思维方式。

P2756 飞行员配对方案问题

P3701 主主树

P1231 教辅的组成

P2472 [SCOI2007]蜥蜴

当然,其中也有思考难度比较大的题目。但是由于最大流思想的普遍性(毕竟大家都是由流量问题开始学习摸清网络流的,而不是割边问题),这种题数目不多,范围也比较小,难点集中在题意的分析和转化上。

P2766 最长不下降子序列问题

P2764 最小路径覆盖问题

P2172 [国家集训队]部落战争

总的来说,这类思想的题一般题目特征比较鲜明,或是与其他算法思想结合考察,学习过程中只需要多练即可。

3 - 最小割 - 对立思想

网络流题目中,若出现求解满足对立相关关系的概念陈述,则通常用最小割的思考方式理解题意与建模方式。

在做题过程中,我们以最小割来解释网络与题目的相关含义,并且以割边权和边的连接关系来分析其对于单纯网络和题目模型所带来的价值。

从某种意义上来说,虽然最大流和最小割在本质上是一样的,但是概念上的差别依然能使最小割算作是最大流的扩展层面,这也导致这类思想的题目在分析难度和构造难度上均更上一个台阶(尤指构造难度)。

对立思想的体现正是我们从最小割角度思考的原因。一般这种题多可分为两方性质阵营,而点与点间的矛盾或互斥等关系就是连边的要素,比如说以下几题就是。

P2598 [ZJOI2009]狼和羊的故事

P2057 [SHOI2007]善意的投票 / [JLOI2010]冠军调查

P4001 [ICPC-Beijing 2006]狼抓兔子

当然上述题目的构图还算比较简单,但下述题目就比较难了,这也正是构造难度在最小割题目中的体现。

P2774 方格取数问题

P4304 [TJOI2013]攻击装置

P1646 [国家集训队]happiness

P4313 文理分科

同时,最小割的扩展性相对于最大流的局限性而言,出题难点和范围就广难的多了。不仅仅在于上述几题的复杂构造外,还可在图论综合性理解和其他方面上制造难点。

P5934 [清华集训2012]最小生成树

P5039 [SHOI2010]最小生成树

总而言之,做这类思想的题目时,更要注意分析与构造能力的理解和训练上,提升自身思维的灵敏度和做题手感。

4 - 残余网络 / 集合

有的时候题目需要求解的并不是流量网络或是割边集合相关,反而是在网络流扫荡后留下的图当中寻找。

通常情况中只需同概念性质下的总量减去网络流过程的结果便是答案,例如以下几题。

P2472 [SCOI2007]蜥蜴

P2774 方格取数问题

P4304 [TJOI2013]攻击装置

P2764 最小路径覆盖问题

P2172 [国家集训队]部落战争

P1646 [国家集训队]happiness

P4313 文理分科

Part 2 建模方式

1 - 朴素构图(超源+超汇)

意为在题意所给原图上直接进行建模,无需多余操作。

该类题目主要以考察思路为主,故需深刻理解网络流的本质和应用思想,也可能与其它图论算法结构相关联。

应用类的网络流题目,很多时候并没有明确的源点和汇点,或是将同性质的类型点都视作源点或汇点。这时,我们需要加超级源点和超级汇点,并使所有在原图中的可行源点和汇点都与之连接。然后,我们从超级源点开始跑最大流,直到超级汇点再无增广流量为止,以此来将网络构建完整。

可结合朴素构图的应用实例理解其应用背景、范围及特点。

例题(下文有解析)

P2598 [ZJOI2009]狼和羊的故事

P4001 [ICPC-Beijing 2006]狼抓兔子

P5934 [清华集训2012]最小生成树

P5039 [SHOI2010]最小生成树

P2057 [SHOI2007]善意的投票 / [JLOI2010]冠军调查

2 - 分层

分层思想在网络流题目中非常普遍。当图可被分为若干由同性质的类型点组成的点集时,通常可以利用分层建模。因为在解决问题的过程中,这些点是互相平行且相等但又相对独立且一体的。我们将这些点分为同一层,而其他点则不和这些放一起。一般来说,这些点之间并没有必然联系,同层的点一般也不会连关系边。

就这样,我们可以根据题意分多层构建模型网络,并在不同层的点之间连接有向边。要注意的是,不同层是有先后次序的,因此连边时要注意有向边的方向和层次间的联系。

平时,我们最常遇到的分层构图是二分图,还带有超源和超汇。通常,两层图和各自的源汇之间的边表示与点的个体有关的值,而两层图之间的边则与点间限制条件有关。我们在做题时,要多关注二分图中灵活的概念转化,多换角度和方向思考。

可结合分层的应用实例理解其应用背景、范围及特点。

例题(下文有解析)

P2756 飞行员配对方案问题

P3701 主主树

P1231 教辅的组成

P2774 方格取数问题

P4304 [TJOI2013]攻击装置

3 - 拆点(点权)

拆点,指复制本身生成新节点,即有另一个本体。分点在概念与意义上与原点相当,通常作用在于与原点共同形成一组入点和出点,而两者区别在于与外部(其他点)的连边均为入边或出边。

拆点同时一般也构成二分图,并基于题意和二分图性质连边形成正确的模型网络。它与分层的不同之处在于:前者是实际上的复制,需新建节点,是操作层面的;后者是概念上的隔离,无需新建,是思路层面的。

拆点自连边有增加点容量限制的作用,有时我们设自连边的权为点独立意义值(即点权),来表示取用次数等相关。

可结合拆点的应用实例理解其应用背景、范围及特点。

例题(下文有解析)

P2764 最小路径覆盖问题

P1231 教辅的组成

P2472 [SCOI2007]蜥蜴

P2172 [国家集训队]部落战争

P2766 最长不下降子序列问题

4 - 合点(点集权)

当题意存在多点共同满足条件所获取的权值时,我们需要用合点的方式进行建模。

合点需要新建一个节点,并将该点与所有点集内的点进行连边。同时,该点和点集内的点与外部同性质点的连边条件和形式(方向等)相同。

值得注意的是,该点仅是作为点集权的载体,与外部连接着唯一一条值为点集权的边,而不能代表整个点集。如有第二个同点集权,则需再次新建节点连边并加以赋权。

合点在网络流题目中出现不多,在做题时我们仍需考虑实际情况,做到思路清晰。建议大家手推一下流程以及这种连边方式的原因和效果。

可结合合点的应用实例理解其应用背景、范围及特点。

例题(下文有解析)

P1646 [国家集训队]happiness

P4313 文理分科

Part 3 例题解析

注意:

1. 无向边的连接应为:

代码
add(u,v,w),add(v,u,0);add(v,u,w),add(u,v,0);

而这种连接方式是错的:

代码
add(u,v,w),add(v,u,w);

两者的意义和效果截然不同(大家可以想下为什么)。

2. 核心代码省略网络流部分,主要为建模部分。

fip是快速读入函数,adde是加复边函数;

Init_net是网络流初始化函数,Isap是网络流递归求解函数。

1 - P2740 [USACO4.2]草地排水Drainage Ditches

应用思想:最大流 - 并列思想

建模方式:朴素

模型描述:

无向边 of 流量 from 图内节点 to 图内节点 through 连接关系

核心代码:

C++
int n,m,tot,st,en;
long long ans;
int main(){
	register int i,u,v,w;
	scanf("%d%d",&m,&n);
	tot=n,st=1,en=n;
	for(i=1;i<=m;++i)
		fip(u),fip(v),
		fip(w),adde(u,v,w);
	Init_net();
	while(lab[st]<=tot)
		ans+=Isap(st,INF);
	printf("%lld",ans);
	return 0;
}

2 - P2756 飞行员配对方案问题

应用思想:最大流 - 并列思想(最大匹配数)

建模方式:分层

模型描述:

有向边 of 1 from 超源(S) to 外地节点 through 无

有向边 of 1 from 外地节点 to 本地节点 through 配对关系

有向边 of 1 from 本地节点 to 超汇(T) through 无

核心代码:

C++
int n,m,st,en,tot,ans;
int main(){
	register int i,u,v;
	scanf("%d%d",&m,&n);
	tot=n,st=++tot,en=++tot;
	for(i=1;i<=m;++i)adde(st,i);
	for(i=i;i<=n;++i)adde(i,en);
	while(true){
		fip(u),fip(v);
		if(u==-1||v==-1)break;
		adde(u,v);
	}Init_net();
	while(lab[st]<=tot)
		ans+=Isap(st);
	printf("%d\n",ans);
	for(u=1;u<=m;++u)
	for(i=beg[u];i;i=nex[i])
		if(!len[i]&&go[i]<st)
			write(u),putchar(' '),
			write(go[i]),putchar('\n');
	return 0;
}

3 - P3701 主主树

应用思想:最大流 - 并列思想(最大匹配数)

建模方式:分层

模型描述:

有向边 of HP from 超源(S) to 前者节点 through 无

有向边 of 1 from 前者节点 to 后者节点 through 胜负关系

有向边 of HP from 后者节点 to 超汇(T) through 无

核心代码:(注意回血)

C++
#define P(i,j) (i+(~-(j))*n)
const bool rel[9][9]={
	{0,0,0,0,0,0},
	{0,0,1,1,0,0},
	{0,0,0,1,1,0},
	{0,0,0,0,1,1},
	{0,1,0,0,0,1},
	{0,1,1,0,0,0}};
int a[N],b[N],hpa,hpb;
int n,m,tot,st,en,ans;
inline int Index(char c[]){
	if(c[0]=='J')return 1;
	if(c[0]=='H')return 2;
	if(c[0]=='W')return 3;
	if(c[0]=='E')return 4;
	if(c[0]=='Y')return 5;
	return 0;
}
int main(){
	register int i,j,u,v,w;
	register char str[5];
	scanf("%d%d\n",&n,&m);
	tot=n+n,st=++tot,en=++tot;
	for(i=1;i<=n;++i){
		scanf("%s",str);
		a[i]=Index(str);
		if(a[i]==5)++hpa;
	}for(i=1;i<=n;++i){
		scanf("%s",str);
		b[i]=Index(str);
		if(b[i]==5)++hpb;
	}for(i=1;i<=n;++i){
		fip(w);
		if(a[i]==1)w+=hpa;
		adde(st,P(i,1),w);
	}for(i=1;i<=n;++i){
		fip(w);
		if(b[i]==1)w+=hpb;
		adde(P(i,2),en,w);
	}for(i=1;i<=n;++i)
	for(j=1;j<=n;++j)
	if(rel[a[i]][b[j]])
		adde(P(i,1),P(j,2),1);
	Init_net(),ans=m;
	while(lab[st]<=tot&&ans>0)
		ans-=Isap(st,ans);
	printf("%lld",m-ans);
	return 0;
}

4 - P2057 [SHOI2007]善意的投票 / [JLOI2010]冠军调查

应用思想:最小割 - 对立思想

建模方式:朴素

模型描述:

有向边 of 1 from 超源(S) to 图内节点 through 是否想睡

无向边 of 1 from 图内节点 to 图内节点 through 朋友关系

有向边 of 1 from 图内节点 to 超汇(T) through 是否不睡

核心代码:

C++
int n,m,st,en,tot,ans;
int main(){
	register int i,u,v,tp;
	scanf("%d%d",&n,&m);
	tot=n,st=++tot,en=++tot;
	for(i=1;i<=n;++i){
		fip(tp);
		if(tp)adde(st,i);
		else adde(i,en);
	}for(i=1;i<=m;++i){
		fip(u),fip(v);
		adde(u,v),adde(v,u);
	}Init_net();
	while(lab[st]<=tot)
		ans+=Isap(st);
	printf("%d",ans);
   	return 0;
}

5 - P1231 教辅的组成

应用思想:最大流 - 并列思想

建模方式:分层,拆点

模型描述:

有向边 of 1 from 超源(S) to 习题节点 through 无

有向边 of 1 from 习题节点 to 书本节点1 through 对应关系

有向边 of 1 from 书本节点1 to 书本节点1 through 同点关系

有向边 of 1 from 书本节点2 to 答案节点 through 对应关系

有向边 of 1 from 答案节点 to 超汇(T) through 无

核心代码:(注意拆点)

C++
int a,b,c,n,m,st,en,ans;
inline int I(int tp,int u){
	switch(tp){
		case 1:return u;//text1
		case 2:return u+a;//text2
		case 3:return u+a+a;//practice
		case 4:return u+a+a+b;//answer
	}return 0;
}
int main(){
	register int i,u,v;
	scanf("%d%d%d",&a,&b,&c);
	n=a+a+b+c,st=++n,en=++n;
	for(i=1;i<=b;++i)
		adde(st,I(3,i));
	for(fip(m),i=1;i<=m;++i)
		fip(u),fip(v),
		adde(I(3,v),I(1,u));
	for(i=1;i<=a;++i)
		adde(I(1,i),I(2,i));
	for(fip(m),i=1;i<=m;++i)
		fip(u),fip(v),
		adde(I(2,u),I(4,v));
	for(i=1;i<=c;++i)
		adde(I(4,i),en);
	Init_net();
	while(lab[st]<=n)
		ans+=Isap(st);
	printf("%d",ans);
	return 0;
}

6 - P4001 [ICPC-Beijing 2006]狼抓兔子

应用思想:最小割 - 对立思想

建模方式:朴素

模型描述:

无向边 of 流量 from 图内节点 to 图内节点 through 连接关系

核心代码:

C++
#define P(x,y) ((~-(x))*m+(y))
int n,m,st,en,tot,ans;
int main(){
	register int i,j,u,v,w;
	scanf("%d%d",&n,&m);
	tot=en=n*m,st=1;
	for(i=1;i<=n;++i)for(j=1;j<m;++j)
		u=P(i,j),v=P(i,j+1),fip(w),
		adde(u,v,w),adde(v,u,w);
	for(i=1;i<n;++i)for(j=1;j<=m;++j)
		u=P(i,j),v=P(i+1,j),fip(w),
		adde(u,v,w),adde(v,u,w);
	for(i=1;i<n;++i)for(j=1;j<m;++j)
		u=P(i,j),v=P(i+1,j+1),fip(w),
		adde(u,v,w),adde(v,u,w);
	Init_net();
	while(lab[st]<=tot)
		ans+=Isap(st,INF);
	printf("%lld",ans);
	return 0;
}

7 - P2598 [ZJOI2009]狼和羊的故事

应用思想:最小割 - 对立思想

建模方式:朴素

模型描述:

有向边 of INF from 超源(S) to 狼节点 through 无

无向边 of 1 from 图内节点 to 图内节点 through 相邻关系

有向边 of INF from 羊节点 to 超汇(T) through 无

核心代码:

C++
#define P(x,y) ((~-(x))*m+(y))
int n,m,st,en,tot,ans;
int tp[N][N];
int main(){
	register int i,j,u,v;
	scanf("%d%d\n",&n,&m);
	tot=n*m,st=++tot,en=++tot;
	for(i=1;i<=n;++i)for(j=1;j<=m;++j)
		fip(tp[i][j]);
	for(i=1;i<=n;++i)for(j=1;j<=m;++j){
		u=P(i,j);
		if(tp[i][j]==1)adde(st,u,INF);
		if(tp[i][j]==2)adde(u,en,INF);
		if(i>1)adde(u,P(i-1,j),1);
		if(i<n)adde(u,P(i+1,j),1);
		if(j>1)adde(u,P(i,j-1),1);
		if(j<m)adde(u,P(i,j+1),1);
	}Init_net();
	while(lab[st]<=tot)
		ans+=Isap(st,INF);
	printf("%d",ans);
	return 0;
}

8 - P2774 方格取数问题

应用思想:最小割 - 对立思想(最大独立集)

建模方式:分层

模型描述:

定义满足(i+j)%2==1的点(x,y)为黑节点,满足(i+j)%2==0的点(x,y)为白节点

有向边 of 点值 from 超源(S) to 黑节点 through 无

有向边 of INF from 黑节点 to 白节点 through 相邻关系

有向边 of 点值 from 白节点 to 超汇(T) through 无

核心代码:

C++
#define P(x,y) ((~-(x))*m+(y))
int n,m,tot,st,en;
long long sum,ans;
int main(){
	register int i,j,u,v,w;
	scanf("%d%d",&n,&m);
	tot=n*m,st=++tot,en=++tot;
	for(i=1;i<=n;++i)
	for(j=1;j<=m;++j){
		u=P(i,j),fip(w),sum+=w;
		if((i+j)%2){
			adde(st,u,w);
			if(i>1)adde(u,P(i-1,j),INF);
			if(i<n)adde(u,P(i+1,j),INF);
			if(j>1)adde(u,P(i,j-1),INF);
			if(j<m)adde(u,P(i,j+1),INF);
		}else adde(u,en,w);
	}Init_net();
	while(lab[st]<=tot)
		ans+=Isap(st,INF);
	printf("%lld",sum-ans);
	return 0;
}

9 - P1646 [国家集训队]happiness

应用思想:最小割 - 对立思想

建模方式:合点

模型描述:

有向边 of 点文值 from 超源(S) to 图内节点 through 无

有向边 of 邻文值 from 超源(S) to 邻文节点 through 无

有向边 of INF from 邻文节点 to 图内节点 through 相邻关系

有向边 of INF from 图内节点 to 邻理节点 through 相邻关系

有向边 of 邻理值 from 邻理节点 to 超汇(T) through 无

有向边 of 点理值 from 图内节点 to 超汇(T) through 无

核心代码:

C++
#define P(x,y) ((~-(x))*m+(y))
int n,m,tot,st,en;
long long sum,ans;
int main(){
	register int i,j,w;
	scanf("%d%d",&n,&m);
	tot=n*m,st=++tot,en=++tot;
	for(i=1;i<=n;++i)
	for(j=1;j<=m;++j)
		fip(w),sum+=w,
		adde(st,P(i,j),w);
	for(i=1;i<=n;++i)
	for(j=1;j<=m;++j)
		fip(w),sum+=w,
		adde(P(i,j),en,w);
	for(i=1;i<n;++i)
	for(j=1;j<=m;++j)
		fip(w),sum+=w,
		adde(st,++tot,w),
		adde(tot,P(i,j),INF),
		adde(tot,P(i+1,j),INF);
	for(i=1;i<n;++i)
	for(j=1;j<=m;++j)
		fip(w),sum+=w,
		adde(++tot,en,w),
		adde(P(i,j),tot,INF),
		adde(P(i+1,j),tot,INF);
	for(i=1;i<=n;++i)
	for(j=1;j<m;++j)
		fip(w),sum+=w,
		adde(st,++tot,w),
		adde(tot,P(i,j),INF),
		adde(tot,P(i,j+1),INF);
	for(i=1;i<=n;++i)
	for(j=1;j<m;++j)
		fip(w),sum+=w,
		adde(++tot,en,w),
		adde(P(i,j),tot,INF),
		adde(P(i,j+1),tot,INF);
	Init_net();
	while(lab[st]<=tot)
		ans+=Isap(st,INF);
	printf("%lld",sum-ans);
	return 0;
}

10 - P2764 最小路径覆盖问题

应用思想:最大流 - 并列思想(最大独立集)

建模方式:拆点

模型描述:

有向边 of 1 from 超源(S) to 图内节点1 through 无

有向边 of 1 from 图内节点1 to 图内节点2 through 连接关系

有向边 of 1 from 图内节点2 to 超汇(T) through 无

核心代码:

注:fol数组在增广路回溯时记录即可

C++
int n,m,tot,st,en;
int ans,fol[N];
bool vis[N];
void dfs(int u){
    vis[u]=1;
    printf("%d ",u);
    if(fol[u])dfs(fol[u]-n);
}
int main(){
    register int i,u,v,w;
    scanf("%d%d",&n,&m);
    tot=n+n,st=++tot,en=++tot;
    for(cnt=i=1;i<=n;++i)
        adde(st,i),adde(i+n,en);
    for(i=1;i<=m;++i)
        fip(u),fip(v),
		adde(u,v+n);
    Init_net();
    while(lab[st]<=tot)
        ans+=Isap(st);
    for(i=1;i<=n;++i)
    if(!vis[i])dfs(i),
        putchar('\n');
    printf("%d",n-ans);
    return 0;
}

11 - P2472 [SCOI2007]蜥蜴

应用思想:最大流 - 并列思想

建模方式:拆点

模型描述:

有向边 of 数目 from 超源(S) to 石柱节点1 through 有无蜥蜴

有向边 of 高度 from 石柱节点1 to 石柱节点2 through 同点关系

有向边 of INF from 石柱节点2 to 石柱节点1 through 相通关系

有向边 of INF from 图内节点2 to 超汇(T) through 能否出界

核心代码:

注:此题如用 快速读入字符 写法,请按如下语句写

代码
while((ch<'0'||ch>'9')&&ch!='.'&&ch!='L')ch=getchar();

但使用如下语句会报错,原因未知

代码
while(ch==' '||ch=='\n')ch=getchar();
C++
#define P(x,y) ((~-(x))*m+(y))
#define x(i) p[i].first
#define y(i) p[i].second
int n,m,l,tot,st,en;
long long sum,ans;
char c[N][N];
pair<int,int>p[N];
inline bool disc(int& x1,int& y1,int& x2,int& y2){
	return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)<=l*l;}
inline bool limc(int& x1,int& y1){
	return x1-l<1||x1+l>n||y1-l<1||y1+l>m;}
int main(){
	register int i,j,k,w;
	scanf("%d%d%d\n",&n,&m,&l);
	for(i=1;i<=n;++i)
	for(j=1;j<=m;++j)
	if((c[i][j]=readc())!='0')
		p[++tot]={i,j};
	st=tot*2+1,en=tot*2+2;
	for(i=1;i<=tot;++i)
		adde(i,i+tot,c[x(i)][y(i)]-48);
	for(i=1;i<=tot;++i)
	for(j=1;j<i;++j)
	if(disc(x(i),y(i),x(j),y(j)))
		adde(i+tot,j,INF),
		adde(j+tot,i,INF);
	for(i=1;i<=tot;++i)
	if(limc(x(i),y(i)))
		adde(i+tot,en,INF);
	for(i=k=1;i<=n;++i)
	for(j=1;j<=m;++j)
	if(readc()=='L'){
		while(x(k)<i||y(k)<j)++k;
		adde(st,k,1),++sum;
	}tot=en;
	Init_net();
	while(lab[st]<=tot)
		ans+=Isap(st,INF);
	printf("%lld",sum-ans);
	return 0;
}

12 - P5934 [清华集训2012]最小生成树

应用思想:最小割 - 对立思想,kruskal最小生成树算法思想

建模方式:朴素

模型描述:

第一次:无向边 of 1 from 图内节点 to 图内节点 through 是否小于给定边权

第二次:无向边 of 1 from 图内节点 to 图内节点 through 是否大于给定边权

核心代码:

C++
int eu[M],ev[M],ew[M];
int n,m,l,tot,st,en,ans;
int main(){
	register int i,u,v,w;
	scanf("%d%d",&n,&m);
	for(tot=n,i=1;i<=m;++i)
		fip(eu[i]),
		fip(ev[i]),
		fip(ew[i]);
	scanf("%d%d%d",&st,&en,&l);
	for(i=1;i<=m;++i)if(ew[i]<l)
		adde(eu[i],ev[i]),
		adde(ev[i],eu[i]);
	Init_net();
	while(lab[st]<=tot)
		ans+=Isap(st);
	Init_graph();
	for(i=1;i<=m;++i)if(ew[i]>l)
		adde(eu[i],ev[i]),
		adde(ev[i],eu[i]);
	Init_net();     
	while(lab[st]<=tot)
		ans+=Isap(st);
	printf("%d",ans);
	return 0;
}

13 - P2766 最长不下降子序列问题

应用思想:最大流 - 并列思想,线性动态规划思想

建模方式:拆点

模型描述:

有向边 of 1 from 超源(S) to 序列节点1 through 起点关系

有向边 of 1 from 序列节点1 to 序列节点2 through 同点关系

有向边 of 1 from 序列节点2 to 序列节点1 through 衔接关系

有向边 of 1 from 序列节点2 to 超汇(T) through 终点关系

(3) 修改边S—>1(1),1(1)—>1(2),n(1)—>n(2),n(2)—>T容量为INF。

核心代码:

C++
int n,m,tot,st,en;
int s,a[N],f[N];
long long ans;
int main(){
	register int i,j;
	scanf("%d",&n);
	for(i=1;i<=n;++i)fip(a[i]);
	tot=n+n,st=++tot,en=++tot;
	for(s=i=1;i<=n;++i){
		for(f[i]=j=1;j<i;++j)
		if(a[j]<=a[i])
			f[i]=max(f[i],f[j]+1);
		s=max(s,f[i]);
		adde(i,i+n,1);
	}printf("%d\n",s);
	for(i=1;i<=n;++i){
		if(f[i]==1)adde(st,i,1);
		if(f[i]==s)adde(i+n,en,1);
	}for(i=1;i<=n;++i)
	for(j=1;j<i;++j)
	if(a[j]<=a[i]&&f[i]==f[j]+1)
		adde(j+n,i,1);
	Init_net();
	while(lab[st]<=tot)
		ans+=Isap(st,INF);
	printf("%lld\n",ans);
	if(f[1]!=s)
		adde(st,1,INF);
		adde(1,1+n,INF);
	if(f[n]==s)
		adde(n,n+n,INF),
		adde(n+n,en,INF);
	for(i=1;i<=tot;++i)
		lab[i]=cur[i]=gap[i]=0;
	Init_net(); 
	while(lab[st]<=tot)
		ans+=Isap(st,INF);
	printf("%lld\n",ans);
	return 0;
}

14 - P4304 [TJOI2013]攻击装置

应用思想:最小割 - 对立思想(最大独立集)

建模方式:分层

模型描述:

定义满足(i+j)%2==1的点(x,y)为黑节点,满足(i+j)%2==0的点(x,y)为白节点

有向边 of 点值 from 超源(S) to 黑节点 through 有无障碍

有向边 of INF from 黑节点 to 白节点 through 日字关系

有向边 of 点值 from 白节点 to 超汇(T) through 有无障碍

核心代码:

C++
#define P(x,y) ((~-(x))*n+(y))
const int mx[9]={-2,-2,-1,-1,1,1,2,2};
const int my[9]={-1,1,-2,2,-2,2,-1,1};
int n,m,st,en,tot,sum,ans;
bool tp[201][201];
inline bool limc(int x,int y){
	return x<1||x>n||y<1||y>n||tp[x][y];}
int main(){
	register int i,j,k,u,x,y;
	scanf("%d\n",&n);
	tot=n*n,st=++tot,en=++tot;
	for(k=i=1;i<=n;++i){
		for(j=1;j<=n;++j)
			tp[i][j]=getchar()-48;
		getchar();
	}for(i=1;i<=n;++i)
	for(j=1;j<=n;++j){
		if(tp[i][j])continue;
		else u=P(i,j);
		if((i+j)%2){
			for(k=0;k<8;++k){
				if(limc(x=i+mx[k],y=j+my[k]))continue;
				adde(u,P(x,y),INF);
			}adde(st,u,1);
		}else adde(u,en,1);
		++sum;
	}Init_net();
	while(lab[st]<=tot)
		ans+=Isap(st,INF);
	printf("%d",sum-ans);
	return 0;
}

15 - P2172 [国家集训队]部落战争

应用思想:最大流 - 并列思想(最大独立集)

建模方式:拆点

模型描述:

有向边 of 1 from 超源(S) to 城镇节点1 through 无

有向边 of 1 from 城镇节点1 to 城镇节点2 through RC关系

有向边 of 1 from 城镇节点2 to 超汇(T) through 无

核心代码:

注:此题如用 快速读入字符 写法,请按如下语句写

代码
while(ch!='.'&&ch!='x')ch=getchar();

但使用如下语句会报错,原因未知

代码
while(ch==' '||ch=='\n')ch=getchar();
C++
#define P(x,y) ((~-(x))*m+(y))
int n,m,bx,by,st,en,tot;
long long sum,ans;
bool tp[51][51];
inline bool limc(int x,int y){
	return x<1||x>n||y<1||y>m||tp[x][y];}
int main(){
	register int i,j,k,x,y,u;
	scanf("%d%d%d%d",&n,&m,&bx,&by);
	tot=n*m,st=tot*2+1,en=st+1;
	int mx[5]={bx,by,by,bx};
	int my[5]={-by,-bx,bx,by};
	for(i=1;i<=n;++i)
	for(j=1;j<=m;++j)
		if(readc()=='x')
			tp[i][j]=1;
		else ++sum;
	for(i=1;i<=n;++i)
	for(j=1;j<=m;++j){
		if(tp[i][j])continue;
		adde(st,u=P(i,j));
		for(k=0;k<4;++k){
			x=i+mx[k],y=j+my[k];
			if(limc(x,y))continue;
			adde(u,P(x,y)+tot);
		}adde(u+tot,en);
	}tot=en;
	Init_net();
	while(lab[st]<=tot)
		ans+=Isap(st);
	printf("%lld",sum-ans);
	return 0;
}

16 - P4313 文理分科

应用思想:最小割 - 对立思想

建模方式:合点

模型描述:

有向边 of 点文值 from 超源(S) to 图内节点 through 无

有向边 of 合文值 from 超源(S) to 合文节点 through 无

有向边 of INF from 合文节点 to 图内节点 through 相邻关系

有向边 of INF from 图内节点 to 合理节点 through 相邻关系

有向边 of 合理值 from 合理节点 to 超汇(T) through 无

有向边 of 点理值 from 图内节点 to 超汇(T) through 无

核心代码:

C++
#define P(x,y) ((~-(x))*m+(y))
int n,m,tot,st,en;
long long sum,ans;
int main(){
	register int i,j,w;
	scanf("%d%d",&n,&m);
	tot=n*m,st=++tot,en=++tot;
	for(i=1;i<=n;++i)
	for(j=1;j<=m;++j)
		fip(w),sum+=w,
		adde(st,P(i,j),w);
	for(i=1;i<=n;++i)
	for(j=1;j<=m;++j)
		fip(w),sum+=w,
		adde(P(i,j),en,w);
	for(i=1;i<=n;++i)
	for(j=1;j<=m;++j){
		fip(w),sum+=w;
		adde(st,++tot,w),
		adde(tot,P(i,j),INF);
		if(i>1)adde(tot,P(i-1,j),INF);
		if(i<n)adde(tot,P(i+1,j),INF);
		if(j>1)adde(tot,P(i,j-1),INF);
		if(j<m)adde(tot,P(i,j+1),INF);
	}for(i=1;i<=n;++i)
	for(j=1;j<=m;++j){
		fip(w),sum+=w;
		adde(++tot,en,w),
		adde(P(i,j),tot,INF);
		if(i>1)adde(P(i-1,j),tot,INF);
		if(i<n)adde(P(i+1,j),tot,INF);
		if(j>1)adde(P(i,j-1),tot,INF);
		if(j<m)adde(P(i,j+1),tot,INF);
	}Init_net();
	while(lab[st]<=tot)
		ans+=Isap(st,INF);
	printf("%lld",sum-ans);
	return 0;
}

17 - P5039 [SHOI2010]最小生成树

应用思想:最小割 - 对立思想,kruskal最小生成树算法思想

建模方式:朴素

模型描述:

无向边 of 需操作数 from 图内节点 to 图内节点 through 是否小等于给定边权

核心代码:

C++
int eu[M],ev[M],ew[M];
int n,m,p,tot,st,en;
long long ans;
int main(){
	register int i,u,v,w;
	scanf("%d%d%d",&n,&m,&p);
	for(i=1;i<=m;++i)
		fip(eu[i]),
		fip(ev[i]),
		fip(ew[i]);
	tot=n,st=eu[p],en=ev[p];
	for(i=1;i<=m;++i)
	if(ew[i]<=ew[p]&&i!=p){
		u=eu[i],v=ev[i];
		w=ew[p]-ew[i]+1;
		adde(u,v,w),adde(v,u,w);
	}Init_net();
	while(lab[st]<=tot)
		ans+=Isap(st,INF);
	printf("%lld",ans);
	return 0;
}

Part 4 Isap-01Graph代码

朴素Isap网络流算法代码-[图论]网络流学习笔记-Isap算法原理

C++
#include<cstdio>
#include<queue>
using namespace std;
const int N=100001;
const int M=500001;
int cnt=1,beg[N],nex[M],go[M];
int lab[N],cur[N],gap[N];
int n,m,st,en,tot,ans;
bool len[M];
inline void fip(int& rer){
	register char ch;
	for(ch=0;ch<'0'||ch>'9';ch=getchar());
	for(rer=0;ch>='0'&&ch<='9';ch=getchar())
		rer=(rer<<3)+(rer<<1)+(ch^48);
}
inline void adde(int u,int v){
	nex[++cnt]=beg[u],beg[u]=cnt;
	go[cnt]=v,len[cnt]=true;
	nex[++cnt]=beg[v],beg[v]=cnt;
	go[cnt]=u,len[cnt]=false;
}
void Init_net(){
	register int i,u,v;queue<int>Q;
	for(lab[en]=1,Q.push(en);!Q.empty();Q.pop())
		for(i=beg[u=Q.front()];v=go[i],i;i=nex[i])
			if(!lab[v])lab[v]=lab[u]+1,Q.push(v);
	for(i=1;i<=tot;++i)++gap[lab[i]],cur[i]=beg[i];
}
bool Isap(int u){
	if(u==en)return true;
	register int i,v;
	for(i=cur[u];v=go[i],i;cur[u]=i=nex[i]){
		if(lab[u]!=lab[v]+1)continue;
		if(!len[i])continue;
		if(!Isap(v))continue;
		len[i]=0,len[i^1]=1;
		return true;
	}if(!--gap[lab[u]])lab[st]=tot+1;
	++gap[++lab[u]],cur[u]=beg[u];
	return false;
}
int main(){
	register int i,u,v;
	scanf("%d%d",&n,&m);
	tot=n,st=++tot,en=++tot;
	//To be continued
	Init_net();
	while(lab[st]<=tot)
		ans+=Isap(st);
	printf("%d",ans);
    return 0;
}

Part 5 后记

Thx for reading

Uploaded on 2022-01-27

For schoolwork

原文
抓取于
  1. nosexynomoney

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  2. nosexynomoney

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%