POJ-1679.The Unique MST.(Prim求次小生成树)
The Unique MST
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 39561 | Accepted: 14444 |
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
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
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + , maxe = * / + , INF = 0x3f3f3f3f;
int n, m, lowc[maxn], pre[maxn], cost[maxn][maxn], Max[maxn][maxn];
bool vis[maxn], used[maxn][maxn]; int Prim(int source) {
int ans = ;
memset(vis, false, sizeof vis);
memset(Max, , sizeof Max);
memset(used, false, sizeof used);
for(int i = ; i <= n; i ++) {
lowc[i] = cost[source][i];
pre[i] = source;
}
pre[source] = -;
lowc[source] = ;
vis[source] = true;
for(int i = ; i <= n; i ++) {
int MIN = INF, k = -;
for(int j = ; j <= n; j ++)
if(!vis[j] && MIN > lowc[j]) {
MIN = lowc[j];
k = j;
}
if(MIN == INF) return -;
vis[k] = true;
ans += MIN;
used[pre[k]][k] = used[k][pre[k]] = true;//这里记得要把现在访问的边进行标记
for(int j = ; j <= n; j ++) {
if(vis[j] && j != k)
Max[k][j] = Max[j][k] = max(Max[pre[k]][j], lowc[k]);//每次加入一个顶点,就将这个顶点到达其他顶点路径上的最大边权进行更新
//为什么要这样更新呢?我们知道一个顶点在还没有加入最小生成树时它距离MST中各边顶点的最小值可以由它的父亲结点到j结点和它本身到MST结点的最小值的最大值来表示
if(!vis[j] && lowc[j] > cost[k][j]) {
lowc[j] = cost[k][j];
pre[j] = k;
}
}
}
return ans;
} int Second_Prim(int MST) {
int ans = INF;
for(int i = ; i <= n; i ++)
for(int j = + i; j <= n; j ++)
if(!used[i][j] && cost[i][j] != INF)
ans = min(ans, MST - Max[i][j] + cost[i][j]);
return ans;
} int main () {
int t, u, v, w;
scanf("%d", &t);
while(t --) {
scanf("%d %d", &n, &m);
memset(cost, INF, sizeof cost);
for(int i = ; i <= m; i ++) {
scanf("%d %d %d", &u, &v, &w);
cost[u][v] = cost[v][u] = w;
}
int MST = Prim();
int Second_MST = Second_Prim(MST);
if(Second_MST > MST)
printf("%d\n", MST);
else printf("Not Unique!\n");
}
return ;
}
POJ-1679.The Unique MST.(Prim求次小生成树)的更多相关文章
- POJ 1679 The Unique MST 【最小生成树/次小生成树模板】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
- poj 1679 The Unique MST 【次小生成树】【模板】
题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...
- poj 1679 The Unique MST(唯一的最小生成树)
http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submis ...
- poj 1679 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
题目连接 http://poj.org/problem?id=1679 The Unique MST Description Given a connected undirected graph, t ...
- 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(次小生成树)
题意:求解最小生成树的权值是否唯一,即要我们求次小生成树的权值两种方法求最小生成树,一种用prim算法, 一种用kruskal算法 一:用prim算法 对于给定的图,我们可以证明,次小生成树可以由最小 ...
- POJ 1679 The Unique MST (最小生成树)
The Unique MST 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/J Description Given a conn ...
- poj 1679 The Unique MST 【次小生成树+100的小数据量】
题目地址:http://poj.org/problem?id=1679 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 Outpu ...
随机推荐
- FFmpeg从入门到出家(HEVC在RTMP中的扩展)
由金山云视频云技术团队提供:FFmpeg从入门到出家第三季: 为推进HEVC视频编码格式在直播方案中的落地,经过CDN联盟讨论,并和主流云服务厂商达成一致,规范了HEVC在RTMP/FLV中的扩展,具 ...
- 获取Http请求IP的工具类
public class IpAddressUtil { public static String getIpAddr(HttpServletRequest request){ String ipAd ...
- nextSibling 属性与 nextElementSibling 属性的异同
不同点: nextSibling 属性返回元素节点之后的下一个兄弟节点(包括文本节点.注释节点): nextElementSibling 属性只返回元素节点之后的下一个兄弟元素节点(不包括文本节点.注 ...
- zabbix3.4.8中提示host [4gronghe_110] not found
查看zabbix_agentd.log时出现下列错误 [root@4gronghe_110 ~]# tail /var/log/zabbix/zabbix_agentd.log 1266:2014 ...
- Orabbix无法获取Oracle DB Size和DB Files Size的解决方法
Orabbix无法获取Oracle DB Size和DB Files Size的解决方法 这几天在研究Orabbix时发现在Zabbix中无法获取DB Size和DB Files Size的大小,后来 ...
- 英语单词leading
leading 来源——https://www.docker.com/products/docker-hub 翻译 a. 领导的,指导的:最主要的 词根词缀词源 leader汉语英译为了“领导”
- php strchr()函数 语法
php strchr()函数 语法 作用:搜索字符串在另一字符串中的第一次出现.直线电机哪家好 语法:strchr(string,search,before_search); 参数: 参数 描述 st ...
- Android逆向之旅---静态分析技术来破解Apk
一.前言 从这篇文章开始我们开始我们的破解之路,之前的几篇文章中我们是如何讲解怎么加固我们的Apk,防止被别人破解,那么现在我们要开始破解我们的Apk,针对于之前的加密方式采用相对应的破解技术,And ...
- PHP 最全的正则表达式
一.校验数字的表达式 1 数字:^[0-9]*$2 n位的数字:^\d{n}$3 至少n位的数字:^\d{n,}$4 m-n位的数字:^\d{m,n}$5 零和非零开头的数字:^(0|[1-9][0 ...
- 前端每日实战:124# 视频演示如何用纯 CSS 创作一只纸鹤
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/xagoYb 可交互视频 此视频是可 ...