F - The Unique MST

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!

代码是自己写的,和题解不一样,思路是看题解的

以下思路来自(https://www.xuebuyuan.com/2967049.html)
次小生成树:首先必须存在最小生成树,次小生成树的意思就是,除了最小生成树之外,存在一个生成树,权值仅大于最小生成树,也就是

给出一个带边权的无向图G,设其最小生成树为T,求出图G的与T不完全相同的边权和最小的生成树(即G的次小生成树)。一个无向图的两棵生成树不完全相同,当且仅当这两棵树中至少有一条边不同。(注意,图G可能不连通,可能有平行边)

那么如何来求最小生成树?

定义生成树T的一个可行变换(-E1, +E2):将T中的边E1删除后,再加入边E2(满足边E2原来不在T中但在G中),若得到的仍然是图G的一棵生成树,则该变换为可行变换,该可行变换的代价为(E2权值 - E1权值)。这样,很容易证明,G的次小生成树就是由G的最小生成树经过一个代价最小的可行变换得到。进一步可以发现,这个代价最小的可行变换中加入的边E2的两端点如果为V1和V2,那么E1一定是原来最小生成树中从V1到V2的路径上的权值最大的边。

算法:
1.设立数组F,F[x][y]表示T中从x到y路径上的最大边的权值。F数组可以在用Prim算法求最小生成树的过程中得出。
2.每次将边(i,j) G[i][j]加入后(j是新加入的边的新端点),枚举树中原有的每个点k(包括i,但不包括j),则F[k][j]=max{F[k][i],G[i][j]},又由于F数组是对称的,可以得到F[j][k]=F[k][j]。
3.将图G中的边(i, j)删除(就是将邻接矩阵中(i,j)边权值改为∞)!因为T中的边是不能被加入的。
4.等T被求出后,所有的F值也求出了,然后,枚举点i、j,若邻接矩阵中边(i, j)权值不是无穷大(这说明i、j间存在不在T中的边),则求出{(i, j)边权值 - F[i][j]}的值,即为加入边(i, j)的代价,求最小的总代价即可。

!!! 另外注意三种特殊情况:
【1】图G不连通,此时最小生成树和次小生成树均不存在。判定方法:在扩展T的过程中找不到新的可以加入的边;【2】图G本身就是一棵树,此时最小生成树存在(就是G本身)但次小生成树不存在。判定方法:在成功求出T后,发现邻接矩阵中的值全部是无穷大;
【3】图G存在平行边。这种情况最麻烦,因为这时代价最小的可行变换(-E1, +E2)中,E1和E2可能是平行边!因此,只有建立两个邻接矩阵,分别存储每两点间权值最小的边和权值次小的边的权值,然后,每当一条新边(i, j)加入时,不是将邻接矩阵中边(i, j)权值改为无穷大,而是改为连接点i、j的权值次小的边的权值

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define INF 0x3f3f3f3f
#define N 105
using namespace std;
int n,m;
int flag[N],d[N][N],res[N],dt[N][N],result,road[N];
void Initial(){
	memset(flag,0,sizeof(flag));
	memset(d,INF,sizeof(d));
	memset(res,INF,sizeof(res));
	memset(dt,0,sizeof(dt));
	result=0;
}
void Prim(){
	int add=1;
	res[1]=0;
	while(1){
		flag[add]=1;
		for(int i=1;i<=n;i++){
			if(flag[i]) continue;
			if(res[i]> d[add][i]){
				res[i]=d[add][i];
				road[i]=add;
			}
		}
		int minid=-1,minnum=INF;
		for(int i=1;i<=n;i++){
			if(flag[i]) continue;
			if(minnum>res[i]){
				minnum=res[i];
				minid=i;
			}
		}
		if(minid==-1) break;
		add=minid;
		result+=minnum;
		for(int i=1;i<=n;i++){
			if(flag[i]){
				dt[i][minid]=dt[minid][i]=max( d[minid][ road[minid] ],dt[ road[minid] ][i]);
			}
		}
	}//计算好了dt  
	int yw=1;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++){
			if( i==road[j] || j==road[i] ) continue;
			if(d[i][j]==INF) continue;
			if(d[i][j]==dt[i][j]) 
			 yw=0;//没有唯一的 
		}
	if(yw) cout<<result<<endl;
	else cout<<"Not Unique!"<<endl;
}
int main(){
	int t;
	cin>>t;
	while(t--){
		Initial();
		scanf("%d%d",&n,&m);
		for(int i=1;i<=m;i++){
			int u,v,w;
			scanf("%d%d%d",&u,&v,&w);
			d[u][v]=d[v][u]=w;
		}
		Prim();
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

洛阳八中我最棒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值