题目链接

如果没有特殊边的话显然答案就是权值最小的点向其他所有点连边。

所以把特殊边和权值最小的点向其他点连的边丢一起跑最小生成树就行了。

#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 200010;
typedef long long ll;
inline ll read(){
ll s = 0, w = 1;
char ch = getchar();
while(ch < '0' || ch > '9'){ if(ch == '-') w = -1; ch = getchar(); }
while(ch >= '0' && ch <= '9'){ s = s * 10 + ch - '0'; ch = getchar(); }
return s * w;
}
struct Edge{
int from, to;
ll dis;
int operator < (const Edge A) const{
return dis < A.dis;
}
}e[MAXN << 1];
int n, m, pos;
ll ans, w[MAXN], mn = 2147483647214748364ll;
int f[MAXN];
inline int find(int x){
return f[x] == x ? x : f[x] = find(f[x]);
}
int main(){
n = read(); m = read();
for(int i = 1; i <= n; ++i){
w[f[i] = i] = read();
if(w[i] < mn) mn = w[i], pos = i;
}
for(int i = 1; i <= m; ++i){
e[i].from = read();
e[i].to = read();
e[i].dis = read();
}
for(int i = 1; i <= n; ++i)
e[i + m] = (i == pos) ? (Edge){ 0, 0, 2147483647214748364ll } : (Edge){ i, pos, w[i] + w[pos] };
sort(e + 1, e + n + m + 1);
for(int i = 1, k = n - 1; k; ++i)
if(find(e[i].from) != find(e[i].to))
f[find(e[i].from)] = find(e[i].to), --k, ans += e[i].dis;
printf("%lld\n", ans);
return 0;
}

【CF1095F】 Make It Connected(最小生成树)的更多相关文章

  1. [CF1095F]Make It Connected

    题目大意:给你$n(n\leqslant2\times10^5)$个点和$m(m\leqslant2\times10^5)$条边,第$i$个点点权为$a_i$.连接$u,v$两个点的代价为$a_u+a ...

  2. Codeforces 1095F Make It Connected(最小生成树)

    题目链接:Make It Connected 题意:给定一张$n$个顶点(每个顶点有权值$a_i$)的无向图,和已连接的拥有边权$w_i$的$m$条边,顶点u和顶点v直接如果新建边,边权为$a_u+a ...

  3. Make It Connected CodeForces - 1095F (建图+最小生成树)

    Make It Connected CodeForces - 1095F You are given an undirected graph consisting of nn vertices. A ...

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

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

  5. 题解 CF1095F 【Make It Connected】

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

  6. Codeforces Round #529 (Div. 3) F. Make It Connected (贪心,最小生成树)

    题意:给你\(n\)个点,每个点都有权值,现在要在这\(n\)个点中连一颗最小树,每两个点连一条边的边权为两个点的点权,现在还另外给了你几条边和边权,求最小权重. 题解:对于刚开始所给的\(n\)个点 ...

  7. Prim 最小生成树算法

    Prim 算法是一种解决最小生成树问题(Minimum Spanning Tree)的算法.和 Kruskal 算法类似,Prim 算法的设计也是基于贪心算法(Greedy algorithm). P ...

  8. Kruskal 最小生成树算法

    对于一个给定的连通的无向图 G = (V, E),希望找到一个无回路的子集 T,T 是 E 的子集,它连接了所有的顶点,且其权值之和为最小. 因为 T 无回路且连接所有的顶点,所以它必然是一棵树,称为 ...

  9. Hdu 4081 最小生成树

    Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3 ...

随机推荐

  1. 因在缓存对象中增加字段,而导致Redis中取出缓存转化成Java对象时出现反序列化失败的问题

    背景描述 因为业务需求的需要,我们需要在原来项目中的一个DTO类中新增两个字段(我们项目使用的是dubbo架构,这个DTO在A项目/服务的domain包中,会被其他的项目如B.C.D引用到).但是这个 ...

  2. 使用sql实现固定样式的输出

    declare @i int ,@j int, @k varchar(60)   set @i = 1   while   @i <= 12    begin        set @j = 1 ...

  3. 搭建阿里云服务 FTP 折中方案

    该配置的服务都配置了,端口也都打开了 ,但是ftp  就是连接不上 就是打不开目录 8uftp  出现以下情况 配置文件逐条检查,端口逐个检查 都没有问题,还是出现这种情况,实在没辙,蛋疼...... ...

  4. Git初始配置【一】

    Git客户端的工作 安装完成后,还需要最后一步设置,在命令行输入 $ git config --global user.name "Your Name" $ git config ...

  5. docker登录报错Error response from daemon: Get https://192.168.30.10/v1/users/: dial tcp 192.168.30.10:443: connect: connection refused

    背景描述: 登录docker报错: [root@localhost sysconfig]# docker login 192.168.30.10 Username (newcs06): newcs06 ...

  6. 【原创】MongoDB安装配置详解(标注两个坑)

    1.下载安装 3.4正式版([坑]不要最新版,有可能进度卡在这个位置不动,等了半个小时也没什么反映,) http://downloads.mongodb.org/win32/mongodb-win32 ...

  7. [译]如何比较master分支上与git上任意的一个老版本的区别?

    原文来源:https://stackoverflow.com/questions/5586383/how-to-diff-one-file-to-an-arbitrary-version-in-git ...

  8. InfluxDB入门

    InfluxDB是一个用于存储和分析时间序列数据的开源数据库 时序数据是基于时间的一系列的数据 时序数据库就是存放时序数据的数据库,并且需要支持时序数据的快速写入.持久化.多纬度的聚合查询等基本功能 ...

  9. 【视频开发】【计算机视觉】doppia编译之二:boost安装

    编译安装boost库的方法大部分都是来自http://www.linuxidc.com/Linux/2013-07/87573.htm这篇文章,这里我用自己的语言重新组织,稍作修改和补充,最主要是方便 ...

  10. 使用memcpy函数时要注意拷贝数据的长度

    memcpy函数简介 memcpy函数是C/C++语言中的一个用于内存复制的函数,声明在 string.h 中(C++是 cstring).其原型是: void *memcpy(void *desti ...