题目大意:给你$n(n\leqslant2\times10^5)$个点和$m(m\leqslant2\times10^5)$条边,第$i$个点点权为$a_i$。连接$u,v$两个点的代价为$a_u+a_v$或者一条连接$u,v$的边的边权。问连通的最小代价

题解:发现若不考虑特殊边,一定是点权最小的点连向其他点。于是建出由点权最小的点连向其他各点的边,边权为两点点权和。与特殊边一起跑最小生成树即可。

卡点:

C++ Code:

#include <algorithm>
#include <cstdio>
#define maxn 200010
int n, m;
int l[maxn << 1], r[maxn << 1], rnk[maxn << 1];
long long ans, a[maxn], w[maxn << 1]; int f[maxn];
int find(int x) { return x == f[x] ? x : (f[x] = find(f[x])); } int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%lld", a + i);
rnk[i] = f[i] = i;
}
std::sort(rnk + 1, rnk + n + 1, [] (int x, int y) { return a[x] < a[y]; });
const long long base = a[rnk[1]];
const int L = rnk[1];
for (int i = 1; i < n; ++i) {
w[i] = a[rnk[i + 1]] + base;
l[i] = L, r[i] = rnk[i + 1];
rnk[i] = i;
}
for (int i = n; i < n + m; ++i) {
scanf("%d%d%lld", l + i, r + i, w + i);
rnk[i] = i;
}
std::sort(rnk + 1, rnk + n + m, [] (int x, int y) { return w[x] < w[y]; });
int num = n - 1;
for (int i = 1, u, v; i < n + m && num; ++i) {
u = find(l[rnk[i]]), v = find(r[rnk[i]]);
if (u != v) {
f[u] = v;
ans += w[rnk[i]];
--num;
}
}
printf("%lld\n", ans);
return 0;
}

  

[CF1095F]Make It Connected的更多相关文章

  1. 【生成树,堆】【CF1095F】 Make It Connected

    Description 给定 \(n\) 个点,每个点有点权,连结两个点花费的代价为两点的点权和.另外有 \(m\) 条特殊边,参数为 \(x,y,z\).意为如果你选择这条边,就可以花费 \(z\) ...

  2. 【CF1095F】 Make It Connected(最小生成树)

    题目链接 如果没有特殊边的话显然答案就是权值最小的点向其他所有点连边. 所以把特殊边和权值最小的点向其他点连的边丢一起跑最小生成树就行了. #include <cstdio> #inclu ...

  3. 题解 CF1095F 【Make It Connected】

    题意简述 \(n\)( \(1≤n≤2×10^5\) )个点,每个点 \(i\) 有一个点权 \(a_i\) ( \(1≤a_i≤2×10^{12}\) ),将两个点 \(i\),\(j\) 直接相连 ...

  4. F. Make It Connected 解析(思維、MST)

    Codeforce 1095 F. Make It Connected 解析(思維.MST) 今天我們來看看CF1095F 題目連結 題目 給你\(n\)個點,每個點\(u\)還有一個值\(a[u]\ ...

  5. [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  6. PTA Strongly Connected Components

    Write a program to find the strongly connected components in a digraph. Format of functions: void St ...

  7. poj 1737 Connected Graph

    // poj 1737 Connected Graph // // 题目大意: // // 带标号的连通分量计数 // // 解题思路: // // 设f(n)为连通图的数量,g(n)为非连通图的数量 ...

  8. LeetCode Number of Connected Components in an Undirected Graph

    原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...

  9. Windows Phone 8 解锁提示IpOverUsbSvc问题——IpOverUsbEnum返回No connected partners found解决方案

    我的1520之前总是无法解锁,提示:IpOverUsbSvc服务没有开启什么的. 根据网上网友的各种解决方案: 1. 把手机时间设置为当前时间,并且关闭“自动设置” 2. 确保手机接入了互联网 3.确 ...

随机推荐

  1. 【LG2481】[SDOI2011]拦截导弹

    [LG2481][SDOI2011]拦截导弹 题面 洛谷 题解 可以看出第一问就是一个有关偏序的\(LIS\),很显然可以用\(CDQ\)优化 关键在于第二问 概率\(P_i=\) \(总LIS数\) ...

  2. 1030: [JSOI2007]文本生成器

    1030: [JSOI2007]文本生成器 https://www.lydsy.com/JudgeOnline/problem.php?id=1030 分析: AC自动机+dp. 正难则反,求满足的, ...

  3. hive中的优化问题

    一.fetch抓取 fetch 抓取是指,hive中对某些情况的查询可以不必使用MapReduce计算.(1)把hive.fetch.task.conversion 设置成none,然后执行查询语句, ...

  4. CC3200-LAUNCHXL驱动不能正常识别的问题

    1. 本次使用利尔达的CC3200底板,完全兼容官方CC3200-LAUNCHXL,如果上电之后驱动识别为2路串口,是有问题的.原因是FT2232外接的EEPROM没有烧写固件. 2. 安装FT_Pr ...

  5. android学习十三 首选项

    1,首选项可用用来持久保存用户设置,游戏最高分等 2,首选项有,列表首选项,复选框首选项,对话框首选项.. 3,通过xml文件和代码创建首选项      addPreferencesFromResou ...

  6. VIN码识别:让VIN码采集so easy!

    近几年汽车后市场呈喷井式发展,在过去的半年,汽车后市场规模已高达万亿级,产业前景广阔,与此同时行业运营也受信息区域化.数据不统一的制约,让企业面临着效率低下.规模化运行困难的痛点. 在汽车配件市场中, ...

  7. liunx环境下安装禅道

    环境: vm12.5.2 CentOS-7-x86_64 ZenTaoPMS.9.1.stable.zbox_64 SecureCRT 8.0 因为liunx环境下配置apache, php, mys ...

  8. JMeter自学笔记3-创建自己的第一个测试用例

    一.写在前面的话: 上篇我们已经认识了JMeter的图形界面,大家应该都是很懵的.那么这篇,我们将学习使用JMeter创建第一个属于自己测试用例. 二.创建自己的第一个测试用例: 1.新建一个Thre ...

  9. git服务器搭建及eclipse使用git

    一.搭建git服务器 1.yum install git 2.新建用户linux用户git,管理git服务 useradd git passwd git 3.初始化git仓库 git init --b ...

  10. 使用jenkins构建一个maven项目

    1.登陆到jenkins首页,创建项目-->选择maven-->输入项目名称-->选择项目类型 2.进入项目配置:{先写一下项目描述和设置下保留的历史构建,然后向下拉} 找到源吗管理 ...