POJ-1679 The Unique MST,次小生成树模板题
Time Limit: 1000MS | Memory Limit: 10000K | |
Description
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
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
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
接触的第一道次小生成树的题,还是有很多不足。
题意:t组测试数据,n个点,m条边,求最小生成树是否唯一。
思路:我们知道最小生成树再加入任何一条边都会成环,而且新加的这条边绝对不小于环中的任何一条边。次小生成树就是第二小生成树,与最小生成树差值最小。也就是新加入的边与环中最大边差距尽量小,我们在求最小生成树的时候可以将任意两点间的最大边用邻接矩阵存起来,然后依次遍历所有不在MST中的边替换生成树中环中最大的边。然后得出的最小值便是次小生成树。这里只需判断求出的次小生成树是否等于最小生成树。是则Not
Unique!
const int INF=0x3f3f3f3f;
const int N=200+10;
int n,m,d[N],w[N][N],ma[N][N],vis[N],pre[N],used[N][N];
int prim()
{
memset(vis,0,sizeof(vis));
memset(used,0,sizeof(used));
memset(ma,0,sizeof(ma));
for(int i=1;i<=n;i++) d[i]=w[1][i],pre[i]=1;
vis[1]=1;
pre[1]=-1;
int ans=0;
for(int i=2;i<=n;i++)
{
int x,m=INF;
for(int j=1;j<=n;j++)
if(!vis[j]&&m>d[j])
m=d[x=j];
vis[x]=1;
ans+=m;
used[pre[x]][x]=used[x][pre[x]]=1;
for(int j=1;j<=n;j++)
{
if(vis[j]) ma[j][x]=ma[x][j]=max(ma[pre[x]][j],d[x]);
if(!vis[j]&&d[j]>w[x][j])
d[j]=w[x][j],pre[j]=x;
}
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<N;i++)
for(int j=1;j<N;j++)
w[i][j]=i==j?0:INF;
int u,v,c;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&c);
w[u][v]=w[v][u]=c;
}
int ans=prim();
int res=INF;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(i!=j&&!used[i][j]&&w[i][j]!=INF)
res=min(res,ans+w[i][j]-ma[i][j]);//用不在MST中的边替换环中最大的边。
if(res==ans) printf("Not Unique!\n");
else printf("%d\n",ans);
}
return 0;
}
POJ-1679 The Unique MST,次小生成树模板题的更多相关文章
- POJ 1679 The Unique MST (次小生成树)
题目链接:http://poj.org/problem?id=1679 有t组数据,给你n个点,m条边,求是否存在相同权值的最小生成树(次小生成树的权值大小等于最小生成树). 先求出最小生成树的大小, ...
- 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 (次小生成树kruskal算法)
The Unique MST 时间限制: 10 Sec 内存限制: 128 MB提交: 25 解决: 10[提交][状态][讨论版] 题目描述 Given a connected undirect ...
- poj 1679 The Unique MST (次小生成树(sec_mst)【kruskal】)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 35999 Accepted: 13145 ...
- poj1679The Unique MST(次小生成树模板)
次小生成树模板,别忘了判定不存在最小生成树的情况 #include <iostream> #include <cstdio> #include <cstring> ...
- POJ_1679_The Unique MST(次小生成树模板)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23942 Accepted: 8492 D ...
- poj 1679 The Unique MST 【次小生成树】【模板】
题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...
- POJ1679 The Unique MST —— 次小生成树
题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total S ...
- POJ 1679 The Unique MST 【最小生成树/次小生成树模板】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
随机推荐
- PHP autoload实践
本文目的 本文简要的描述了PHP提供的autoload机制,以及在scake中使用实践.用于减少不必要的文件包含,提高php系统性能. 什么是__autoload php是脚本语言,不同于c++只需要 ...
- Join方法,yield方法,线程的优先级
- ubu下编译安装php7
第一步: 安装依赖库zlib.libpng.freetype.jpegsrc.libxml2.libgd.freetds.mhash.libmcrypt.mcrypt(依赖于mhash和libmcry ...
- [转]asp.net 跨域单点登录
本文转自:http://tech.e800.com.cn/articles/2009/814/1250212319986_1.html 单点登录(Single Sign On),简称为 SSO,是目前 ...
- 可以装一把——c#中手动添加控件
TextBox txt = new TextBox(); //文本框控件 //如果想在移动控件位置 point(x,y) txt.Location = new Point(50,50); this.C ...
- iOS 中集成海康威视 摄像视频
本文原文地址 http://www.cnblogs.com/qianLL/p/6652104.html 一.要导入相关的库,注意 这里比较坑的是 要用和他一样的 如果开始的工程中用了AFN或者MJE ...
- 后缀数组 (Suffix Array) 学习笔记
\(\\\) 定义 介绍一些写法和数组的含义,首先要知道 字典序 . \(len\):字符串长度 \(s\):字符串数组,我们的字符串存储在 \(s[0]...s[len-1]\) 中. \(suff ...
- 打包Scala jar 包的正确步骤
实验目的:打包可运行的scala jar,上传到spark集群,提交执行 1.idea中编译运行代码,可成功运行 2.修改2处代码//只配置appName,其他配置项注释掉val conf=new S ...
- CSS——BFC
http://www.cnblogs.com/lhb25/p/inside-block-formatting-ontext.html 元素若不是bfc,那么内部浮动元素的高度不参与计算 元素若不是bf ...
- PHP正则表达式考察点
正则表达式的作用 分隔.查找.匹配.替换字符串 正则表达式的组成部分 分隔符 "/" . "#" . "~" 通用原子 \d : 十进制的0 ...