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(最小生成树)相关推荐

  1. Codeforces Round #529 (Div. 3) F(kul最小生成树)

    https://codeforces.com/problemset/problem/1095/F 题意 给你n个互不相通的点,每个点有权值ai,题目要求将n个点连接生成最小生成树,连接两点的代价是这两 ...

  2. [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 ...

  3. Codeforces Round #699 (Div. 2) F - AB Tree(贪心、树上DP)超级清晰,良心题解,看不懂来打我 ~

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #699 (Div. 2) F - AB Tree Problem ...

  4. 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 ...

  5. 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 的数 ...

  6. 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= ...

  7. Codeforces Round #742 (Div. 2) F. One-Four Overload 构造 + 二分图染色

    传送门 文章目录 题意: 思路: 题意: 给你一个n∗mn*mn∗m的矩形,包含...和XXX,你有两种颜色,你需要给...染色使得每个XXX上下左右相邻的...其两种颜色个数相同,输出一种合法方案. ...

  8. Codeforces Round #740 (Div. 2) F. Top-Notch Insertions 线段树 / 平衡树 + 组合数学

    传送门 文章目录 题意: 思路: 题意: 思路: 考虑最终的序列是什么鸭子的,首先序列肯定单调不降,也就是a1≤a2≤a3≤...≤ana_1\le a_2\le a_3\le ...\le a_na ...

  9. 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 ...

最新文章

  1. Redis第三集:redis-benchmark性能测试
  2. 移动端https抓包那些事--进阶篇
  3. 【ruoyi若依】引入activiti 模块
  4. QT QML 在qml中自定义信号
  5. 水晶报表设置图片高度与宽度
  6. mysql 自动备份发送,Centos定时自动备份MySQL数据库并发送至指定邮箱
  7. 组态软件运行在云服务器_能在云服务器上运行软件吗
  8. crontab java job_crontab 定时任务
  9. STM32之输入捕获
  10. java连接mysql数据库 R,java连接MySql数据库!
  11. mysql命令 show_mysql show 相关命令
  12. Java学生管理系统-增删改查
  13. 分布式协议与算法(一)Paxos 算法
  14. 【算法随记二】线卷积积分及其在图像增强和特效方面的应用(一)
  15. Python图像处理(Pillow/PIL)入门
  16. 企业怎样优化用户体验?F5给出三条专业建议
  17. 有哪些常用的搜索引擎指令?
  18. java网络编程 TCP程序
  19. 常用的web服务器软件整理(转载)
  20. Python 保留小数位

热门文章

  1. 通过Fiddler进行手机抓包
  2. Android的基本常用的短信操作
  3. php 自带过滤和转义函数
  4. [f]动态判断js加载完成
  5. ProE二次开发之VS2005+ProE Wildfire 4.0开发环境配置
  6. 关于框架的胡言乱语(上)
  7. 1026:空格分隔输出
  8. 【Java2】一维数组,家庭收支界面,/属性和方法,方法重载,变量分类,可变参数,包
  9. 【Linux】一步一步学Linux——gcov命令(257)
  10. 【Linux】一步一步学Linux——sum命令(234)