题目链接

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

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

#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. axios跨域请求报错:Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.

    在做项目时,用到axios,数据用post提交时,老是报错,错误提示为: Access to XMLHttpRequest at 'http://127.0.0.1:3000/api/add' fro ...

  2. 第07组 Alpha冲刺(6/6)

    队名:摇光 队长:杨明哲 组长博客:求戳 作业博客:求再戳 队长:杨明哲 过去两天完成了哪些任务 文字/口头描述:博客生成的逻辑 展示GitHub当日代码/文档签入记录:(组内共用,已询问过助教小姐姐 ...

  3. 范仁义html+css课程---6、表格

    范仁义html+css课程---6.表格 一.总结 一句话总结: 表格中最常用的元素就是table.tr.td.th,还有语义化的thead.tbody.tfoot标签 1.表格构成三个基本要素? t ...

  4. how does SELECT TOP works when no order by is specified?

    how does SELECT TOP works when no order by is specified? There is no guarantee which two rows you ge ...

  5. [转]c++多线程编程之pthread线程深入理解

    多线程编程之pthread线程深入理解         Pthread是 POSIX threads 的简称,是POSIX的线程标准.           前几篇博客已经能给你初步的多线程概念.在进一 ...

  6. Spring cloud微服务安全实战-7-8ELK+SpringBoot环境搭建

    采集不可聚合的离散的.日志信息的e ELK是三个系统的简称 LogStash:用来做日志的收集.过滤.格式转换 Kibana:和普罗米修斯的grafana一个意思.主要用来展示数据. 用docker来 ...

  7. 有无关键字new的区别

    function Person(firstName, lastName) { this.firstName = firstName; this.lastName = lastName; } const ...

  8. python之psutil模块

    简述 psutil是一个跨平台库(http://code.google.com/p/psutil/) ,能够轻松实现获取系统运行的进程和系统利用率(包括CPU.内存.磁盘.网络等)信息.它主要应用于系 ...

  9. Bladex-Boot使用Postman调用服务说明

    一:GitBladex-Boot项目,并启动服务 二:打开Postman 三:使用Post,调用http://localhost/blade-auth/oauth/token/ 配置:Header中增 ...

  10. 最新 大众书网java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.大众书网等10家互联网公司的校招Offer,因为某些自身原因最终选择了大众书网.6.7月主要是做系统复习.项目复盘.Leet ...