The Unique MST----poj1679次小生成树
题目链接:http://poj.org/problem?id=1679
判断最小生成数是否唯一:如果唯一这权值和次小生成树不同,否则相同;
#include<stdio.h>
#include<string.h>
#include<map>
#include<iostream>
#include<algorithm>
#include<math.h>
#define N 110
#define INF 0xfffffff using namespace std; int Max[N][N], used[N][N], vis[N], maps[N][N], dist[N], fa[N], n; void Init()
{
memset(vis, , sizeof(vis));
memset(used, , sizeof(used));
memset(Max, , sizeof(Max));
memset(fa, , sizeof(fa));
for(int i=; i<=n; i++)
{
dist[i] = INF;
for(int j=; j<=n; j++)
if(i == j)
maps[i][j] = ;
else
maps[i][j] = INF;
}
} int Prim(int start)
{
int ans=;
for(int i=; i<=n; i++)
{
dist[i] = maps[start][i];
fa[i] = start;
}
vis[start] = ;
for(int i=; i<=n; i++)
{
int Min = INF, index = -;
for(int j=; j<=n; j++)
{
if(vis[j]== && Min > dist[j])
{
Min = dist[j];
index = j;
}
}
if(index == -)break;
vis[index] = ;
ans += Min;
used[fa[index]][index] = used[index][fa[index]] = ;
for(int j=; j<=n; j++)
{
if(vis[j] == && index != j)
{
Max[index][j] = Max[j][index] = max(Max[fa[index]][j], dist[index]);
}
else if(vis[j] == && dist[j] > maps[index][j])
{
dist[j] = maps[index][j];
fa[j] = index;
}
}
}
return ans;
} int SMST(int ans)
{
int Min = INF;
for(int i=; i<=n;i++)
{
for(int j=i+; j<=n; j++)
{
if(used[i][j]== && maps[i][j] != INF)
Min=min(Min, ans-Max[i][j]+maps[i][j]);
}
}
return Min;
} int main()
{
int T, m, a, b, c;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &m);
Init();
for(int i=; i<m; i++)
{
scanf("%d%d%d", &a, &b, &c);
maps[a][b] = maps[b][a] = c;
}
int num1 = Prim();
int num2 = SMST(num1);
if(num1 != num2)
printf("%d\n", num1);
else
printf("Not Unique!\n");
}
return ;
}
The Unique MST----poj1679次小生成树的更多相关文章
- POJ-1679 The Unique MST(次小生成树、判断最小生成树是否唯一)
http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its minimum s ...
- POJ1679 The Unique MST 【次小生成树】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20421 Accepted: 7183 D ...
- POJ1679 The Unique MST【次小生成树】
题意: 判断最小生成树是否唯一. 思路: 首先求出最小生成树,记录现在这个最小生成树上所有的边,然后通过取消其中一条边,找到这两点上其他的边形成一棵新的生成树,求其权值,通过枚举所有可能,通过这些权值 ...
- The Unique MST(次小生成树)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22335 Accepted: 7922 Description Give ...
- POJ1679The Unique MST(次小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25203 Accepted: 8995 D ...
- poj 1679 The Unique MST【次小生成树】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24034 Accepted: 8535 D ...
- POJ 1679:The Unique MST(次小生成树&&Kruskal)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19941 Accepted: 6999 D ...
- poj 1679 The Unique MST 【次小生成树】【模板】
题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...
- POJ 1679 The Unique MST (次小生成树)题解
题意:构成MST是否唯一 思路: 问最小生成树是否唯一.我们可以先用Prim找到一棵最小生成树,然后保存好MST中任意两个点i到j的这条路径中的最大边的权值Max[i][j],如果我们能找到一条边满足 ...
- 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 ...
随机推荐
- hive操作
1.创建hive分区表: create table invites (id int, name string) partitioned by (ds string) row format delimi ...
- iOS开发--UIButton 设置圆角 边框颜色 点击回调方法
UIButton *signBtn = [UIButton buttonWithType:UIButtonTypeCustom]; signBtn.frame = CGRectMake(, , , ) ...
- javascript/css压缩工具---yuicompressor使用方法
1. 下载 地址:https://github.com/yui/yuicompressor/downloads 2. 安装 yuicompressor是由java写成的一组jar文件,需要jdk环境支 ...
- YYWebImage——iOS异步图片加载框架
本文转载至 http://www.mobile-open.com/2015/86582.html YYWebImage 是一个异步图片加载框架 (YYKit 组件之一). 其设计目的是试图替代 S ...
- django初体验 学习笔记
django环境搭建 1.安装Python 2.ipython sudo apt-get install ipython sudo pip instal ...
- 探求C#.Net中ArrayList与Array的区别
ArrayList与Array的区别概述 ArrayList 是数组的复杂版本.ArrayList 类提供在大多数 Collections 类中提供但不在 Array 类中提供的一些功能.例 ...
- JS缺失错误- Uncaught SyntaxError: Unexpected token <
这种情况,表明,缺少js文件 解决方式:在文件夹下将缺少js文件补足
- MegaCli 使用
安装: wget ftp://rpmfind.net/linux/Mandriva/devel/cooker/x86_64/media/non-free/release/megacli-8.02.21 ...
- django进阶-查询(适合GET4以上人群阅读)
前言: 下篇博客写关于bootstrap... 一.如何在脚本测试django from django.db import models class Blog(models.Model): name ...
- 【抓包分析】 charles + 网易mumu 模拟器数据包
charles 的使用.我就不再多说了.可以参考以往文章,传送门: https://www.cnblogs.com/richerdyoung/p/8616674.html 此处主要说网易模拟器的使用 ...