ZJNU 1528 - War--高级
2024-08-30 18:13:17
类似于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 ;
}
最新文章
- 【SharePoint学习笔记】第2章 SharePoint Windows PowerShell 指南
- centos修改hostname以及时间同步
- Mybatis的ResultMap的使用
- signed char、unsigned char
- php-fpm正在生成页面时,浏览器刷新后,php-fpm会退出吗?
- 对C#泛型中的new()约束思考
- hdu 4622 Reincarnation(后缀数组)
- HDU 4709 Herding 几何题解
- 成为JAVA软件开发工程师要学哪些东西
- SQL STUFF函数 拼接字符串
- 小甲鱼:Python学习笔记001_变量_分支_数据类型_运算符等基础
- 设计模式的征途—10.装饰(Decorator)模式
- JSP标签c:forEach实例
- node七-required、缓存
- Linux下面使用命令如何运行.sh文件的两种解决办法
- Mongodb到mysql数据库的数据迁移(Java,Windows)
- 转://Oracle 11gR2 硬件导致重新添加节点
- ObjC.primitive-methods
- poj 2488 A Knight's Journey
- (LeetCode)用两个栈实现一个队列
热门文章
- 学术Essay写作如何体现逻辑的应用
- python+opencv+dlib瘦脸效果
- opencv.js双边滤波 磨皮处理
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring事务管理接口PlatformTransactionManager、TransactionDefinition和TransactionStatus
- Kicstart+pxe搭建自动化安装Linux 整理了一下
- 使用SSH工具连接WSL
- 二十、CI框架数据库操作之查看生产的sql语句
- 六、CI框架之分配变量
- c# 循环界面控件
- 初识MyBatis-Generator