1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define inf 0x3fffffff
  4. #define N 600
  5. struct node {
  6. int x,y,w;
  7. }ma[N*N];
  8. int cmp(const void *a,const void *b) {
  9. return (*(struct node *)a).w-(*(struct node *)b).w;
  10. }
  11. int pre[N];
  12. int find(int x) {
  13. if(x!=pre[x])
  14. pre[x]=find(pre[x]);
  15. return pre[x];
  16. }
  17. int main() {
  18. int t,n,m,i,j,k,aa,bb;
  19. scanf("%d",&t);
  20. while(t--) {
  21. scanf("%d%d",&n,&m);
  22. for(i=0;i<m;i++)
  23. scanf("%d%d%d",&ma[i].x,&ma[i].y,&ma[i].w);
  24. qsort(ma,m,sizeof(ma[0]),cmp);
  25. k=0;
  26. for(i=1;i<=n;i++)
  27. pre[i]=i;
  28. for(i=0;i<m;i++) {
  29. aa=find(ma[i].x);
  30. bb=find(ma[i].y);
  31. if(aa!=bb) {
  32. pre[aa]=bb;
  33. k+=ma[i].w;
  34. }
  35. }
  36. j=inf;
  37. for(i=0;i<n;i++) {
  38. scanf("%d",&m);
  39. if(m<j)
  40. j=m;
  41. }
  42. printf("%d\n",j+k);
  43. }
  44. return 0;
  45. }

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. Centos7操作系统部署指南

    一.硬件环境: Dell R620 二.软件环境: Centos6.4 X86_64 +KVM Windows7+vnc 三.安装说明 操作系统更新之迅速,让作为新手的系统运维人员有点措手不及,相对于 ...

  2. N数码问题的启发式搜索算法--A*算法python实现

    一.启发式搜索:A算法 1)评价函数的一般形式 : f(n) = g(n) + h(n) g(n):从S0到Sn的实际代价(搜索的横向因子) h(n):从N到目标节点的估计代价,称为启发函数(搜索的纵 ...

  3. 写个js动态调整图片宽高 (原创)

    <body style="TEXT-ALIGN: center;"> <div id="testID" style="backgro ...

  4. python学习-字符串 列表 元祖

    目录 Python翻转字符串(reverse string) 简单的步长为-1, 即字符串的翻转(常用) 递归反转 借用列表,使用reverse()方法 字符串常用操作 index split 切片 ...

  5. 大数字运算——1、BigInteger

    package com.wh.BigInteger; import java.math.BigInteger; import java.util.Arrays; /** * @author 王恒 * ...

  6. Data内置对象

    1.内置对象 Date 日期对象 2.创建日期对象 2.1 根据当前的系统时间来创建日期对象. var date1 = new Date(); //a.输出日期对象的信息 console.log(da ...

  7. (转)Java进阶java int与Integer的区别

    Java进阶java int与Integer的区别 前言 int与Integer的区别从大的方面来说就是基本数据类型与其包装类的区别: int 是基本类型,直接存数值,而Integer是对象,用一个引 ...

  8. 给<hr/>添加样式

    点线式 破折线式 直线式 双线式 脊线式 槽线式 内嵌效果的 突起效果的 border-top:10px 设置水平线的大小 <hr style=" border-top:5px dot ...

  9. Android-crop:漂亮的图片裁切工具

    主要特点 Gradle构建和AAR 现代化的UI 向后兼容到SDK 10 配置简单 示例项目 使用 首先,在你的 manifest 文件中申明 CropImageActivity : <acti ...

  10. 【Oracle】Rman简介

    1.备份和恢复功能的用途: –保护数据 -介质故障 -用户错误 -应用程序错误 –数据保存和历史数据保留 –数据传输 2.典型的备份和恢复任务 –通过配置数据库实现可恢复 –定义备份计划 –对不同类型 ...