题解 P2330 【[SCOI2005]繁忙的都市】
又是一道Kruskal题目。
AC代码见下。
主要思路就是将所有的边储存起来,然后进行贪心地选择,期间需要判断两个端点是否有关联,这一过程通过并查集实现。Kruskal部分套模板就可以了。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std; const int maxn = ; int n, m;
int x[maxn], y[maxn], f[maxn];
int ans = ; struct node {
int x, y;
int val;
}dis[]; bool cmp(node a, node b) {
return a.val < b.val;
} int find(int x) {
int r = x;
while(r != f[r]) r = f[r];
int i = x, j;
while(f[i] != r) {
j = f[i];
f[i] = r;
i = j;
}
return r;
} void merge(int x, int y) {
x = find(x);
y = find(y);
if(x != y) f[y] = x;
} int main() {
cin >> n >> m;
for(int i = ; i <= m; i++) {
cin >> dis[i].x >> dis[i].y >> dis[i].val;
}
for(int i = ; i <= n; i++) f[i] = i; sort(dis + , dis + m + , cmp);
int tmp = , maxn = ;
for(int i = ; i <= m; i++) {
if(find(dis[i].x) != find(dis[i].y)) {
merge(dis[i].x, dis[i].y);
tmp++;
maxn = max(maxn, dis[i].val);
}
if(tmp == n - ) break;
} cout << tmp << ' ' << maxn;
}
题解 P2330 【[SCOI2005]繁忙的都市】的更多相关文章
- 洛谷—— P2330 [SCOI2005]繁忙的都市
P2330 [SCOI2005]繁忙的都市 题目描述 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道路是这样分布的:城市中有n个交叉路口,有些交叉路 ...
- 洛谷 P2330 [SCOI2005]繁忙的都市
题目链接 https://www.luogu.org/problemnew/show/P2330 题目描述 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市 ...
- 洛谷 P2330 [SCOI2005]繁忙的都市(最小生成树)
嗯... 题目链接:https://www.luogu.org/problemnew/show/P2330 这道题的问法也实在是太模板了吧: 1.改造的道路越少越好 2.能够把所有的交叉路口直接或间接 ...
- [LUOGU] P2330 [SCOI2005]繁忙的都市
题目描述 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两个交叉路口之间最多有一条 ...
- P2330 [SCOI2005] 繁忙的都市 洛谷
https://www.luogu.org/problem/show?pid=2330#sub 题目描述 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C ...
- 洛谷 P2330 [SCOI2005] 繁忙的都市 x
题目描述 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两个交叉路口之间最多有一条 ...
- 洛谷P2330 [SCOI2005]繁忙的都市——kruskal
给一手链接 https://www.luogu.com.cn/problem/P2330 这道题实质就是最小生成树 TIPS:最小生成树不仅是整体权值最小,也是最大边最小 #include<cs ...
- 洛谷P2330 [SCOI2005]繁忙的都市
#include<bits/stdc++.h> using namespace std; ; ; int n,k,Max,tot; struct node{ int cnt,fa; }f[ ...
- BZOJ 1083: [SCOI2005]繁忙的都市 kruskal
1083: [SCOI2005]繁忙的都市 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1083 Description 城市C是一个非 ...
- BZOJ 1083 [SCOI2005]繁忙的都市
1083: [SCOI2005]繁忙的都市 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1664 Solved: 1080[Submit][Sta ...
随机推荐
- proxy in java
[Static] IFeature.java ImpicateF.java Runport.java StaticProxy.java IFeature.java package UProxy.sta ...
- sql知识小记
1.在sql语句中,单引号嵌套时,使用单引号做转义
- crontab执行脚本和手动执行脚本输出结果不一致的问题处理
背景:huskiesir最近用公司给分配的账户写了脚本去检测某应用状态并发送到企业邮箱,写完脚本之后去执行了一下,发现效果还不错,在邮箱显示效果如下: 10.11.116.6 检查结果OK,检查时间 ...
- thinkphp 5.0整合phpsocketio完整攻略,绕坑
使用环境: thinkphp5.0 项目需求 前端下单,后台接受,并立即做出提示.例如:美团外卖,客户端下单成功后,商家端就会立即有接单语音提示. 开发环境 thinkphp5.0 phpsocket ...
- python基础8(装饰器)
1.装饰器本质 装饰器的本质:一个闭包函数 装饰器的功能:在不修改原函数及其调用方式的情况下对原函数功能进行扩展 2.装饰器函数 假设要写一个输出函数执行时间的装饰器 def timer(func): ...
- Linux用户与用户组
Linux用户与用户组 Linux系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统. 用户的账号一方面可以帮助系统管 ...
- @SpringBootApplication cannot be resolved to a type In STS
@SpringBootApplication cannot be resolved to a type In STS 学习了:https://stackoverflow.com/questions/4 ...
- jstack命令dump线程信息
jstack命令dump线程信息 D:\Java\jdk1.8.0_05\bin>jstack.exe 6540 > dump17 6540为java 线程pid: 出来的dump17文件 ...
- [MST] Create an Entry Form to Add Models to the State Tree
It is time to add new entries to the wishlist. We will achieve this by reusing forms and models we'v ...
- Springmvc JSON交互
先上前端javascript.ajax代码 <pre name="code" class="javascript"> function testAj ...