POJ-1679 The Unique MST 次最小生成树

这两种算法的思路都是相同的,首先求出最小生成树,我们枚举每条不在最小生成树上的边,并把这条边放到最小生成树上面,然后就一定会形成环,那么我们在这条环路中取出一条最长的路(除了新加入的那一条边)。最终我们得到的权值就是次小生成树的权值。

#include<cstring>
#include<cstdio>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<iostream>
#include<vector>
/*
 * 次小生成树
 * 求最小生成树时,用数组Max[i][j]来表示MST中i到j最大边权
 * 求完后,直接枚举所有不在MST中的边,替换掉最大边权的边,更新答案
 * 点的编号从0开始
 */
using namespace std;
const int inf=0x3f3f3f3f;
const int N=110;
int Max[N][N],dis[N],mp[N][N];
int pre[N];
bool used[N][N],vis[N];
int prim(int n)
{
	memset(vis,false,sizeof(vis));
	memset(dis,inf,sizeof(dis));
	memset(pre,0,sizeof(pre));
	memset(Max,0,sizeof(Max));
	memset(used,false,sizeof(used));
	dis[1]=0;
	int ans=0;
	for(int i=0;i<n;i++){
		int u=-1;
		int minn=inf;
		for(int j=1;j<=n;j++){
			if(!vis[j]&&minn>dis[j]){
				u=j;
				minn=dis[j];
			}
		}
		if(u==-1) break;
		vis[u]=true;
		ans+=minn;
		//表示用过的俩点 
		used[u][pre[u]]=used[pre[u]][u]=true;
		for(int j=1;j<=n;j++){
			if(vis[j]&&j!=u){
				//可以理解成j到u 
				//当前边的值(dis[u]) 和j到u的前一个节点的最大边权值  
				Max[u][j]=Max[j][u]=max(Max[j][pre[u]],dis[u]);
			}
			if(!vis[j]&&dis[j]>mp[u][j]){
				dis[j]=mp[u][j];
				pre[j]=u;
			}
		}
	}
	return ans;
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--){
		int n,m;
		scanf("%d %d",&n,&m);
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				if(i==j) mp[i][j]=0;
				else mp[i][j]=inf;
			}
		}
		while(m--){
			int a,b,c;
			scanf("%d %d %d",&a,&b,&c);
			mp[a][b]=mp[b][a]=c;
		}
		int ans=prim(n);
	//	cout<<"**************"<<ans<<endl;
		int Min=inf;
		for(int i=1;i<=n;i++){
			for(int j=i+1;j<=n;j++){
				if(!used[i][j]&&mp[i][j]!=inf){
					Min=min(Min,ans+mp[i][j]-Max[i][j]);
				}
			}
		}
	//	cout<<Min<<endl;
		if(ans==Min){
			printf("Not Unique!\n");
		}
		else printf("%d\n",ans);
		
	}
	return 0;
}

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!

猜你喜欢

转载自blog.csdn.net/CC_1012/article/details/89815718