POJ1679 The Unique MST —— 次小生成树
题目链接:http://poj.org/problem?id=1679
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 31378 | Accepted: 11306 |
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
题解:
问:最小生成树是否唯一。
次小生成树模板题。
代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e2+; int cost[MAXN][MAXN], lowc[MAXN], pre[MAXN], Max[MAXN][MAXN];
bool vis[MAXN], used[MAXN][MAXN]; int Prim(int st, int n)
{
int ret = ;
memset(vis, false, sizeof(vis));
memset(used, false, sizeof(used));
memset(Max, , sizeof(Max)); for(int i = ; i<=n; i++)
lowc[i] = (i==st)?:INF;
pre[st] = st; for(int i = ; i<=n; i++)
{
int k, minn = INF;
for(int j = ; j<=n; j++)
if(!vis[j] && minn>lowc[j])
minn = lowc[k=j]; if(minn==INF) return -; //不连通
vis[k] = true;
ret += minn;
used[pre[k]][k] = used[k][pre[k]] = true; //pre[k]-k的边加入生成树
for(int j = ; j<=n; j++)
{
if(vis[j] && j!=k) //如果遇到已经加入生成树的点,则找到两点间路径上的最大权值。
Max[j][k] = Max[k][j] = max(Max[j][pre[k]], lowc[k]); //k的上一个点是pre[k]
if(!vis[j] && lowc[j]>cost[k][j]) //否则,进行松弛操作
{
lowc[j] = cost[k][j];
pre[j] = k;
}
}
}
return (ret==INF)?-:ret;
} int SMST(int t1 ,int n)
{
int ret = INF;
for(int i = ; i<=n; i++) //用生成树之外的一条边去代替生成树内的一条边
for(int j = i+; j<=n; j++)
{
if(cost[i][j]!=INF && !used[i][j]) //去掉了i-j路径上的某条边,但又把i、j直接连上,所以还是一棵生成树。
ret = min(ret, t1+cost[i][j]-Max[i][j]);
}
return ret;
} int main()
{
int T, n, m;
scanf("%d", &T);
while(T--)
{ scanf("%d%d",&n,&m);
for(int i = ; i<=n; i++)
for(int j = ; j<=n; j++)
cost[i][j] = (i==j)?:INF; for(int i = ; i<=m; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
cost[u][v] = cost[v][u] = w;
} int t1 = Prim(, n);
int t2 = SMST(t1, n);
if(t1!=- && t2!=- && t1!=t2) printf("%d\n", t1);
else printf("Not Unique!\n");
}
}
POJ1679 The Unique MST —— 次小生成树的更多相关文章
- POJ1679 The Unique MST[次小生成树]
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28673 Accepted: 10239 ...
- POJ-1679 The Unique MST,次小生成树模板题
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Description Given a connected undirec ...
- 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(次小生成树)
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 Total Submissions: 23942 Accepted: 8492 D ...
- POJ 1679 The Unique MST (次小生成树)
题目链接:http://poj.org/problem?id=1679 有t组数据,给你n个点,m条边,求是否存在相同权值的最小生成树(次小生成树的权值大小等于最小生成树). 先求出最小生成树的大小, ...
- poj1679The Unique MST(次小生成树模板)
次小生成树模板,别忘了判定不存在最小生成树的情况 #include <iostream> #include <cstdio> #include <cstring> ...
- 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 ...
随机推荐
- 【01】Firebug 教程
Firebug 教程 什么是 Firebug? Firebug 是一个开源的web开发工具. 现在浏览器自带firebug了. 安装 Firebug Firebug下载地址: https: ...
- [Go]接口的运用
在Go语言中,不能通过调用new函数或make函数创建初一个接口类型的值,也无法用字面量来表示一个接口类型的值.可以通过关键字type和interface声明接口类型,接口类型的类型字面量与结构体类型 ...
- [CodePlus2017]汀博尔
Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 158 Solved: 61[Submit][Status][Discuss] Description ...
- Codevs 1695 Windows2013
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 话说adamyi编的Windows 2013超时了(- -!),所以他不得不在自 ...
- struts2中的session使用
1.1. 如何获取Session 1.1.1. 获取Session的方式 Struts2中获取Session的方式有3种,大家掌握其中任何一种都可以. 通过ActionContext.getConte ...
- 设置select的默认选项
2014-01-07 09:54:34| 通过后台传出的值进行选择默认项的设置 <select name="user_id" id="user_id" ...
- Java日志框架-logback配置文件多环境日志配置(开发、测试、生产)(原始解决方法)
说明:这种方式应该算是最通用的,原理是通过判断标签实现. <!-- if-then form --> <if condition="some conditional exp ...
- IOS开发 序列化与反序列化
原帖地址:http://blog.csdn.net/ally_ideveloper/article/details/7956942 不会用,记下自己有时间看 序列化与反序列化概述 序列化,它又称串行化 ...
- hdoj 3351 Seinfeld 【栈的简单应用】
Seinfeld Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- 微信小程序之 ShoppingCart(购物车)
1.项目目录 2.逻辑层 group.js // pages/group/group.js Page({ /** * 页面的初始数据 */ data: { goodslist: [ { id: &qu ...