Bzoj 1083: [SCOI2005]繁忙的都市 (最小生成树)
Bzoj 1083: [SCOI2005]繁忙的都市
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1083
此题是最小瓶颈生成树的裸题.
最小瓶颈生成树:由最小的边权的\(n-1\)条边连接起\(n\)个点.
显然这就是\(Kruskal\)算法.继而得知其实就是求最小生成树,用\(prim\)也可以求.
然后这道题就成了最小生成树入门题??
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#define max(a,b) a > b ? a : b
const int maxN = 300 + 7;
const int maxM = 5e4 + 7;
using namespace std;
struct Node {
int u,v,w;
}Map[maxM];
int num ;
int f[maxN];
bool cmp(Node a,Node b) {
return a.w < b.w;
}
void add_Node(int u,int v,int w) {
Map[++ num].u = u;
Map[num].v = v;
Map[num].w = w;
return;
}
int find(int x) {
return f[x] == x ? x : f[x] = find(f[x]);
}
void unit(int u,int v) {
int fx = find(u),fy = find(v);
if(rand() % 2) f[fx] = fy;
else f[fy] = fx;
return;
}
inline int read() {
int x = 0,f = 1;char c = getchar();
while(c < '0' || c > '9') {if(c == '-')f = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = getchar();}
return x * f;
}
int main() {
int n,m;
n = read();m = read();
for(int i = 1;i <= n;++ i)
f[i] = i;
for(int i = 1,u,v,w;i <= m;++ i) {
u = read();v = read();w = read();
add_Node(u,v,w);
}
sort(Map + 1,Map + m + 1,cmp);
int sum = 0,k = 0;
for(int i = 1;i <= m;++ i) {
if(k == n - 1) break;
int fx = find(Map[i].u),fy = find(Map[i].v);
if(fx != fy) {
k ++;
sum = max(Map[i].w,sum);
unit(Map[i].u,Map[i].v);
}
}
printf("%d %d",n - 1,sum);
return 0;
}
Bzoj 1083: [SCOI2005]繁忙的都市 (最小生成树)的更多相关文章
- bzoj 1083: [SCOI2005]繁忙的都市 (最小生成树)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1083 思路:连接所有点,肯定最少是需要n-1条边的,也就是写个最小生成树,记得保存下最大的权 ...
- BZOJ 1083: [SCOI2005]繁忙的都市【Kruscal最小生成树裸题】
1083: [SCOI2005]繁忙的都市 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2925 Solved: 1927[Submit][Sta ...
- BZOJ 1083: [SCOI2005]繁忙的都市 kruskal
1083: [SCOI2005]繁忙的都市 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1083 Description 城市C是一个非 ...
- BZOJ 1083: [SCOI2005]繁忙的都市(MST)
裸的最小生成树..直接跑就行了 ---------------------------------------------------------------------- #include<c ...
- BZOJ 1083 [SCOI2005]繁忙的都市
1083: [SCOI2005]繁忙的都市 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1664 Solved: 1080[Submit][Sta ...
- BZOJ 1083 [SCOI2005]繁忙的都市 (最小生成树裸题无重边) 超简单写法!!
Description 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两个交叉路口 ...
- BZOJ 1083: [SCOI2005]繁忙的都市 裸的最小生成树
题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=1083 代码: #include<iostream> #include< ...
- BZOJ(5) 1083: [SCOI2005]繁忙的都市
1083: [SCOI2005]繁忙的都市 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4105 Solved: 2595[Submit][Sta ...
- 1083: [SCOI2005]繁忙的都市
1083: [SCOI2005]繁忙的都市 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1319 Solved: 878[Submit][Stat ...
随机推荐
- ie img 3px bug
ie img 3px bug 日期:2008-11-22 分类:CSS ie img 有 3px 的缝隙也是 ie 的经典 bug 之一,相信已经不陌生了,但还是先看看效果吧(也许你并没有见过): 效 ...
- android 通知(android 8.0可用)
final String CHANNEL_ID = "com.chao.channel.id"; final String CHANNEL_NAME = "com.c ...
- Django (六) 视图 views
views 1. 视图及HttpRequest 和HttpResponse Django中的视图主要用来接受Web请求,并做出响应. 视图的本质就是一个Python中的函数 视图的响应分为两大类 1) ...
- Codeforces Round #532(Div. 2) C.NN and the Optical IIIusion
链接:https://codeforces.com/contest/1100/problem/C 题意: 一个圆球外面由其他圆球包裹,两两相连. 给出n,r. n为外面圆球数量,r为内部圆球半径. 求 ...
- python_14(js)
第1章 图片方法 1.1 设置背景图:1.2 背景图问题:1.3 background-repeat; noa-repe 1.4 background-attachment: fixed1.5 bac ...
- 现在的Unix时间戳(Unix timestamp)
如何在不同编程语言中获取现在的Unix时间戳(Unix timestamp)? Java time JavaScript Math.round(new Date().getTime()/1000)ge ...
- Semi-prime H-numbers
题目描述形如4n+1的数被称为“H数”,乘法在“H数”集合内为封闭的.因数只有1和本身的数叫“H素数”(不包括1),其余叫“H合数”.一个“H合成数”能且只能分解为两个“H素数”.求0·h内的“H合成 ...
- list control 【转】
1. CListCtrl 风格 LVS_ICON: 为每个item显示大图标 LVS_SMALLICON: 为每个item显示小图标 LVS_LIST: 显示一列带有小图标的ite ...
- Easyui combobox如何默认选中第一项???
以下代码可以实现combobox默认选中第一项,在实际开发中我们可能会用到! // 处理combobox默认选中的问题 <input id="user_type" class ...
- LINUX提高openfire并发数(网上收集)
谷歌博客地址:http://tsaiquinn.blogspot.com/2014/10/linuxopenfire.html 影响连接数的元素包含三种:1)Linux的系统参数2)进程自身可以创建的 ...