题目链接

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

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

#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;
}

最新文章

  1. org.springframework.jdbc.UncategorizedSQLException异常
  2. LeetCode Patching Array
  3. Kth Largest Element in an Array - LeetCode
  4. python启动服务器
  5. spark sql cache
  6. MyEclipse的快捷使用(含关联源码和Doc的方式)
  7. cf 189B - Counting Rhombi
  8. 通过命令修改wampserver的mysql密码
  9. POJ Countries in War 3114
  10. linux查看接口连接状态
  11. Binomial Coeffcients 过去山东省省赛冠军
  12. java类的加载和执行顺序
  13. Best Reward 拓展kmp
  14. 移动端利用-webkit-box水平垂直居中
  15. Python3学习之路~8.3 socket 服务端与客户端
  16. myeclipse修改了安装目录名字打不开解决方法
  17. java中远程http文件上传及file2multipartfile
  18. 高盛为什么认为中国AI领域将超越美国?
  19. jenkins 入门教程(上)【转】
  20. 7 款超炫的 jQuery 插件

热门文章

  1. 谈下sublime和vscode
  2. windows 10下启用docker的k8s集群
  3. compile install deploy;
  4. 【Beta】软件使用说明——致社长
  5. 论文解读:3D Hand Shape and Pose Estimation from a Singl RGB Image
  6. mariadb 10.2/mysql 8.0实现递归
  7. Cache busting
  8. mysql 中 int 等类型如何选择
  9. Qt请求http地址数据
  10. 快排的时间复杂度O(n) = nlogn计算过程