次小生成树

图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;
}

你可能感兴趣的:(次小生成树)