跳到正文

题解

4.26-周日晚上-陈-最小生成树进阶总结

WangChi123456约 12 分钟
原文
文章信息与版本

文章信息

收录
2026年5月12日
更新
2026年5月14日
篇幅
约 12 分钟

版本

  1. v2当前
  2. v1

4.26-周日晚上-陈-最小生成树进阶总结

第一题

这题的可以用二分来写,要用二分首先得要有单调性,如果小明妈妈道路的拥挤度越大,那可以选择的边越多,而边越多,两地的道路也越多,道路的拥挤度越小则反之,所以我可以用二分来查找小明妈妈的最大拥挤度

C++
#include<bits/stdc++.h>
using namespace std;
const int w=2e4+5;
int n,m,s,t;
int fa[w];
struct node{
	int u,v,z;
}a[w];
int find(int x){
	if(fa[x]==x)return x;
	return fa[x]=find(fa[x]);
}
void unionn(int x,int y){
	x=find(x);
	y=find(y);
	if(x!=y)fa[x]=y;
}
bool check(int x){
	for(int i=1;i<=n;i++){
		fa[i]=i;
	}
	for(int i=1;i<=n;i++){
		if(a[i].z<=x){
			unionn(a[i].u,a[i].v);
		}
	}
	return find(s)==find(t);
}

int main(){
	cin>>n>>m>>s>>t;
	for(int i=1;i<=m;i++){
		cin>>a[i].u>>a[i].v>>a[i].z;
	}
	int l=0,r=1e4,mid,p=0;
	while(l<=r){
		mid=l+(r-l)/2;
		if(check(mid)){
			p=mid;
			r=mid-1;
		}else{
			l=mid+1;
		}
	}
	cout<<p;
	return 0;
}

这题还有第二种写法,就是用生成树写的,首先我得要了解瓶颈生成树,瓶颈生成树就是生成树中最大的边权最小,可以发现刚好就是我们要求的,要是最大边权最小,然后最小生成树就是瓶颈生成树,而瓶颈生成树却不是最小生成树,证明是,因为最小生成树为了总和最小,绝不会留一条边权大的边能换成更小的边,所以它的最大边是所有生成树中最小的,所以是瓶颈生成树,现在只要跑一遍Kruskal,连接边,如果两地相连,就输出就可以了

C++
#include<bits/stdc++.h>
using namespace std;
const int w=2e4+5;
int n,m,s,t;
int fa[w];
struct node{
	int u,v,z;
}a[w];
int find(int x){
	if(fa[x]==x)return x;
	return fa[x]=find(fa[x]);
}
void unionn(int x,int y){
	x=find(x);
	y=find(y);
	if(x!=y)fa[x]=y;
}
int main(){
	cin>>n>>m>>s>>t;
	for(int i=1;i<=m;i++){
		cin>>a[i].u>>a[i].v>>a[i].z;
	}
	sort(a+1,a+m+1,[](node x,node y){
		return x.z<y.z;
	});
    for(int i=1;i<=n;i++){
        fa[i]=i;
    }
	for(int i=1;i<=m;i++){
		if(find(a[i].v)!=find(a[i].u)){
			unionn(a[i].v,a[i].u);
		}
		if(find(s)==find(t)){
			cout<<a[i].z;
			return 0;
		}
	}
	return 0;
}

第二题

在这题中,没说ki,j<Ak_{i,j}<A,所以我们还要考虑是否使用优惠,我们可以把买完一个物品买另一个物品后看作一个有向边,然后跑一遍Kruskal,可现在需要考虑点权,所以我们可以把点权转换为边权,可以发现n个点只能连n-1条边,还剩下一个点的点权,所以我们得在最后答案加上这个点权

C++
#include<bits/stdc++.h>
using namespace std;
const int w=1e6+5;
int A,B;
int fa[w];
struct node{
	int u,v,z;
}a[w];
int find(int x){
	if(fa[x]==x)return x;
	return fa[x]=find(fa[x]);
}
void unionn(int x,int y){
	x=find(x);
	y=find(y);
	if(x!=y)fa[x]=y;
}
int idx=0;
void add_edge(int u,int v,int z){
	idx++;
	a[idx].u=u;
	a[idx].v=v;
	a[idx].z=z;
}
int main(){
	cin>>A>>B;
	for(int i=1;i<=B;i++){
		for(int j=1;j<=B;j++){
			int z;
			cin>>z;
			if(z!=0){
				add_edge(i,j,z);
			}
			add_edge(i,j,A);
		}
	}
	sort(a+1,a+idx+1,[](node x,node y){
		return x.z<y.z;
	});
	for(int i=1;i<=B;i++){
		fa[i]=i;
	}
	//cout<<a[idx].u<<" "<<a[idx].v<<" "<<a[idx].z<<'\n';
	int cnt=0,ans=0;
	for(int i=1;i<=idx;i++){
		if(cnt==B-1){
			cout<<ans+A;
			return 0;
		}
		if(find(a[i].u)!=find(a[i].v)){
			unionn(a[i].u,a[i].v);
			cnt++;
			ans+=a[i].z;
		}
	}
	if(cnt==B-1){
		cout<<ans+A;
		return 0;
	}
	return 0;
}

这题与上题一样,也有两种解法,我们可以再建一个点,与之前每个点相连,边权是A,我们可以理解一个起始点,从这个点买的东西就看成没有优惠的,从其他点就当成有优惠买,所以再跑Kruskal再怎么样这个点一定会被连,然后这要把Kruskal跑完就可以了,注意最后答案就不要+A了

C++
#include<bits/stdc++.h>
using namespace std;
const int w=1e6+5;
int A,B;
int fa[w];
struct node{
	int u,v,z;
}a[w];
int find(int x){
	if(fa[x]==x)return x;
	return fa[x]=find(fa[x]);
}
void unionn(int x,int y){
	x=find(x);
	y=find(y);
	if(x!=y)fa[x]=y;
}
int idx=0;
void add_edge(int u,int v,int z){
	idx++;
	a[idx].u=u;
	a[idx].v=v;
	a[idx].z=z;
}
int main(){
	cin>>A>>B;
	for(int i=1;i<=B;i++){
		for(int j=1;j<=B;j++){
			int z;
			cin>>z;
			if(z!=0){
				add_edge(i,j,z);
			}
		}
	}
	for(int i=1;i<=B;i++){
		add_edge(0,i,A);
	}
	sort(a+1,a+idx+1,[](node x,node y){
		return x.z<y.z;
	});
	for(int i=1;i<=B;i++){
		fa[i]=i;
	}
	int cnt=0,ans=0;
	for(int i=1;i<=idx;i++){
		if(cnt==B){
			cout<<ans;
			return 0;
		}
		if(find(a[i].u)!=find(a[i].v)){
			unionn(a[i].u,a[i].v);
			cnt++;
			ans+=a[i].z;
		}
	}
	if(cnt==B){
		cout<<ans;
		return 0;
	}
	return 0;
}

第三题

这题的我们可以把照顾的顺序抽象成一个图,我们把在路上花费的时间看成边权,照顾奶牛的时间看成点权,我们拿图中眼里做例子 我们从1开始出发,从1-2,2-3,3-2,2-4,4-5,5-4,4-2,2-1 一共就是10+5+10+5+20+5+10+12+6+12+30+12+6+12+10+5+10=180{\color{yellow}10}+{\color{red}5}+{\color{yellow}10}+{\color{red}5}+{\color{yellow}20}+{\color{red}5}+{\color{yellow}10}+{\color{red}12}+{\color{yellow}6}+{\color{red}12}+{\color{yellow}30}+{\color{red}12}+{\color{yellow}6}+{\color{red}12}+{\color{yellow}10}+{\color{red}5}+{\color{yellow}10}=180 我们把重复的整理一下 (10+10)+(5+5)+(10+10)+(5+5)+20+10+(12+12)+(6+6)+(12+12)+30=180({\color{yellow}10}+{\color{yellow}10})+({\color{red}5}+{\color{red}5})+({\color{yellow}10}+{\color{yellow}10})+({\color{red}5}+{\color{red}5})+{\color{yellow}20}+{\color{yellow}10}+({\color{red}12}+{\color{red}12})+({\color{yellow}6}+{\color{yellow}6})+({\color{red}12}+{\color{red}12})+{\color{yellow}30}=180 再整理得 10+(10+5+5+10)+(10+5+5+20)+(10+12+12+6)+(6+12+12+30)=180{\color{yellow}10}+({\color{yellow}10}+{\color{red}5}+{\color{red}5}+{\color{yellow}10})+({\color{yellow}10}+{\color{red}5}+{\color{red}5}+{\color{yellow}20})+({\color{yellow}10}+{\color{red}12}+{\color{red}12}+{\color{yellow}6})+({\color{yellow}6}+{\color{red}12}+{\color{red}12}+{\color{yellow}30})=180 整理完以后,可以发现,走了以后都是每条路加上边的边权*2,和两点的点权,最后在加上起点,那其他点是不是也这样,答案是是的,所以我们可以根据这个在新建一个图,跑一遍Kruskal,最后在答案后面加上最小值就可以了

C++
#include<bits/stdc++.h>
using namespace std;
const int w=1e6+5;
int n,m;
int c[w];
int fa[w];
struct node{
	int u,v,z;
}a[w];
int find(int x){
	if(fa[x]==x)return x;
	return fa[x]=find(fa[x]);
}
void unionn(int x,int y){
	x=find(x);
	y=find(y);
	if(x!=y)fa[x]=y;
}
int idx=0;
void add_edge(int u,int v,int z){
	idx++;
	a[idx].u=u;
	a[idx].v=v;
	a[idx].z=z;
}
int main(){
	int minn=1e9;
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>c[i];
		minn=min(minn,c[i]);
	}
	for(int i=1;i<=m;i++){
		int u,v,z;
		cin>>u>>v>>z;
		add_edge(u,v,2*z+c[u]+c[v]);
	}
	sort(a+1,a+idx+1,[](node x,node y){
		return x.z<y.z;
	});
	for(int i=1;i<=n;i++){
		fa[i]=i;
	}
	//cout<<a[idx].u<<" "<<a[idx].v<<" "<<a[idx].z<<'\n';
	int cnt=0,ans=0;
	for(int i=1;i<=idx;i++){
		if(cnt==n-1){
			cout<<ans+minn;
			return 0;
		}
		if(find(a[i].u)!=find(a[i].v)){
			unionn(a[i].u,a[i].v);
			cnt++;
			ans+=a[i].z;
		}
	}
	if(cnt==n-1){
		cout<<ans+minn;
		return 0;
	}
	return 0;
}
原文