#include<stdio.h>
#include<stdlib.h>
#define inf 0x3fffffff
#define N 600
struct node {
int x,y,w;
}ma[N*N];
int cmp(const void *a,const void *b) {
return (*(struct node *)a).w-(*(struct node *)b).w;
}
int pre[N];
int find(int x) {
if(x!=pre[x])
pre[x]=find(pre[x]);
return pre[x];
}
int main() {
int t,n,m,i,j,k,aa,bb;
scanf("%d",&t);
while(t--) {
scanf("%d%d",&n,&m);
for(i=0;i<m;i++)
scanf("%d%d%d",&ma[i].x,&ma[i].y,&ma[i].w);
qsort(ma,m,sizeof(ma[0]),cmp);
k=0;
for(i=1;i<=n;i++)
pre[i]=i;
for(i=0;i<m;i++) {
aa=find(ma[i].x);
bb=find(ma[i].y);
if(aa!=bb) {
pre[aa]=bb;
k+=ma[i].w;
}
}
j=inf;
for(i=0;i<n;i++) {
scanf("%d",&m);
if(m<j)
j=m;
}
printf("%d\n",j+k);
}
return 0;
}

nyoj 38 简单并查集的应用&最小生成树的更多相关文章

  1. POJ 2524 (简单并查集) Ubiquitous Religions

    题意:有编号为1到n的学生,然后有m组调查,每组调查中有a和b,表示该两个学生有同样的宗教信仰,问最多有多少种不同的宗教信仰 简单并查集 //#define LOCAL #include <io ...

  2. poj1611 简单并查集

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 32781   Accepted: 15902 De ...

  3. 1213 How Many Tables(简单并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单并查集,统计单独成树的数量. 代码: #include <stdio.h> #i ...

  4. 【简单并查集】Farm Irrigation

    Farm Irrigation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tot ...

  5. ACM_“打老虎”的背后(简单并查集)

    “打老虎”的背后 Time Limit: 2000/1000ms (Java/Others) Problem Description: “习大大”自担任国家主席以来大力反腐倡廉,各地打击贪腐力度也逐步 ...

  6. poj1988 简单并查集

    B - 叠叠乐 Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:30000KB     64bit ...

  7. UVA - 1197 (简单并查集计数)

    Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...

  8. poj 2524 Ubiquitous Religions 一简单并查集

    Ubiquitous Religions   Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 22389   Accepted ...

  9. ACM_城市交通线(简单并查集)

    城市交通线 Time Limit: 2000/1000ms (Java/Others) Problem Description: A国有n座城市,编号为1~n,这n个城市之间没有任何交通线路,所以不同 ...

随机推荐

  1. python TypeError: &#39;builtin_function_or_method&#39; object is not iterable keys

    statinfo = os.stat( OneFilePath ) if AllFiles.has_key( statinfo.st_size ): OneKey = AllFiles[ statin ...

  2. Git与SVN区别 \git学习

    http://blog.csdn.net/w13770269691/article/details/38705473/ https://git-scm.com/book/zh/v2

  3. PyCharm 运行工程

  4. hdu 1002 A + B Problem II(大正整数相加)

    代码: #include<cstdio> #include<cstring> #define Min(a,b) ((a)<(b)?(a):(b)) using names ...

  5. element-UI中table表格的@row-click事件和@selection-change耦合了

    <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark&quo ...

  6. Nginx中的root&alias文件路径及索引目录配置详解

    这篇文章主要介绍了Nginx中的root&alias文件路径及索引目录配置,顺带讲解了root和alias命令的用法,需要的朋友可以参考下     root&alias文件路径配置ng ...

  7. kindoreditor上传图片

    <!doctype html><html> <head> <meta charset="utf-8" /> <title> ...

  8. bind(),call(), apply()方法的区别是什么?

    bind(),call(), apply()方法的区别是什么? 共同点:改变this指向,任何调用都不在起作用 bind() 改变this的指向,不会调用函数,返回一个新的函数 var o ={a:' ...

  9. 前端之HEML

    HTML介绍 Web服务本质   import socket sk = socket.socket() sk.bind(("127.0.0.1", 8080)) sk.listen ...

  10. Centos7中 文件大小排序

    centos7中根据文件大小排序以及jenkins配置每周删除一次jobs日志信息 https://blog.csdn.net/u013066244/article/details/70232050