本文主要是介绍POJ 1679 The Unique MST(次小生成树之一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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:
- V’ = V.
- 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!
题目大意
对于一个无向图,判断其最小生成树是否唯一,若唯一,输出最小生成树,若不唯一,输出Not Unique!。
解题思路
次小生成数求解,对于无向图,求次小生成树,判断其值是否与最小生成树相等,若相等,则最小生成树不唯一;相反,则唯一。
次小生成树求解思想:
1、先求出最小生成树,在这一过程中,标记出最小生成树包含的边,并且保存在最小生成树中从节点i到节点j路径中的最大边权Max[i][j];
2、选取不在最小生成树中的一条边,添加进去,那么一定会构成一个环,然后除去这个环中边(除了刚添加的边)的最大值(即Max[i][j]),当前仍是无向图的一个生成树且该生成树有可能是次小生成树;
3、重复步骤2,依次将不在最小生成树中的边遍历完之后,得到的最小值便是次小生成树。
代码实现
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 207
#define INF 0x3f3f3f3f
int cost[maxn][maxn],used[maxn][maxn];
int pre[maxn],lowcost[maxn];
int Max[maxn][maxn]; //Max[i][j]表示在最小生成树中从i到j的路径中的最大边权
bool vis[maxn];
int n,m,ans;
int Prim()
{memset(vis,0,sizeof(vis));memset(lowcost,INF,sizeof(lowcost));memset(Max,0,sizeof(Max));memset(used,0,sizeof(used));vis[0]=1; //先将节点0添加到最小生成树中pre[0]=-1;int an=0;for(int i=1; i<n; i++){lowcost[i]=cost[0][i];pre[i]=0;}for(int i=1; i<n; i++){int Min=INF;int p=-1;for(int j=0; j<n; j++) //寻找最短的边添加到最小生成树中if(!vis[j]&&Min>lowcost[j]){Min=lowcost[j];p=j;}an+=Min; //最小生成树的值vis[p]=1; //将节点p添加到最小生成树中used[p][pre[p]]=used[pre[p]][p]=1; //标记最小生成树包含的边for(int j=0; j<n; j++){if(vis[j])Max[j][p]=Max[p][j]=max(Max[j][pre[p]],lowcost[p]);else{if(lowcost[j]>cost[j][p]) //更新还未添加到最小生成树中的节点与现有的最小生成树的最短距离{lowcost[j]=cost[j][p];pre[j]=p;}}}}return an;
}
int smst()
{int cnt=INF;for(int i=0; i<n; i++){for(int j=i+1; j<n; j++){if(cost[i][j]!=INF&&!used[i][j]) //添加不在最小生成树的边,减去环中的最大边权,更新次小生成树{cnt=min(cnt,ans+cost[i][j]-Max[i][j]);}}}return cnt;
}
int main()
{int T;int u,v,l;scanf("%d",&T);while(T--){scanf("%d %d",&n,&m);for(int i=0; i<n; i++) //初始化for(int j=0; j<n; j++){if(i==j) cost[i][j]=0;else cost[i][j]=INF;}for(int i=0; i<m; i++){scanf("%d %d %d",&u,&v,&l);u--,v--; //节点从0开始cost[u][v]=cost[v][u]=l;}ans=Prim(); //求最小生成树if(ans==smst()) //求次小生成树进行比较printf("Not Unique!\n");elseprintf("%d\n",ans);}return 0;
}
这篇关于POJ 1679 The Unique MST(次小生成树之一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!