次小生成树

本文详细介绍了一种求解次小生成树问题的有效算法。通过预处理每个顶点的最短边路径,利用DFS遍历并找到可能形成次小生成树的边,最终计算出次小生成树的总权重。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

图G(V,E)的最小生成树构成边集E',对于每个顶点v∈V,遍历从v出发的、且不在E’中的边e。

将边e加入MST的边集E’,此时E‘必然包含环路,删除环上除边e外的最长边,剩下的边集成为一棵生成树。

遍历每条不在MST上的边计算生成树的总权值,取最小值即为次最小生成树的总权值。


对于两个在最小生成树上没有直接相连的顶点,它们之间在最小生成树上的路径和它们直接相连的边构成一个需要搜索的环路。

则可以预处理每个顶点,DFS出从这个顶点到其他顶点的路径上的最长边。

int F[100];  // 记录到顶点路径上的最长边
bool pass[100];  // 顶点是否遍历过

void fill(int x, int m)  // x为目前顶点,m为路径上已知的最长边
{
	F[x] = m;
	pass[x] = true;
	for(int i = 0; i < N; i++) {
		if(mst[x][i] && !pass[i]) {  // 若边在MST上且未遍历过,继续DFS
			fill(i, max(m, map[x][i]));
		}
	}
}

F[j]即为从x到j的路径上的最长边,即环上除添加的边外的最长边。

则对于每个顶点i,在其上添加边可以获得最小的次小生成树的总权值为min(L_mst - F[j] + map[i][j]),j∈V,L_mst为最小生成树的总权值,map[i][j]为顶点i, j之间的权值,i, j在图G(V,E)上直接相连(即e(i, j)∈E),而在最小生成树上不直接相连。

int smst(int t)
{
	int ans = 10000000;
	for(int i = 0; i < N; i++) {
		memset(F, 0, sizeof(F));
		memset(pass, 0, sizeof(pass)); 
		fill(i, 0);  // 预处理
		for(int j = 0; j < N; j++) {
			if(i != j && !mst[i][j] && map[i][j]) {  // 若i, j不同且满足条件
				ans = min(ans, t - F[j] + map[i][j]);
			}
		}
	}
	return ans;	
}


例题:POJ 1679 The Unique MST

Description
Given a connected undirected graph, tell if its minimum spanning tree is unique. 

Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V', E'), with the following properties: 
1. V' = V. 
2. T is connected and acyclic. 

Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E') of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E'. 

Input
The first line contains a single integer t (1 <= t <= 20), the number of test cases. Each case represents a graph. It begins with a line containing two integers n and m (1 <= n <= 100), the number of nodes and edges. Each of the following m lines contains a triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.

Output
For each input, if the MST is unique, print the total cost of it, or otherwise print the string 'Not Unique!'.

Sample Input
2
3 3
1 2 1
2 3 2
3 1 3
4 4
1 2 2
2 3 2
3 4 2
4 1 2

Sample Output
3
Not Unique!

求次小生成树的权值与最小生成树的权值比较,相同输出Not Unique!,不同输出最小生成树的权值。

求最小生成树用Kruskal + 并查集 + 最小堆。

代码:

#include <iostream>
#include <queue>
#include <cstring>
using namespace std;

struct edge
{
	int a, b, len;
	
	bool operator<(const edge &e) const {
		return len > e.len;
	}
};

int N, dj[100], map[100][100];
bool mst[100][100];
priority_queue<edge> heap;

int find(int x)
{
	if(dj[x] != x) {
		dj[x] = find(dj[x]);
		return dj[x];
	}
	return x;
}

void join(int a, int b)
{
	dj[find(a)] = dj[find(b)];
}

void read()
{
	edge E;
	int M;
	
	cin >> N >> M;
	memset(map, 0, sizeof(map));
	memset(mst, 0, sizeof(mst));

	for(int i = 0; i < M; i++) {
		cin >> E.a >> E.b >> E.len;
		E.a--, E.b--;
		map[E.a][E.b] = E.len;
		map[E.b][E.a] = E.len;
		heap.push(E);
	}
	
	for(int i = 0; i < N; i++) {
		dj[i] = i;
	}
}

int kruskal()
{
	int ans = 0;
	while(!heap.empty()) {
		edge E = heap.top();
		heap.pop();
		int a = find(E.a), b = find(E.b);
		if(a != b) {
			join(a, b);
			mst[E.a][E.b] = true;
			mst[E.b][E.a] = true;
			ans += E.len;
		}
	}
	return ans;
}

int F[100];
bool pass[100];

void fill(int x, int m)
{
	F[x] = m;
	pass[x] = true;
	for(int i = 0; i < N; i++) {
		if(mst[x][i] && !pass[i]) {
			fill(i, max(m, map[x][i]));
		}
	}
}

int smst(int t)
{
	int ans = 10000000;
	for(int i = 0; i < N; i++) {
		memset(F, 0, sizeof(F));
		memset(pass, 0, sizeof(pass)); 
		fill(i, 0);
		for(int j = 0; j < N; j++) {
			if(i != j && !mst[i][j] && map[i][j]) {
				ans = min(ans, t - F[j] + map[i][j]);
			}
		}
	}
	return ans;	
}

int main()
{
	int T;
	cin >> T;
	while(T--) {
		read();
		int x = kruskal();
		if(smst(x) == x) {
			cout << "Not Unique!" << endl;
		} else {
			cout << x << endl;
		}
	}
	return 0;
}

内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、CO和SO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性和便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算和结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征和技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析和模拟,揭示了生物质炉具在实际应用中的优点和挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向和政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值