题目链接

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

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

#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. Mysql索引面试题

    转载:https://mp.weixin.qq.com/s/_bk2JVOm2SkXfdcvki6-0w 本文来自一位不愿意透露姓名的粉丝投稿,由Hollis整理并"还原"了面试现 ...

  2. vue+elementui搭建后台管理界面(8 同步/异步获取数据渲染table)

    elementui已经封装好了 el-table 组件,只需要指定 data 数据源即可,因此通常在 vue 实例生命周期的 created 阶段,从数据库获取数据,再将返回的数据绑定到 data 如 ...

  3. Netty集成Protobuf

    一.创建Personproto.proto 创建Personproto.proto文件 syntax = "proto2"; package com.example.protobu ...

  4. mysql索引原理及优化(二)

    索引原理分析:数据结构 索引是最常见的慢查询优化方式其是一种优化查询的数据结构,MySql中的索引是用B+树实现,而B+树就是一种数据结构,可以优化查询速度,可以利用索引快速查找数据,优化查询. 可以 ...

  5. SpringBoot——读取配置文件@Value和@Configuration比较

    1.@Configuration package com.xgcd.springboot.bean; import org.springframework.boot.context.propertie ...

  6. String字符串与其他格式(int、boolean等八大数据类型)的相互转换

    1.(String 转 int)String str = "123"; int a = Integer.parseInt(str);(int 转 String)int a = 5; ...

  7. AudioManager: android插上耳机仍然使用扬声器播放音频

    手机音频的输出有外放(Speaker).听筒(Telephone Receiver).有线耳机(WiredHeadset).蓝牙音箱(Bluetooth A2DP)等输出设备.在平时,电话免提.插拔耳 ...

  8. EF6中的SQL监控

    在MVC或WEBAPI中的监控 System.Action<string> action = (string message) => { Debug.WriteLine(messag ...

  9. Swift编码总结6

    1.UILabel的minimumScaleFactor: 需要UIlabel根据字数多少来减小字体大小,使得UIlabel能够显示全所有的文字.你需要做的就是设置minimumScaleFactor ...

  10. jenkins中启用tag标签

    参照里面的第9步: https://www.cnblogs.com/effortsing/p/10468840.html