类似于1213取水

可以把空投当作第0个城市

最后将0~n的所有城市跑最小生成树

#include<iostream>
#include<algorithm>
using namespace std;
struct road{
int from,to,cost;
bool operator < (const road &a) const{
return cost<a.cost;
}
}ar[];
int gp[];
int find(int a){
return a==gp[a]?a:gp[a]=find(gp[a]);
}
void merge(int a,int b){
gp[find(b)]=find(a);
}
int main(){
ios::sync_with_stdio();cin.tie();
int T,i,n,m,k;
long long res;
cin>>T;
while(T--){
cin>>n>>m;
for(i=;i<n;i++){
ar[i*].from=i+;
ar[i*].to=;
cin>>ar[i*].cost;
ar[i*+].from=;
ar[i*+].to=i+;
ar[i*+].cost=ar[i*].cost;
}
for(i=n;i<n+m;i++){
cin>>ar[i*].from>>ar[i*].to>>ar[i*].cost;
ar[i*+].from=ar[i*].to;
ar[i*+].to=ar[i*].from;
ar[i*+].cost=ar[i*].cost;
}
sort(ar,ar+(m+n)*);
for(i=;i<=n;i++)
gp[i]=i;
for(k=res=i=;i<(m+n)*;i++){
if(k==n)
break;
if(find(ar[i].from)!=find(ar[i].to)){
merge(ar[i].from,ar[i].to);
res+=ar[i].cost;
k++;
}
}
cout<<res<<'\n';
} return ;
}

最新文章

  1. 【SharePoint学习笔记】第2章 SharePoint Windows PowerShell 指南
  2. centos修改hostname以及时间同步
  3. Mybatis的ResultMap的使用
  4. signed char、unsigned char
  5. php-fpm正在生成页面时,浏览器刷新后,php-fpm会退出吗?
  6. 对C#泛型中的new()约束思考
  7. hdu 4622 Reincarnation(后缀数组)
  8. HDU 4709 Herding 几何题解
  9. 成为JAVA软件开发工程师要学哪些东西
  10. SQL STUFF函数 拼接字符串
  11. 小甲鱼:Python学习笔记001_变量_分支_数据类型_运算符等基础
  12. 设计模式的征途—10.装饰(Decorator)模式
  13. JSP标签c:forEach实例
  14. node七-required、缓存
  15. Linux下面使用命令如何运行.sh文件的两种解决办法
  16. Mongodb到mysql数据库的数据迁移(Java,Windows)
  17. 转://Oracle 11gR2 硬件导致重新添加节点
  18. ObjC.primitive-methods
  19. poj 2488 A Knight's Journey
  20. (LeetCode)用两个栈实现一个队列

热门文章

  1. 学术Essay写作如何体现逻辑的应用
  2. python+opencv+dlib瘦脸效果
  3. opencv.js双边滤波 磨皮处理
  4. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring事务管理接口PlatformTransactionManager、TransactionDefinition和TransactionStatus
  5. Kicstart+pxe搭建自动化安装Linux 整理了一下
  6. 使用SSH工具连接WSL
  7. 二十、CI框架数据库操作之查看生产的sql语句
  8. 六、CI框架之分配变量
  9. c# 循环界面控件
  10. 初识MyBatis-Generator