跳到正文

个人记录

2026/4/19晚上 总结

xuyulan约 9 分钟
原文
文章信息与版本

文章信息

发布
2026年4月20日
收录
2026年4月20日
更新
2026年11月12日
篇幅
约 9 分钟

版本

  1. v2当前
  2. v1

新~ 芝士 ~🎉🎉

最小生成树(MSTMST)

  1. 生成树
    • 包含图里全部 n 个点
    • 只有 n−1 条边
    • 连通、无环(就是一棵树)
  2. 最小
    • 在所有可能的生成树里,边的权值加起来最小

直观理解

  • 点 = 城市
  • 边 = 修路,权值 = 修路成本
  • 目标:用最少的总费用,把所有城市连通,不造多余环路 → 这就是最小生成树。
    两个核心算法
  • Kruskal:边从小到大选,用并查集判环
  • Prim:像 DijkstraDijkstra,从一个点向外扩展,每次选最短边

一句话概括:
给你一张带权无向连通图, 选出总边权最小的一些边,把所有点连在一起,且不形成环,这棵树就是最小生成树。

P3366 【模板】最小生成树

并查集打辅助判断环,需要贪心寻找最小值,所以把边按边权从小到大排序,把不成环的边连起来,ansans+=边权,当连到了 n−1n-1 条边时,输出 ansans ,如果连不到 n−1n-1 条边,输出 orzorz

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+50;
int n,m,ans,f[N],cnt;
struct E{
	int u,v,w;
}e[N];
int find(int x){
	if(f[x]==x)return x;
	return f[x]=find(f[x]);
}
void ch(int x,int y){
	x=find(x);
	y=find(y);
	if(x!=y)f[x]=y;
}
int cmp(E x,E y){
	return x.w<y.w;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)f[i]=i;
	for(int i=1;i<=m;i++){
		cin>>e[i].u>>e[i].v>>e[i].w;
	}
	sort(e+1,e+1+m,cmp);
	for(int i=1;i<=m;i++){
		int x=e[i].u,y=e[i].v;
		if(find(x)!=find(y)){
			ch(x,y);
			ans+=e[i].w;
			cnt++;
			if(cnt==n-1){
				cout<<ans;
				return 0;
			}
		}
	}
	cout<<"orz"; 
	return 0;
}

P1195 口袋的天空

与上一题几乎完全一样,唯一不同的是输出条件,这题是当连到了 n−kn-k 条边时,输出 ansans ,如果连不到 n−kn-k 条边,输出 NoNo AnswerAnswer

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e4+50;
int n,m,k,ans,f[N],cnt;
struct E{
	int u,v,w;
}e[N];
int find(int x){
	if(f[x]==x)return x;
	return f[x]=find(f[x]);
}
void ch(int x,int y){
	x=find(x);
	y=find(y);
	if(x!=y)f[x]=y;
}
int cmp(E x,E y){
	return x.w<y.w;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n>>m>>k;
	for(int i=1;i<=n;i++)f[i]=i;
	for(int i=1;i<=m;i++){
		cin>>e[i].u>>e[i].v>>e[i].w;
	}
	sort(e+1,e+1+m,cmp);
	for(int i=1;i<=m;i++){
		int x=e[i].u,y=e[i].v;
		if(cnt==n-k){
			cout<<ans;
			return 0;
		}
		if(find(x)!=find(y)){
			ch(x,y);
			ans+=e[i].w;
			cnt++;
		}
	}
	if(cnt==n-k){
		cout<<ans;
		return 0;
	}
	cout<<"No Answer"; 
	return 0;
}

P4826 [USACO15FEB] Superbull S

这题要把边从大到小排序,addadd是把每条边的起点和终点以及边权都存下来

C++
#include<bits/stdc++.h>
using namespace std;
const int N=4e6+50;
long long n,m,ans,a[N],cnt,f[N];
struct E{
	long long u,v,w;
}e[N];
int find(int x){
	if(f[x]==x)return x;
	return f[x]=find(f[x]);
}
void ch(int x,int y){
	x=find(x);
	y=find(y);
	if(x!=y)f[x]=y;
}
void add(int x,int y,int w){
	m++;
	e[m]={x,y,w};
}
int cmp(E x,E y){
	return x.w>y.w;
}
void kruskal(){
	for(int i=1;i<=m;i++){
		if(cnt==n-1)break;
		int x=find(e[i].u),y=find(e[i].v);
		if(x!=y){
			ch(x,y);
			ans+=e[i].w;
			cnt++;
		}
	}
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		f[i]=i;
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(i!=j){
				add(i,j,a[i]^a[j]);
			}
		}
	}
	sort(e+1,e+1+m,cmp);
	kruskal();
	if(cnt==n-1){
		cout<<ans;
	}
	return 0;
}

U555760 AT_abc328_e

用dfs枚举选或不选,g是在判断是否有环,如果没环 ans 取边权的最小值

注意!所有的都要开longlong longlong

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=4e2+50;
int n,m,M,ans=1e18,a[N],cnt,f[N],v[N],r[N];
struct E{
	int u,v,w;
}e[N];
int find(int x){
	if(f[x]==x)return x;
	return f[x]=find(f[x]);
}
void ch(int x,int y){
	x=find(x);
	y=find(y);
	if(x!=y)f[x]=y;
}
int g(){
	for(int i=1;i<=n;i++){
		f[i]=i;
	}
	for(int i=1;i<n;i++){
		int x=find(e[r[i]].u),y=find(e[r[i]].v);
		if(x!=y){
			ch(x,y);
		}else{
			return 0;
		}
	}
	return 1;
}
int cmp(E x,E y){
	return x.w>y.w;
}
void dfs(int c,int x,int cnt){
	if(x==n-1){
		if(g())ans=min(ans,cnt%M);
		return;
	}
	if(c>m)return;
	r[x+1]=c;
	dfs(c+1,x+1,(cnt%M+e[c].w%M)%M);
	dfs(c+1,x,cnt%M);
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n>>m>>M;
	for(int i=1;i<=m;i++){
		cin>>e[i].u>>e[i].v>>e[i].w;
	}
	dfs(1,0,0);
	cout<<ans;
	return 0;
}

P5687 [CSP-S 2019 江西] 网格图

初始先用最小的横向边 + 最小的纵向边把第一行、第一列连通。

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=3e5+50;
int n,m,ans,cnt,p,pp,h,l,a[N],b[N];
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=m;i++){
		cin>>b[i];
	}
	sort(a+1,a+n+1);
	sort(b+1,b+1+m);
	ans=(m-1)*a[1]+(n-1)*b[1],cnt=n+m-2,p=2,pp=2,h=1,l=1;
	while(cnt!=n*m-1){
        if(a[p]<b[pp]){
        	int t=m-l;        // 这条横向边能连通 t 个位置
        	ans+=a[p]*t;      // 累加总代价
        	cnt+=t;           // 记录已选边数
        	p++;h++;          // 换下一条横向边,更新连通行数
        }else{
        	int t=n-h;        // 这条纵向边能连通 t 个位置
        	ans+=b[pp]*t;     // 累加总代价
        	cnt+=t;           // 记录已选边数
        	pp++;
			l++;         // 换下一条纵向边,更新连通列数
    	}
    }
	cout<<ans;
	return 0;
}
原文