次小生成树 判断 unique MST
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!
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
#include<vector>
#include<iomanip>
#include<iostream>
using namespace std;
#define MAXN 101
#define INF 0x3f3f3f3f
/*
判断最小生成树是否唯一。
求次小生成树,若两个权值相等说明not unique
次小生成树算法,在prim()算法求解的时候,求出MST中u到v最大边权值
,然后用不在MST中的边依次枚举取最小值
*/
int g[MAXN][MAXN],Max[MAXN][MAXN],lowcost[MAXN],pre[MAXN],n,m,t;
bool used[MAXN][MAXN],been[MAXN];
int Prim()
{
int ret = ;
memset(been,false,sizeof(been));
memset(Max,,sizeof(Max));
memset(used,false,sizeof(used));
been[] = true;
pre[] = -;
for(int i=;i<=n;i++)
{
pre[i] = ;
lowcost[i] = g[][i];
}
lowcost[] = ;
for(int i=;i<n;i++)
{
int minc = INF,k =- ;
for(int j=;j<=n;j++)
{
if(!been[j]&&lowcost[j]<minc)
{
minc = lowcost[j];
k = j;
}
}
if(k==-) return -;
been[k] = true;
ret+=minc;
used[k][pre[k]] = used[pre[k]][k] = true;
for(int j=;j<=n;j++)
{
if(been[j])
Max[j][k] = Max[k][j] = max(Max[j][pre[k]],lowcost[k]);
if(!been[j]&&lowcost[j]>g[k][j])
{
lowcost[j] = g[k][j];
pre[j] = k;
}
}
}
return ret;
}
int cixiao(int ans)
{
int tmp = INF;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
if(!used[i][j]&&g[i][j]!=INF)
tmp = min(tmp,ans-Max[i][j]+g[i][j]);
}
if(tmp==INF)
return -;
return tmp;
}
int main()
{
cin>>t;
while(t--)
{
cin>>n>>m;
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
g[i][j] = INF;
}
int x,y,d;
for(int t=;t<m;t++)
{
cin>>x>>y>>d;
g[x][y] = g[y][x] = d;
}
int ans = Prim();
int tmp = cixiao(ans);
if(tmp==ans||ans==-)
cout<<"Not Unique!\n";
else
cout<<ans<<endl;
}
return ;
}
次小生成树 判断 unique MST的更多相关文章
- 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(次小生成树、判断最小生成树是否唯一)
http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its minimum s ...
- The Unique MST(次小生成树)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22335 Accepted: 7922 Description Give ...
- poj 1679 The Unique MST【次小生成树】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24034 Accepted: 8535 D ...
- poj 1679 判断MST是不是唯一的 (次小生成树)
判断MST是不是唯一的 如果是唯一的 就输出最小的权值和 如果不是唯一的 就输出Not Unique! 次小生成树就是第二小生成树 如果次小生成树的权值和MST相等 那么MST就不是唯一的 法一: ...
- POJ 1679 The Unique MST (次小生成树)题解
题意:构成MST是否唯一 思路: 问最小生成树是否唯一.我们可以先用Prim找到一棵最小生成树,然后保存好MST中任意两个点i到j的这条路径中的最大边的权值Max[i][j],如果我们能找到一条边满足 ...
- 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: 22668 Accepted: 8038 D ...
- 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 ...
随机推荐
- mysql的子查询的提高
统计胜负结果的sql语句 date result 2011-02-01 胜 2011-02-01 负 2011-02-0 ...
- Authorization 头信息为空的解决方案
在 Apache配置添加SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=此次问题完美解决.
- php可以定义数组的常量吗
是这样吗?<?php define('BEST_PHPER',array('name'=>'巩文','address'=>'china')); My God,明确告诉你不可以:原因是 ...
- SQL 经典语句大全
原地址:http://www.cnblogs.com/yubinfeng/archive/2010/11/02/1867386.html 一.基础 1.说明:创建数据库 CREATE DATABASE ...
- 数据结构之单链表(C实现)
list.h #ifndef LIST_H #define LIST_H #include <iostream> #include <stdio.h> #include < ...
- hbase本地调试环境搭建
1,前言 想要深入的了解hbase,看hbase源码是必须的.以下描述了搭建hbase本地调试环境的经历 2,安装步骤 2.1,启动hbase 1,安装java和IDE IntelliJ,下载源码等. ...
- request获取请求参数
/** * 方式1 */ Enumeration<?> enu=request.getParameterNames(); while(enu.hasMoreElements()){ Str ...
- [ 东莞市选 2008 ] GCD&LCM
\(\\\) \(Description\) 给出两数的\(GCD\)和\(LCM\),求合法的两数之差的绝对值最小是多少. \(GCD\times LCM\le10^{18}\) \(\\\) \( ...
- Mantis 配置与使用学习
转载自:http://blog.csdn.net/xifeijian/article/category/1429687
- Slow HTTP Denial of Service Attack 漏洞解决办法
编辑 删除 问题名称: Slow HTTP Denial of Service Attack 问题URL http://10.238.*.*:58*** 风险等级: 高 问题类型: 服务器配置类 漏洞 ...