首页 > 其他 > 详细

poj1679The Unique MST判断最小生成树是否唯一以及求次小生成树边权和的讲解

时间:2015-04-19 22:49:24      阅读:369      评论:0      收藏:0      [点我收藏+]
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 22346   Accepted: 7924

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!

Source

POJ Monthly--2004.06.27 srbga@POJ

次小生成树:除却当前已生成的最小生成树外,最小的生成树

判断最小生成树唯一:若当前的最小生成树的边权和不等于次小生成树的边权和则此图中最小生成树唯一

计算次小生成树的边权和:

首先生成一棵最小生成树并维护两个数组

pre[i]:树中,i的父亲
mx[i][j]:i到j的树链上最大的边权是多少

方便描述,设一个数组cost[i][j]:i跟j之间的边的权值
然后,枚举任意两点i,j,满足,在树中i,j没有连边,
若最小生成树的边权和sum,存在sum-mx[i][j]+cost[i][j]==sum则说明最小生成树不唯一

这个过程就是所谓的删掉i到j的树链中的最大边,再加上树外的边使得i,j再次连通,
形成一棵新的树,若新树的边权和==sum,则说明最小生成树不唯一



#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
bool used[110],fb[110][110];
int pre[110],dis[110],mx[110][110],cost[110][110];
int work(int n)
{
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		dis[i]=cost[1][i];
		pre[i]=1;
	}
	memset(fb,0,sizeof(fb));
	memset(used,0,sizeof(used));
	used[1]=1;
	int N=n;
	while(--N)
	{
		int from,mn=INT_MAX;
		for(int i=1;i<=n;i++)
			if(!used[i]&&mn>dis[i])
			{
				from=i;
				mn=dis[i];
			}
		ans+=mn;
		used[from]=1;
		fb[from][pre[from]]=fb[pre[from]][from]=1;
		for(int i=1;i<=n;i++)
			if(used[i])
				mx[i][from]=mx[from][i]=max(mx[i][pre[from]],dis[from]);
			else if(!used[i]&&dis[i]>cost[from][i])
			{
				dis[i]=cost[from][i];
				pre[i]=from;
			}
	}
	return ans;
}
int ok(int n)
{
	int ans=work(n);
	int mn=INT_MAX;
	for(int i=1;i<=n;i++)
		for(int j=i+1;j<=n;j++)
			if(!fb[i][j]&&cost[i][j]!=INT_MAX)
				mn=min(mn,ans-mx[i][j]+cost[i][j]);
	return mn==ans?-1:ans;
}
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		int n,m;
		cin>>n>>m;
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				cost[i][j]=INT_MAX;
		while(m--)
		{
			int s,e,w;
			cin>>s>>e>>w;
			cost[s][e]=cost[e][s]=w;
		}
		int ans=ok(n);
		if(ans==-1)
			cout<<"Not Unique!"<<endl;
		else
			cout<<ans<<endl;
	}
}


poj1679The Unique MST判断最小生成树是否唯一以及求次小生成树边权和的讲解

原文:http://blog.csdn.net/stl112514/article/details/45133225

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!