POJ-2485 Highways---最小生成树中最大边
题目链接:
https://vjudge.net/problem/POJ-2485
题目大意:
求最小生成树中的最大边
思路:
是稠密图,用prim更好,但是规模不大,kruskal也可以过
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<sstream>
using namespace std;
typedef long long ll;
const int maxn = 3e5 + ;
const int INF = << ;
int dir[][] = {,,,,-,,,-};
int T, n, m, x;
struct edge
{
int u, v, w;
bool operator <(const edge& a)const
{
return w < a.w;
}
};
edge a[maxn];
int par[], high[];
//初始化n个元素
void init(int n)
{
for(int i = ; i < n; i++)
{
par[i] = i;
high[i] = ;
}
}
//查询树的根
int Find(int x)
{
return par[x] == x ? x : par[x] = Find(par[x]);//路径压缩
}
void unite(int x, int y)
{
x = Find(x);
y = Find(y);
if(x == y)return;
if(high[x] < high[y])par[x] = y;//y的高度高,将x的父节点设置成y
else
{
par[y] = x;
if(high[x] == high[y])high[x]++;
}
}
bool same(int x, int y)
{
return Find(x) == Find(y);
}
void kruskal(int n, int m)//点数n,边数m
{
int ans = ;//mst权值
int num= ;//已经选择的边的边数
sort(a, a + m);//边进行排序
init(n);//初始化并查集
for(int i = ; i < m; i++)
{
int u = a[i].u;
int v = a[i].v;
if(Find(u - ) != Find(v - ))//图最开始的下标是1,并查集是0
{
//printf("%d %d %d\n", u, v, a[i].w);
//sum_mst += a[i].w;
ans = max(ans, a[i].w);
num++;
unite(u - , v - );
}
if(num >= n - )break;
}
//printf("weight of mst is %d\n", sum_mst);
printf("%d\n", ans);
}
int main()
{
cin >> T;
while(T--)
{
scanf("%d", &n);
int x;
int tot = ;
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
{
scanf("%d", &x);
if(i == j)continue;
a[tot].u = i;
a[tot].v = j;
a[tot++].w = x;
}
}
kruskal(n, tot);
}
return ;
}
POJ-2485 Highways---最小生成树中最大边的更多相关文章
- poj 2485 Highways 最小生成树
点击打开链接 Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19004 Accepted: 8815 ...
- POJ 2485 Highways(最小生成树+ 输出该最小生成树里的最长的边权)
...
- POJ 2485 Highways 最小生成树 (Kruskal)
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...
- poj 2485 求最小生成树中 最长的一条边
Sample Input 1 //T 3 //n0 990 692 //邻接矩阵990 0 179692 179 0Sample Output 692 prim # include <iostr ...
- POJ 2485 Highways (求最小生成树中最大的边)
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...
- POJ 2485 Highways【最小生成树最大权——简单模板】
链接: http://poj.org/problem?id=2485 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- poj 2485 Highways (最小生成树)
链接:poj 2485 题意:输入n个城镇相互之间的距离,输出将n个城镇连通费用最小的方案中修的最长的路的长度 这个也是最小生成树的题,仅仅只是要求的不是最小价值,而是最小生成树中的最大权值.仅仅须要 ...
- POJ 2485 Highways( 最小生成树)
题目链接 Description The islandnation of Flatopia is perfectly flat. Unfortunately, Flatopia has no publ ...
- POJ 2485 Highways (prim最小生成树)
对于终于生成的最小生成树中最长边所连接的两点来说 不存在更短的边使得该两点以不论什么方式联通 对于本题来说 最小生成树中的最长边的边长就是使整个图联通的最长边的边长 由此可知仅仅要对给出城市所抽象出的 ...
- poj 2485 Highways
题目连接 http://poj.org/problem?id=2485 Highways Description The island nation of Flatopia is perfectly ...
随机推荐
- js对象系列【二】深入理解js函数,详解作用域与作用域链。
这次说一下对象具体的一个实例:函数,以及其对应的作用域与作用域链.简单的东西大家查下API就行了,这里我更多的是分享自己的理解与技巧.对于作用域和作用域链,相信绝大多数朋友看了我的分享都能基本理解,少 ...
- mysql limit 接收变量
参考文章:https://blog.csdn.net/ljz2009y/article/details/7887743 PREPARE s1 FROM 'SELECT * FROM t LIMIT ? ...
- 【openvpn】转载:烂泥:ubuntu 14.04搭建OpenVPN服务器
地址:http://www.cnblogs.com/ilanni/p/4681740.html (1)安装openVpn软件后.在openVpn的配置目录下添加配置文件: ca.crt client ...
- KVM之十一:调整cpu和内存
1.virsh edit snale (更改前要将snale shutdown) 找到"memory"和"vcpu"标签,将 <memory unit=' ...
- “Swift Language Version” (SWIFT_VERSION) build setting must be set to a supported value for targets which use Swift
使用cocopod导入第三方swift包后,编译报以下错误: The "Swift Language Version" (SWIFT_VERSION) build setting ...
- 让Myeclipse自动生成的get set方法 自动加上文本注释,并且注释内容包含字段中我们加的文档注释
在进行编码写实体类的时候发现,一个实体类有好多的字段要进行注释,他们都是私有的不能直接访问,我们在写的时候加入的文档注释也起不到效果,但是自动生成的get,set方法的文档注释有不符合我们要求(没有包 ...
- c# 实时监控数据库 SqlDependency
http://blog.csdn.net/idays021/article/details/49661855 class Program { private static string _connSt ...
- shell命令总结一
简述:这篇总结是在360企业安全实习第一周学到的. Linux中的 2>&1 .if文件命令 .tr .$0等相关参数含义的用法 1. 2>&1 command>/d ...
- php 常用数据大全
一.数组操作的基本函数 数组的键名和值 array_values($arr);获得数组的值 array_keys($arr);获得数组的键名 array_flip($arr);数组中的值与键名互换(如 ...
- C语言--第一周作业(更改)
*********************学习总结********************* 1.所用词典: 2.Git截图: *********************遇到的问题和解决方法***** ...