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 ...
随机推荐
- Android开发——GPS定位
1.LocationManager LocationManager系统服务是位置服务的核心组件,它提供了一系列方法来处理与位置相关的问题. 与LocationManager相关的两个知识点: 1.1 ...
- js中的constructor 和prototype
参考 http://www.cnblogs.com/yupeng/archive/2012/04/06/2435386.html function a(c){ this.b = c; this.d = ...
- Codevs 1497 取余运算== 洛谷P 1226
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 输入b,p,k的值,编程计算bp mod k的值.其中的b,p,k*k ...
- 【POJ1185】炮兵阵地(状压DP)
题意: 思路:状压DP经典题 可以预处理下每一行内合法的状态,发现很少 所以转移时可以使用状态的编号而不是状态本身 DP时记录前两行状态的编号进行转移和判断 #include<cstdio> ...
- linux 安装问题make: 没有指明目标并且找不到makefile。 停止
错误发生的可能原因,忘记安装软件需要的依赖.
- Flex 4 自定义预加载器
本示例的目的是在Flash Professional里创建自定义预加载器SWC,并扩展SparkDownloadProgressBar类在Flex 4应用程序中使用. 预加载器显示加载进度百分比 ...
- poj2553 有向图缩点,强连通分量。
//求这样的sink点:它能达到的点,那个点必能达到他,即(G)={v∈V|任意w∈V:(v→w)推出(w→v)} //我法:tarjan缩点后,遍历点,如果该点到达的点不在同一个强连通中,该点排除, ...
- ntfs格式uefi启动u盘
http://www.laomaotao.org/softhelp/syjc/925.html http://www.laomaotao.org/softhelp/wtjd/989.html http ...
- 主成分分析(principal components analysis)
http://www.cnblogs.com/jerrylead/tag/Machine%20Learning/ PCA的思想是将n维特征映射到k维上(k<n),这k维是全新的正交特征.这k维特 ...
- Android c/c++ 应用向linux 平台迁移执行
鉴于近期在minicamera (linux + FPGA) 平台上模拟 安卓系统camera流程遭遇不断升级同步的困扰.尤其是 不开放源代码后 , 应用要链接封闭动态库造成的困难.以及在sprdis ...