次小生成树 判断 unique MST
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
Output
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!
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
#include<vector>
#include<iomanip>
#include<iostream>
using namespace std;
#define MAXN 101
#define INF 0x3f3f3f3f
/*
判断最小生成树是否唯一。
求次小生成树,若两个权值相等说明not unique
次小生成树算法,在prim()算法求解的时候,求出MST中u到v最大边权值
,然后用不在MST中的边依次枚举取最小值
*/
int g[MAXN][MAXN],Max[MAXN][MAXN],lowcost[MAXN],pre[MAXN],n,m,t;
bool used[MAXN][MAXN],been[MAXN];
int Prim()
{
int ret = ;
memset(been,false,sizeof(been));
memset(Max,,sizeof(Max));
memset(used,false,sizeof(used));
been[] = true;
pre[] = -;
for(int i=;i<=n;i++)
{
pre[i] = ;
lowcost[i] = g[][i];
}
lowcost[] = ;
for(int i=;i<n;i++)
{
int minc = INF,k =- ;
for(int j=;j<=n;j++)
{
if(!been[j]&&lowcost[j]<minc)
{
minc = lowcost[j];
k = j;
}
}
if(k==-) return -;
been[k] = true;
ret+=minc;
used[k][pre[k]] = used[pre[k]][k] = true;
for(int j=;j<=n;j++)
{
if(been[j])
Max[j][k] = Max[k][j] = max(Max[j][pre[k]],lowcost[k]);
if(!been[j]&&lowcost[j]>g[k][j])
{
lowcost[j] = g[k][j];
pre[j] = k;
}
}
}
return ret;
}
int cixiao(int ans)
{
int tmp = INF;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
if(!used[i][j]&&g[i][j]!=INF)
tmp = min(tmp,ans-Max[i][j]+g[i][j]);
}
if(tmp==INF)
return -;
return tmp;
}
int main()
{
cin>>t;
while(t--)
{
cin>>n>>m;
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
g[i][j] = INF;
}
int x,y,d;
for(int t=;t<m;t++)
{
cin>>x>>y>>d;
g[x][y] = g[y][x] = d;
}
int ans = Prim();
int tmp = cixiao(ans);
if(tmp==ans||ans==-)
cout<<"Not Unique!\n";
else
cout<<ans<<endl;
}
return ;
}
次小生成树 判断 unique MST的更多相关文章
- POJ 1679 The Unique MST (次小生成树 判断最小生成树是否唯一)
题目链接 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. De ...
- POJ-1679 The Unique MST(次小生成树、判断最小生成树是否唯一)
http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its minimum s ...
- The Unique MST(次小生成树)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22335 Accepted: 7922 Description Give ...
- poj 1679 The Unique MST【次小生成树】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24034 Accepted: 8535 D ...
- poj 1679 判断MST是不是唯一的 (次小生成树)
判断MST是不是唯一的 如果是唯一的 就输出最小的权值和 如果不是唯一的 就输出Not Unique! 次小生成树就是第二小生成树 如果次小生成树的权值和MST相等 那么MST就不是唯一的 法一: ...
- POJ 1679 The Unique MST (次小生成树)题解
题意:构成MST是否唯一 思路: 问最小生成树是否唯一.我们可以先用Prim找到一棵最小生成树,然后保存好MST中任意两个点i到j的这条路径中的最大边的权值Max[i][j],如果我们能找到一条边满足 ...
- POJ_1679_The Unique MST(次小生成树)
Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...
- POJ 1679 The Unique MST 【最小生成树/次小生成树模板】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
- poj 1679 The Unique MST 【次小生成树+100的小数据量】
题目地址:http://poj.org/problem?id=1679 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 Outpu ...
随机推荐
- Coding Pages 服务与万网域名的配置
1071220 http://support.huawei.com/learning/NavigationAction!createNavi?navId=MW000001_term1000190292 ...
- MySQL的两种存储引擎storage engine特点和对比
MyISAM 优点:快速读取数据, 占用空间小 缺点:不支持事务,外键 (表级别锁) InnoDB 优点:支持事务,外键; 高性能(CPU效率高) 缺点: 慢,占空间 (行级别锁)
- MySQL与MongoDB的操作对比
MySQL与MongoDB都是开源的常用数据库,但是MySQL是传统的关系型数据库,MongoDB则是非关系型数据库,也叫文档型数据库,是一种NoSQL的数据库.它们各有各的优点,关键是看用在什么地方 ...
- 全3D模具设计自动化解決方案
- Socket编程的简单实现
关于socket编程的简单实现,主要分成客户端.服务端两个部分.实现如下: 1.服务端代码如下,注意:server端要优先于client端启动 2.client端代码,以及启动后客户端和服务端之间的简 ...
- (转)淘淘商城系列——使用Spring来管理Redis单机版和集群版
http://blog.csdn.net/yerenyuan_pku/article/details/72863323 我们知道Jedis在处理Redis的单机版和集群版时是完全不同的,有可能在开发的 ...
- 梦想MxWeb3D协同设计平台 2019.01.24更新
SDK开发包下载地址:http://www.mxdraw.com/ndetail_10124.html1. 编写快速入门教程2. 重构前端代码,支持一个页面多个三维控件同时加载,或二维和三维同时加 ...
- freemarker使用map替换ftl中相关值
ftl文件demo01.ftl <html> <head> <title>Welcome!</title> </head> <body ...
- contab的使用方法
linux 系统则是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工作,因此这个系统服务是默认启动的.另 外, 由于使用者自己也可以设置计划任务,所以, ...
- eBPF监控工具bcc系列五工具funccount
eBPF监控工具bcc系列五工具funccount funccount函数可以通过匹配来跟踪函数,tracepoints 或USDT探针.例如所有以vfs_ 开头的内核函数. ./funccount ...