poj 1679(次小生成树)

The Unique MST
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 25369 Accepted: 9055

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!
 
 
 
 
次小生成树的两种方法:
1:用求最小生成树的方法,求出最小生成树,并记录下该最小生成树上的所有边!
对该生成树上的所有边都进行枚举。对边集中,除了生成树上某条边的集合,构造最小生成树,并记录下最小值!
当生成树上的边枚举完,得到的值的大小就是次小生成树的大小!
2:
首先求出原图最小生成树,权值之和为min
枚举添加每条不在最小生成树上的边 ,加上以后一定会形成一个环。
找到环上除了(u,v)以外的权值最大的边,把它删掉,计算当前生成树的权值之和。
枚举完所有边之后,得到的最小值即为次小生成树的权值。
具体实现时,更简单的方法是从每个节点i遍历整个最小生成树
定义F[i][j]为在生成树上,从i到j的路径上最大的边的权值。通过BFS,求出F[i][j]的值
然后对于添加每条不在最小生成树中的边(i,j),并删去该环中的原生成树的的最大边。
新的生成树权值之和就是Min + w(i,j) – F[i][j]
记录其最小值,则为次小生成树。

AC:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int maxn = 500;
const int inf = 0x7ffff;
int n,m,cost[maxn][maxn],low[maxn],belong[maxn];
bool vis[maxn],used[maxn][maxn];

int Prim()
{
	int k = 1,MIN,ans = 0;
	memset(vis,false,sizeof(vis));
	memset(used,false,sizeof(used));
	vis[k] = true;
	for(int i = 1; i <= n; i++)
	{
		low[i] = cost[k][i];
		belong[i] = k;
	}
	for(int i = 1; i < n; i++)
	{
		MIN = inf;
		for(int j = 1; j <= n; j++)
		{
			if(vis[j] == false && low[j] < MIN)
			{
				MIN = low[j];
				k = j;
			}
		}
		vis[k] = true;
		used[k][belong[k]] = used[belong[k]][k] = true;
		for(int j = 1; j <= n; j++)
		{
			if(vis[j] == false && low[j] > cost[k][j])
			{
				low[j] = cost[k][j];
				belong[j] =k;
			}
		}
		ans += MIN;
	}
	return ans;
}

void Methord()
{
	int ans = Prim();
	int MIN = inf;
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= n; j++)
		{
			if(i != j && used[i][j] == false && cost[i][j] != inf)
			{
				MIN = min(MIN,ans - cost[i][belong[i]] + cost[i][j]);
				if(MIN < ans) MIN = inf;
				MIN = min(MIN,ans - cost[j][belong[j]] + cost[i][j]);
				if(MIN < ans) MIN = inf;
			}
		}
	if(MIN == ans) cout<<"Not Unique!"<<endl;
	else cout<<ans<<endl;
}

int main()
{	
	int t;
	cin>>t;
	while(t--)
	{
		int a,b,l;
		cin>>n>>m;
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= n; j++)
				cost[i][j] = inf;
		for(int i = 1; i <= m; i++)
		{
			cin>>a>>b>>l;
			cost[a][b] = cost[b][a] = l;
		}
		Methord();
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值