POJ_1679_The Unique MST(次小生成树模板)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 23942 | Accepted: 8492 |
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
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
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!
题意:问最小生成树是否唯一。
分析:求次小生成树,推断次小生成树和最小生成树是否相等。留作模板。
次小生成树的步骤:
(1)先用Prime求出最小生成树T,在Prime的同一时候用一个矩阵max_edge[u][v]记录在T中连接随意两点u,v的唯一路径中权
值最大的那条边的权值。注意这里是非常easy做到的。由于Prime是每次添加一个节点t。而设已经标了号的节点集合为S,则S
中全部节点到t的路径中最大权值的边就是当前增加的这条边。
(2)枚举最小生成树以外的边,并删除该边所在环上权值最大的边。
(3)取得的全部生成树中权值最小的一棵即为所求。
算法的时间复杂度为O(n^2)。
题目链接: id=1679">http://poj.org/problem?id=1679
代码清单:
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<string>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std; typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull; const int maxn = 100 + 5;
const int maxv = 10000 + 5;
const int max_dis = 1e9 + 5; int T;
int n,m;
int a,b,c;
int MST,_MST;
bool vis[maxn];
int father[maxn];
int dist[maxn];
int graph[maxn][maxn];
bool used[maxn][maxn];
int max_edge[maxn][maxn]; void init(){
memset(vis,false,sizeof(vis));
memset(used,false,sizeof(used));
memset(max_edge,-1,sizeof(max_edge));
memset(graph,0x7f,sizeof(graph));
} void input(){
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
graph[a][b]=graph[b][a]=c;
used[a][b]=used[b][a]=true;
}
} int prim(){
int ans=0;
dist[1]=0;
vis[1]=true;
father[1]=-1;
for(int i=2;i<=n;i++){
father[i]=1;
dist[i]=graph[1][i];
}
for(int i=1;i<n;i++){
int v=-1;
for(int j=1;j<=n;j++){
if(!vis[j]&&(v==-1||dist[j]<dist[v])) v=j;
}
ans+=dist[v];
vis[v]=true;
used[father[v]][v]=used[v][father[v]]=false;
for(int j=1;j<=n;j++){
if(vis[j]){
max_edge[v][j]=max_edge[j][v]=max(max_edge[father[v]][j],dist[v]);
}
else{
if(graph[v][j]<dist[j]){
dist[j]=graph[v][j];
father[j]=v;
}
}
}
}return ans;
} int second_prim(){
int ans=max_dis;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(used[i][j]) ans=min(ans,MST+graph[i][j]-max_edge[i][j]);
return ans;
} void solve(){
MST=prim();
_MST=second_prim();
if(MST==_MST) printf("Not Unique!\n");
else printf("%d\n",MST);
} int main(){
scanf("%d",&T);
while(T--){
init();
input();
solve();
}return 0;
}
POJ_1679_The Unique MST(次小生成树模板)的更多相关文章
- poj1679The Unique MST(次小生成树模板)
次小生成树模板,别忘了判定不存在最小生成树的情况 #include <iostream> #include <cstdio> #include <cstring> ...
- 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 Description Given a connected undirec ...
- POJ1679 The Unique MST —— 次小生成树
题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total S ...
- POJ1679 The Unique MST[次小生成树]
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28673 Accepted: 10239 ...
- 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 有t组数据,给你n个点,m条边,求是否存在相同权值的最小生成树(次小生成树的权值大小等于最小生成树). 先求出最小生成树的大小, ...
- 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 ...
随机推荐
- 2199. [HZOI 2016] 活动投票
★★ 输入文件:hztp.in 输出文件:hztp.out 简单对比 时间限制:0.5 s 内存限制:2 MB [题目描述] 衡中活动很多,人也很多,一次活动有n个学生参与投票,现已知 ...
- python--12、数据库进阶
SQL语句关键词: #再次不做过多介绍 使用INSERT实现数据的插入 UPDATE实现数据的更新 使用DELETE实现数据的删除 使用SELECT查询数据以及. #示例中department为部门表 ...
- Windows下压缩成tar.gz格式
tar.gz 是linux和unix下面比较常用的格式,几个命令就可以把文件压缩打包成tar.gz格式,然而这种格式在windows并不多见,WinRAR.WinZip等主流压缩工具可以释放解开,却不 ...
- Magento 多站点多域名安装教程(可以设置手机模版哟,亲 \(^o^)/)
这篇文章是安装magento子域名的教程,请先进行安装之前,确认以下几点: 1.请先确认子域名是否已经指向你的服务器 2.可以编辑.htaccess文件 3. 熟悉Cpanel操作 我们的目标是建立一 ...
- (转)淘淘商城系列——使用Jedis操作集群
http://blog.csdn.net/yerenyuan_pku/article/details/72862084 通过上文的学习,我相信大家应该已经知道如何搭建Redis集群了,本文我将为大家介 ...
- 扩增子分析解读6进化树 Alpha Beta多样性
分析前准备 # 进入工作目录 cd example_PE250 上一节回顾:我们的OTU获得了物种注释,并学习OTU表的各种操作————添加信息,格式转换,筛选信息. 接下来我们学习对OTU序列的 ...
- 当From窗体中数据变化时,使用代码获取数据库中的数据然后加入combobox中并且从数据库中取得最后的结果
private void FormLug_Load(object sender, EventArgs e) { FieldListLug.Clear();//字段清除 DI = double.Pars ...
- 数组array的常用方法简介
数组方法简介 数组总共有22种方法,本文将其分为以下几类来进行详细介绍. 原数组变化:push() pop() shift() unshift() reverse() sort() splice() ...
- ie6,ie7,ie8,FF 浏览器兼容问题
javascript部分 1. document.form.item 问题问题:代码中存在 document.formName.item("itemName") 这样的语句,不能在 ...
- 服务器做ssh免秘钥登陆
集群内服务器做非root用户免秘钥登陆:1.node1新建用户abc1,制作公钥.私钥(一路回车键即可)ssh-keygen –t rsa将自动在/home/abc1/.ssh/目录下创建公私钥文件如 ...