次小生成树 判断 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 ...
 
随机推荐
- win10家庭版添加本地账户方法
			
1.正常情况下,windows的使用者都习惯于用本地用户及用户组工具来创建新用户,如下图所示,在win10的开始窗口右侧的空白处,输入lusrmgr.msc 打开本地用户管理控制台. 2.在win10 ...
 - 51nod1344 走格子
			
1344 走格子 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 有编号1-n的n个格子,机器人从1号格子顺序向后走,一直走到n号格子,并需要从n号格 ...
 - scala学习笔记3:基本控制结构基础
			
以下主要记录的是看完scala in programming这本书buildin control structures(第七章)后的要点总结. 1,if,while,do while和for的用法和j ...
 - C#基础  集合
			
//数组定义的时候 //需要定义数据类型 //需要定义初始长度 //int [] array = new int[5]; //int a = array.Length; //集合 //ArrayLis ...
 - log4j建立propertie后要建立log4j2.xml
			
log4j.properties ### \u8BBE\u7F6E### log4j.rootLogger = debug,stdout,D,E ### \u8F93\u51FA\u4FE1\u606 ...
 - php函数的声明与使用
			
function 函数名(){ 函数体 } 一个函数是由3部分组成:声明(function 关键字).函数名(用来找到函数体的).函数体(封装的代码) 2.函数的优越性 代码重用性强.维护方便.提高开 ...
 - scla-基础-函数-元组(0)
			
//元组 class Demo2 extends TestCase { def test_create_^^(){ val yuana = (1,true,1.2,"c",&quo ...
 - C++中图片重命名
			
非常简单的小程序,满足自己的需求. #include <iostream> #include <fstream> #include<sstream> using n ...
 - 2017-11-28 Html-浅谈如何正确给table加边框
			
一般来说,给表格加边框都会出现不同的问题,以下是给表格加边框后展现比较好的方式 <style> table,table tr th, table tr td { border:1px so ...
 - Tornado引入静态css、js文件
			
一.静态路径 template_path=os.path.join(os.path.dirname(__file__), "templates") 这里是设置了模板的路径,放置模板 ...