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 ...
随机推荐
- 文件输入输出C++操作
基于C++的文件操作 在C++中,有一个stream这个类,所有的I/O都以这个"流"类为基础的,包括我们要认识的文件I/O,stream这个类有两个重要的运算符: 1.插入器(& ...
- XDocument
XDocument学习(Winform) using System; using System.Collections.Generic; using System.ComponentModel; us ...
- 原创 SqlParameter 事务 批量数据插入
不错,很好,以后防注入批量事务提交虽然麻烦点研究了几个小时,但不会是问题了 SqlCommand cmd; HelpSqlServer helps = new HelpSqlServer(); //定 ...
- SQL常用自定义函数
1.字符串转Table(Func_SplitToTable) CREATE FUNCTION [dbo].[Func_SplitToTable] ( @SplitString ...
- Android动态权限申请
Android系统中,目前Dangerous级别的权限都需要动态申请.步骤如下: 1.AndroidManfiest.xml中申明需要的动态权限 <?xml version="1.0& ...
- R Programming week1-Data Type
Objects R has five basic or “atomic” classes of objects: character numeric (real numbers) integer co ...
- VUE 入坑系列 一 事件
html代码 <div id="app"> <button v-on:click="counter += 1">加1</butto ...
- vue热重载
依据官网使用 webpack 的 Hot Module Replacement API,Vuex 支持在开发过程中热重载 mutation.module.action 和 getter.你也可以在 B ...
- 浅谈2015新版 U-Boot
过了挺长一断时间没有看U-BOOT了,这两天下载了新版的UBOOT源码(之前看的一些书都是基于早好多年的源码来讲述,总感觉心里有点不对劲,也许是我比较喜新的原因吧,不过小弟我并没有厌旧哈),好了不多扯 ...
- 迅为4412嵌入式安卓开发板兼容3G网络|4G网络
iTOP-Exynos4412开发板内置有无线 WIFI 模块.Bluetooth.GPS.Camera.3G等模组,陀螺仪等,支持 HDMI1.4(1080P/60Hz)显示,客户可以直接从开发平台 ...