You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible routes for the cables that may connect pairs of points. For each possible route between two points, you are given the length of the cable that is needed to connect the points over that route. Note that there may exist many possible routes between two given points. It is assumed that the given possible routes connect (directly or indirectly) each two points in the area.
Your task is to design the network for the area, so that there is a connection (direct or indirect) between every two points (i.e., all the points are interconnected, but not necessarily by a direct cable), and that the total length of the used cable is minimal.

Input

The input file consists of a number of data sets. Each data set defines one required network. The first line of the set contains two integers: the first defines the number P of the given points, and the second the number R of given routes between the points. The following R lines define the given routes between the points, each giving three integer numbers: the first two numbers identify the points, and the third gives the length of the route. The numbers are separated with white spaces. A data set giving only one number P=0 denotes the end of the input. The data sets are separated with an empty line.
The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes are identified with integers between 1 and P (inclusive). The routes between two points i and j may be given as i j or as j i.

Output

For each data set, print one number on a separate line that gives the total length of the cable used for the entire designed network.

Sample Input

  1. 1 0
  2.  
  3. 2 3
  4. 1 2 37
  5. 2 1 17
  6. 1 2 68
  7.  
  8. 3 7
  9. 1 2 19
  10. 2 3 11
  11. 3 1 7
  12. 1 3 5
  13. 2 3 89
  14. 3 1 91
  15. 1 2 32
  16.  
  17. 5 7
  18. 1 2 5
  19. 2 3 7
  20. 2 4 8
  21. 4 5 11
  22. 3 5 10
  23. 1 5 6
  24. 4 2 12
  25.  
  26. 0
  1. #include<iostream>
  2. #include<algorithm>
  3. using namespace std;
  4. int ans=,tot=;
  5. const int N = 1e5;
  6. int f[];
  7. struct ac{
  8. int v,u,w;
  9. }edge[N];
  10. bool cmp(ac a,ac b){
  11. return a.w<b.w;
  12. }
  13. inline int find(int x){
  14. if(x!=f[x])f[x]=find(f[x]);return f[x];
  15. }
  16. inline int join(int x,int y,int w){
  17. int fx=find(x),fy=find(y);
  18. if(fx!=fy){
  19. f[fx]=fy;ans+=w;tot++;
  20. }
  21. }
  22. int main()
  23. {
  24. int n;string a,b;int m,w,cnt=;
  25. while(cin>>n>>m){
  26. ans=tot=cnt=;
  27. for(int i = ;i <=n;++i)f[i]=i;
  28. for(int i = ;i < m;++i){
  29. cin>>edge[i].u>>edge[i].v>>edge[i].w;
  30. }
  31. sort(edge,edge+m,cmp);
  32. for(int i = ;i < m;++i){
  33. join(edge[i].u,edge[i].v,edge[i].w);
  34. if(tot==n-)break;
  35. }
  36. cout<<ans<<endl;
  37. }
  38. return ;
  39. }

POJ - 1287 Networking (最小生成树&并查集的更多相关文章

  1. POJ 1287 Networking (最小生成树)

    Networking Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit S ...

  2. POJ 1287 Networking (最小生成树模板题)

    Description You are assigned to design network connections between certain points in a wide area. Yo ...

  3. ZOJ1372 POJ 1287 Networking 网络设计 Kruskal算法

    题目链接:problemCode=1372">ZOJ1372 POJ 1287 Networking 网络设计 Networking Time Limit: 2 Seconds     ...

  4. UVA 1395 苗条的生成树(最小生成树+并查集)

    苗条的生成树 紫书P358 这题最后坑了我20分钟,怎么想都对了啊,为什么就wa了呢,最后才发现,是并查集的编号搞错了. 题目编号从1开始,我并查集编号从0开始 = = 图论这种题真的要记住啊!!题目 ...

  5. POJ.1287 Networking (Prim)

    POJ.1287 Networking (Prim) 题意分析 可能有重边,注意选择最小的边. 编号依旧从1开始. 直接跑prim即可. 代码总览 #include <cstdio> #i ...

  6. CSP 201703-4 地铁修建【最小生成树+并查集】

    问题描述 试题编号: 201703-4 试题名称: 地铁修建 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 A市有n个交通枢纽,其中1号和n号非常重要,为了加强运输能力,A市 ...

  7. 关于最小生成树(并查集)prime和kruskal

    适合对并查集有一定理解的人.  新手可能看不懂吧.... 并查集简单点说就是将相关的2个数字联系起来 比如 房子                      1   2    3   4  5   6 ...

  8. poj 1733(带权并查集+离散化)

    题目链接:http://poj.org/problem?id=1733 思路:这题一看就想到要用并查集做了,不过一看数据这么大,感觉有点棘手,其实,我们仔细一想可以发现,我们需要记录的是出现过的节点到 ...

  9. poj 1182 食物链 (并查集)

    http://poj.org/problem?id=1182 关于并查集 很好的一道题,开始也看了一直没懂.这次是因为<挑战程序设计竞赛>书上有讲解看了几遍终于懂了.是一种很好的思路,跟网 ...

  10. POJ 1182 食物链(并查集拆点)

    [题目链接] http://poj.org/problem?id=1182 [题目大意] 草原上有三种物种,分别为A,B,C A吃B,B吃C,C吃A. 1 x y表示x和y是同类,2 x y表示x吃y ...

随机推荐

  1. Java多线程和并发(十一),CAS(Compare and Swap)

    目录 1.CAS简介 2.CAS多数情况下对开发者来说是透明的 3.CAS缺点 十一.CAS(Compare and Swap) Synchronized直观意义上是一种悲观锁 cas则是乐观锁的一种 ...

  2. BZOJ 3940 Censoring ( Trie 图 )

    题目链接 题意 : 中文题.点链接 分析 : 直接建 Trie 图.在每一个串的末尾节点记录其整串长度.方便删串操作 然后对于问询串.由于可能有删串操作 所以在跑 Trie 图的过程当中需要拿个栈记录 ...

  3. 多版本python的pip 升级后, pip2 pip3 与python版本失配

    mint19.2   本来pip 和 pip2 对应 python2.7   pip3对应python3.6 用源码安装了python3.7之后. 这样 版本也没问题. 但是,  用pip3.7 安装 ...

  4. (Java多线程系列四)停止线程

    停止线程 停止线程的思路 ①使用退出标志,使线程正常退出,也就是当run()方法结束后线程终止. class Thread01 extends Thread { // volatile关键字解决线程的 ...

  5. Concurrent包里的其他东西:ArrayBlockingQueue、CountDownLatch等等。

    1:本例介绍一个特殊的队列:BlockingQueue,如果BlockingQueue是空的,从BlockingQueue取东西的操作将会被阻断进入等待状态,直到BlockingQueue进了东西才会 ...

  6. 【转】Diamond -- 分布式配置中心

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  7. Linux TCP套接字选项 之 SO_REUSEADDR && SO_REUSEPORT

    说明 前面从stackoverflow上找了一篇讲这两个选项的文章,文章内容很长,读到最后对Linux中的这两个选项还是有些迷茫,所以重新写一篇文章来做一个总结: 本文只总结TCP单播部分,并且只讨论 ...

  8. 七、创建UcRESTTemplate请求管理器

    一.创建UcRESTTemplate管理器封装 import com.alibaba.fastjson.JSON; import org.apache.http.client.config.Reque ...

  9. html 绘图

    <html> <head> <title>canvas绘制图形</title> <style> body{ margin: 0px; pad ...

  10. Android AARCH64 平台的 OpenCL 配置

    原文地址:Android AARCH64 平台的 OpenCL 配置 Android AARCH64 平台的 OpenCL 配置 开发环境 IDE: Android Studio 3.4.1 Andr ...