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 ...
随机推荐
- _bzoj1031 [JSOI2007]字符加密Cipher【后缀数组】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1031 保存后缀数组模版. 其实如果数据范围小一点,或者空间限制再大一点,或者甚至只要字母表再 ...
- Oracle10g修改数据库字符集
Oracle10g修改字符集记录: 版本:Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bit Production 参考 ...
- UML 用例图(转载)
UML是系统架构设计师考试的一个重要考点,需要考生掌握.但是有些考生,在学习的过程中会有这样的疑问,在敏捷开发时代,UML还有没有必要去学习? UML还是有用的,主要用在设计和分析阶段,但是UML不适 ...
- 聊5块钱P2V
上一秒还在写代码,下一秒就跑机房干活. 这台机器产制石器时代,重启一次后再就启动不了了.这个故障处理的方式我们以后再谈. 今天聊聊啥是P2V,国人总喜欢弄些稀奇古怪的定义来证明自己技术很牛X,就跟当年 ...
- vue2.0 自定义指令详解
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 中国版 Office 365 (X-Tenant / Tango) 功能验证报告 - 1 简介
花了点时间做了一次Office 365 X-Tenant的 POC,对过程做了记录和总结,在这里会陆续分享: (一) 简介 这次POC的系统环境是模拟一个公司的生产环境: 1. 公司总部在国外,拥有 ...
- SweetAlert如何实现点击Confirm之后自动关闭
swal({ title: "Are you sure?", text: "You will not be able to recover this imaginary ...
- 数据库和java的类型转化
mysql 的转化 count(1) 返回类型是bigint 对应的java 是 BigInteger cast(sum(name) as char) sum转化为字符串
- opencv读图片错误,已解决
could not loag image... terminate called after throwing an instance of 'cv::Exception' what(): OpenC ...
- viewstate 与 session 区别
Session存在于服务器端可以跨页面.耗费服务器资源.ViewState载体是页面文件,消耗带宽. EG: viewstate是存储在客户端的隐藏信息,当服务器接受请求回传时可以将viewstate ...