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 ...
随机推荐
- Web学习之JS总结
银角大王武Sir的博客地址 银角大王武Sir的博客地址二 1.Javascript的作用域链 由于javascript没有块级作用域,而且每个函数作为一个作用域,如果出现嵌套函数,则就会出现作用域链 ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) - C
题目链接:http://codeforces.com/contest/831/problem/C 题意:给定k个评委,n个中间结果. 假设参赛者初始分数为x,按顺序累加这k个评委的给分后得到k个结果, ...
- 【改】DOS文件格式转UNIX文件格式
windows中的文本文件的换行符是"\r\n",而linux中是"\n",dos格式文件传输到unix系统时,会在每行的结尾多一个^M,当然也有可能看不到,但 ...
- MongoDB的应用
一.MongoDB后台管理 # ./mongo MongoDB shell version v3.4.2 connecting to: mongodb://127.0.0.1:27017 MongoD ...
- ps:选区的存储及载入
有时候需要把已经创建好的选区存储起来,方便以后再次使用.就要使用选区存储功能. 创建选区后,直接点击右键(限于选取工具)出现的菜单中就“存储选区”项目.也可以使用菜单[选择 存储选区].会出现一个名称 ...
- ps:界面概览
首先我们来认识一下Photoshop的界面组成,如下图是一个典型的界面.为了方便识别,我们加上了颜色和数字. 1:顶部的红色区域是菜单栏,包括色彩调整之类的命令都存放在从菜单栏中.在我们的教程中使用[ ...
- 解决Debug JDK source 无法查看局部变量的问题方案
一.问题阐述首先我们要明白JDK source为什么在debug的时候无法观察局部变量,因为在jdk中,sun对rt.jar中的类编译时,去除了调试信息,这样在eclipse中就不能看到局部变量的值. ...
- Java面向对象(二) 接口、多态和泛型
一.接口 二.多态 多态是同一个行为具有多个不同表现形式或形态的能力. 2.1 类型转换 转换方式 隐式 向上转型 对于基本数据类型,存储容量低的可自动向存储容量高的类型转换 对于引用变量,子类可被转 ...
- 【leetcode】1033. Moving Stones Until Consecutive
题目如下: Three stones are on a number line at positions a, b, and c. Each turn, you pick up a stone at ...
- File类常用的方法与字节流类方法简介
File类常用的方法 获取功能的方法 public String getAbsolutePath() :返回此File的绝对路径名字符串. public String getPath() :将此Fil ...