Poj 1679 The Unique MST【次小生成树】

The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 26434   Accepted: 9450

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

求解最小生成树是否唯一


大神的算法解析,点这里

个人并不明白,只找了个模板敲了....

ps:

虽然代码能看明白,但是确实不明白算法的流程,网上的相关信息又比较少,可能是自己对最小生成树理解的还不够透彻,导致不理解这种求次小生成树的算法,看了很多的解释

,还是纠结于,为什么次小生成树一定是最小生成树修改一条边得到的......如果有大神偶然见到弱渣的博客,求解答...

/*
http://blog.csdn.net/liuke19950717
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=1005;
int map[maxn][maxn];
bool vis[maxn];
int low[maxn];
int mst[maxn][maxn],pre[maxn];
bool inmst[maxn][maxn];
int n,m;
void init()
{
	memset(map,inf,sizeof(map));
	for(int i=1;i<=n;++i)
	{
		map[i][i]=0;
	}
}
void slove()
{
	int mincost=0;
	int next,Min;
	memset(inmst,0,sizeof(inmst));
	memset(mst,0,sizeof(mst));
	memset(vis,0,sizeof(vis));
	for(int i=1;i<=n;++i)
	{
		low[i]=map[1][i];
		pre[i]=1;
	}
	vis[1]=1;
	for(int i=2;i<=n;++i)
	{
		Min=inf;next=-1;
		for(int j=1;j<=n;++j)
		{
			if(!vis[j]&&Min>low[j])
			{
				next=j;
				Min=low[j];
			}
		}
		mincost+=Min;
		vis[next]=1;
		int fa=pre[next];
		inmst[next][fa]=inmst[fa][next]=1;
		for(int j=1;j<=n;++j)
		{
			if(vis[j]&&j!=next)
			{
				mst[j][next]=mst[next][j]=max(mst[fa][j],low[next]);
			}
			if(!vis[j]&&low[j]>map[next][j])
			{
				low[j]=map[next][j];
				pre[j]=next;
			}
		}
	}
	for(int i=1;i<=n;++i)
	{
		for(int j=1;j<i;++j)
		{
			if(map[i][j]!=inf&&!inmst[i][j])
			{
				if(map[i][j]==mst[i][j])
				{
					printf("Not Unique!\n");
					return;
				}
			}
		}
	}
	printf("%d\n",mincost);
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		init();
		for(int i=1;i<=m;++i)
		{
			int a,b,c;
			scanf("%d%d%d",&a,&b,&c);
			map[a][b]=map[b][a]=min(map[a][b],c);
		}
		slove();
	}
	return 0;
}


你可能感兴趣的:(Poj 1679 The Unique MST【次小生成树】)