您的位置:首页 > 产品设计 > UI/UE

POJ 1679 The Unique MST 最小代价生成树 次小代价生成树

2016-09-12 23:42 423 查看
The Unique MST

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 28109 Accepted: 10030
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
    题意:给出边集,判断最小生成树是否唯一。

    分析:最小生成树和次小生成树问题。先使用普里姆算法求出最小生成树,在此过程中记录i到j路径上的边的最大值,然后用没有加入生成树的边依次加入,此时根据最小生成树的性质,肯定会形成环,删去记录的i到j路径上的最大值,用添加进去的边替代,此时就是必须使用该边生成的最小代价生成树,枚举所有没有加入MST的边生成对应的MST,其中最小的即是次小代价生成树,若最终生成的最小代价生成树和原来得到的MST
的值相同,则说明MST不唯一,否则则说明唯一。代码参考http://www.cnblogs.com/kuangbin/p/3147329.html 水平太低所以做了很多注释,见代码:

//判断最小生成树是否唯一
//可以求次小生成树  如果两者相等则不唯一
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int maxn=110;
const int INF=0x3f3f3f3f;
bool vis[maxn];//标记一个点是否已加入MST
int min_dis[maxn]; //min_dis [i]  指i这一点到生成树中点的最小距离
int pre[maxn];//标记一个点的上家
int Max[maxn][maxn];//Max[i][j]表示在最小生成树中从i到j的路径中的最大边权
int map[maxn][maxn];//边
bool used[maxn][maxn];//标记一条边是否加入了MST
int n,m;
int Prim()
{
//printf("prim");
int ans=0;
memset(vis,false,sizeof(vis));
memset(Max,0,sizeof(Max));
memset(used,false,sizeof(used));
vis[0]=true;//生成树中加入0点
pre[0]=-1;
for(int i=1; i<n; i++)
{
min_dis[i]=map[0][i]; //每一点到此时生成树中最小距离是到0的距离
pre[i]=0;
}
min_dis[0]=0;
for(int i=1; i<n; i++)//没次循环加入一个点  总共循环n-1次
{
int minc=INF;
int p=-1;
for(int j=0; j<n; j++)
if(!vis[j]&&minc>min_dis[j])//从未加入生成树的点中选出 到生成树中点距离最小的那个点
{
minc=min_dis[j];//记录该最小值
p=j;
}
if(minc==INF)//如果 该最小值 为INF 则说明 图不连通
return -1;
ans+=minc;//生成最小代价生成树的权值
vis[p]=true;//标记该点访问
used[p][pre[p]]=used[pre[p]][p]=true;//两条边都已使用
for(int j=0; j<n; j++)
{
//求出 j 到 p路径上权值最大的边
//其他的点如果想到达 新加入的点 则必须通过它的上家到达
//之前已经确定好了 各点到其上家的路径上  最大的边值
//所以只需要比较已经确定好的最大的边值 和新加入的边值
//用于更新新加入点到MST各点路径上最大的边值
if(vis[j])//更新生成树中 访问过的点 到该点的最大距离
Max[j][p]=Max[p][j]=max(Max[j][pre[p]],min_dis[p]);
if(!vis[j]&&min_dis[j]>map[p][j])
{
//如果仍未访问的点 到之前生成树中的最小距离大于到新加入的点的最小距离的话
//则更新
//该步就是更新仍未加入的点到已加入点集合的最小距离
min_dis[j]=map[p][j];
pre[j]=p;//它的上家是新加入的p点
}
}
}
return ans;
}

int smst(int ans)
{
int Min=INF;
for(int i=0; i<n; i++)
for(int j=i+1; j<n; j++)
if(map[i][j]!=INF && !used[i][j])//两点连通  并且该点没有使用过
//即 是没有加入MST 的边
Min=min(Min,ans+map[i][j]-Max[i][j]);//选择一个 未加入边 并删去原的MST 上的边
//加入边形成环后 减去 i到j 路径上的最大 边
if(Min==INF)
return -1;//不存在
return Min;
}

int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
int u,v,w;
for(int i=0; i<n; i++) //初始化数组
for(int j=0; j<n; j++)
{
if(i==j)
map[i][j]=0;
else
map[i][j]=INF;
}
while(m--)//输入边集
{
scanf("%d%d%d",&u,&v,&w);
u--; //默认设置的是以0为起点
v--;
map[u][v]=map[v][u]=w;
}
int ans=Prim();//生成MST
if(ans==-1)//无法生成MST 的情况
{
printf("Not Unique!\n");
continue;
}
if(ans==smst(ans)) //次小生成树  和最大生成树 如果相等的话则说明最大生成树唯一
printf("Not Unique!\n");
else
printf("%d\n",ans);
}
return 0;
}
    特记下,以备后日回顾。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: