The Unique MST POJ - 1679 (次小生成树)
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! 求次小生成树 看与最小生成树是否相同
prime求次小生成树
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn = , INF = 0x7fffffff;
typedef long long LL;
int graph[][], d[maxn], vis[maxn], maxd[][], pre[maxn];
int n, m; int prime(int s)
{
int temp, sum = ;
mem(vis, );
for(int i=; i<=n; i++) d[i] = graph[s][i], pre[i] = s;
vis[s] = ;
d[s] = ;
for(int i=; i<n; i++)
{
int mincost = INF;
for(int j=; j<=n; j++)
{
if(!vis[j] && mincost > d[j])
mincost = d[j], temp = j;
}
for(int j=; j<=n; j++)
if(vis[j]) maxd[temp][j] = maxd[j][temp] = max(mincost, maxd[pre[temp]][j]);
vis[temp] = ;
sum += mincost;
for(int j=; j<=n; j++)
{
if(!vis[j] && d[j] > graph[temp][j])
d[j] = graph[temp][j], pre[j] = temp;
}
}
// for(int i=1; i<=n; i++)
// sum += d[i];
return sum;
} int main()
{
int T;
cin>> T;
while(T--)
{
cin>> n >> m;
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
if(i == j) graph[i][j] = ;
else graph[i][j] = graph[j][i] = INF;
for(int i=; i<m; i++)
{
int u, v, w;
cin>> u >> v >> w;
graph[u][v] = graph[v][u] = w;
}
int sum = prime();
int lsum = INF;
for(int i=; i<=n; i++)
for(int j=i+; j<=n; j++)
{
if(i != pre[j] && j != pre[i] && graph[i][j] != INF)
if(sum - maxd[i][j] + graph[i][j] < lsum)
lsum = sum - maxd[i][j] + graph[i][j];
} if(lsum == sum)
cout<< "Not Unique!" <<endl;
else
cout<< sum <<endl; } return ;
}
The Unique MST POJ - 1679 (次小生成树)的更多相关文章
- The Unique MST POJ - 1679 次小生成树prim
求次小生成树思路: 先把最小生成树求出来 用一个Max[i][j] 数组把 i点到j 点的道路中 权值最大的那个记录下来 used数组记录该条边有没有被最小生成树使用过 把没有使用过的一条边加 ...
- Day5 - G - The Unique MST POJ - 1679
Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...
- POJ 1679 The Unique MST 【最小生成树/次小生成树模板】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
- poj1679 The Unique MST(判定次小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23180 Accepted: 8235 D ...
- POJ-1679.The Unique MST.(Prim求次小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39561 Accepted: 14444 ...
- poj 1679 次小生成树
次小生成树的求法: 1.Prime法 定义一个二维数组F[i][j]表示点i到点j在最小生成树中的路径上的最大权值.有个知识就是将一条不在最小生成树中的边Edge加入最小生成树时,树中要去掉的边就是E ...
- K - The Unique MST - poj 1679
题目的意思已经说明了一切,次小生成树... ****************************************************************************** ...
- (最小生成树 次小生成树)The Unique MST -- POJ -- 1679
链接: http://poj.org/problem?id=1679 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#probl ...
- The Unique MST POJ - 1679 最小生成树判重
题意:求一个无向图的最小生成树,如果有多个最优解,输出"Not Unique!" 题解: 考虑kruskal碰到权值相同的边: 假设点3通过边(1,3)连入当前所维护的并查集s. ...
随机推荐
- 12,13,14节-51单片机ESP8266学习-AT指令(暂停更新)需要整理
从这一节开始,以视频加源码的形式,后期视频和程序将放在链接中 资料链接 链接: https://pan.baidu.com/s/1jpHZjW_7pQKNfN9G4B6ZjA 密码:nhn3 ...
- Unity报错 GameObject is already being activated or deactivated
unity 在OnDisable 方法里设置父节点会报这个错. void OnDisable() { // transform.parent = oldParent; transform.SetPar ...
- Luogu4137 Rmq problem/mex 主席树
传送门 用主席树水莫队题…… 我们对于前缀和建立主席树,对于主席树中的每一个叶子节点表示它对应的数字最后出现的位置的编号,非叶子节点求左右节点的最小值,那么对于每一次询问$l,r$就是在第$r$棵主席 ...
- HTML 图片轮播制作工具
下载地址:http://wowslider.com/download/wowslider-win-setup.zip?utm_source=free_downl_win&utm_medium= ...
- Luogu P2522 [HAOI2011]Problem b
如果你做过[Luogu P3455 POI2007]ZAP-Queries就很好办了,我们发现那一题求的是\(\sum_{i=1}^a\sum_{j=1}^b[\gcd(i,j)=d]\),就是这道题 ...
- RHEL7VIM编辑器
本文介绍Vim编辑器的使用 vi和vim的区别 它们都是多模式编辑器 不同的是vim是vi的升级版本 它不仅兼容vi的所有指令而且还有一些新的特性在里面 vim的这些优势主要体现在以下几个方面 多级撤 ...
- OpenBLAS简介及在Windows7 VS2013上源码的编译过程
OpenBLAS(Open Basic Linear Algebra Subprograms)是开源的基本线性代数子程序库,是一个优化的高性能多核BLAS库,主要包括矩阵与矩阵.矩阵与向量.向量与向量 ...
- ASP.NET Core使用log4net记录日志
.NET常用的日志组件有NLog.Log4net等,.NET CORE下微软也自带了日志组件,到目前为止还没用过,而我本人常用的是log4net,下面简单讲讲.NET CORE下怎么使用log4net ...
- 学习ML.NET(3): 导入数据集
机器学习算法需要作用于数据,用来训练算法模型.数据集通常是以纯文本文件存储的表格数据,文件的每一行是一条数据记录,每条记录由多列组成,列之间用分隔符(一般是逗号,)分开,例如前面用到过的鸢尾花数据集. ...
- Spring Boot(十八):使用 Spring Boot 集成 FastDFS
上篇文章介绍了如何使用 Spring Boot 上传文件,这篇文章我们介绍如何使用 Spring Boot 将文件上传到分布式文件系统 FastDFS 中. 这个项目会在上一个项目的基础上进行构建. ...