Codeforces Round #529 (Div. 3) F. Make It Connected(最小生成树)
AC
- 最小生成树,建边的时候不需要N2{N^2}N2,首先N个点需要N-1条边,N个点的权值都会被计算,一些顶点会重复计算。我们让重复计算的点是权值最小的点,就可以保证生成树权值最小
- 将特殊边加到边集里,跑一边Kruskal
#include <bits/stdc++.h>
#define P pair<int, int>
#define lowbit(x) (x & -x)
#define mem(a, b) memset(a, b, sizeof(a))
#define REP(i, n) for (int i = 1; i <= (n); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define N 200006
#define LL long long
using namespace std;
struct ac{LL u, v, d;
}g[N*2];
LL a[N];
int fa[N];
int find(int x) {return x == fa[x] ? x : fa[x] = find(fa[x]);
}
int main() {#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);
#endifint n, m;while (scanf("%d %d", &n, &m) != EOF) {int pos = 1;REP (i, n) {scanf("%lld", &a[i]);if (a[i] < a[pos]) pos = i;}int len = 0;REP (i, n) {if (i == pos) continue;g[len].u = pos;g[len].v = i;g[len].d = a[pos] + a[i];len++;}rep (i, m) {scanf("%lld %lld %lld", &g[len].u, &g[len].v, &g[len].d);++len;}sort(g, g + len, [&](const ac &x, const ac &y){return x.d < y.d;});REP (i, n) fa[i] = i;LL u, v, d, ans = 0, cnt = 0;rep (i, len) {u = g[i].u;v = g[i].v;d = g[i].d;if (find(u) != find(v)) {fa[find(u)] = find(v);ans += d;if (++cnt == n-1) break;}}printf("%lld\n", ans);}return 0;
}
Codeforces Round #529 (Div. 3) F. Make It Connected(最小生成树)相关推荐
- Codeforces Round #529 (Div. 3) F(kul最小生成树)
https://codeforces.com/problemset/problem/1095/F 题意 给你n个互不相通的点,每个点有权值ai,题目要求将n个点连接生成最小生成树,连接两点的代价是这两 ...
- [CF]Codeforces Round #529 (Div. 3)
[CF]Codeforces Round #529 (Div. 3) C. Powers Of Two Description A positive integer xx is called a po ...
- Codeforces Round #699 (Div. 2) F - AB Tree(贪心、树上DP)超级清晰,良心题解,看不懂来打我 ~
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #699 (Div. 2) F - AB Tree Problem ...
- Codeforces Round #644 (Div. 3) F.Spy-string
Codeforces Round #644 (Div. 3) F.Spy-string 题目链接 You are given n strings a1,a2,-,an: all of them hav ...
- Codeforces Round #849 (Div. 4) F. Range Update Point Query
Codeforces Round #849 (Div. 4) F. Range Update Point Query 题目大意: 给一串数字,有两个操作: 操作1:将 l − r l-r l−r 的数 ...
- Codeforces Round #538 (Div. 2) F. Please, another Queries on Array? 线段树 + 欧拉函数
传送门 文章目录 题意: 思路: 题意: 给你一个序列aaa,你需要实现两种操作: (1)(1)(1) 将[l,r][l,r][l,r]的aia_iai都乘rrr. (2)(2)(2) 求ϕ(∏i= ...
- Codeforces Round #742 (Div. 2) F. One-Four Overload 构造 + 二分图染色
传送门 文章目录 题意: 思路: 题意: 给你一个n∗mn*mn∗m的矩形,包含...和XXX,你有两种颜色,你需要给...染色使得每个XXX上下左右相邻的...其两种颜色个数相同,输出一种合法方案. ...
- Codeforces Round #740 (Div. 2) F. Top-Notch Insertions 线段树 / 平衡树 + 组合数学
传送门 文章目录 题意: 思路: 题意: 思路: 考虑最终的序列是什么鸭子的,首先序列肯定单调不降,也就是a1≤a2≤a3≤...≤ana_1\le a_2\le a_3\le ...\le a_na ...
- Codeforces Round #585 (Div. 2) F. Radio Stations 2-sat + 神仙建模
传送门 文章目录 题意: 思路: 题意: 你现在有ppp种电台,有nnn对关系(x,y)(x,y)(x,y)代表xxx电台或yyy电台中至少有一个,mmm对关系(x,y)(x,y)(x,y)代表xxx ...
最新文章
- Redis第三集:redis-benchmark性能测试
- 移动端https抓包那些事--进阶篇
- 【ruoyi若依】引入activiti 模块
- QT QML 在qml中自定义信号
- 水晶报表设置图片高度与宽度
- mysql 自动备份发送,Centos定时自动备份MySQL数据库并发送至指定邮箱
- 组态软件运行在云服务器_能在云服务器上运行软件吗
- crontab java job_crontab 定时任务
- STM32之输入捕获
- java连接mysql数据库 R,java连接MySql数据库!
- mysql命令 show_mysql show 相关命令
- Java学生管理系统-增删改查
- 分布式协议与算法(一)Paxos 算法
- 【算法随记二】线卷积积分及其在图像增强和特效方面的应用(一)
- Python图像处理(Pillow/PIL)入门
- 企业怎样优化用户体验?F5给出三条专业建议
- 有哪些常用的搜索引擎指令?
- java网络编程 TCP程序
- 常用的web服务器软件整理(转载)
- Python 保留小数位