题意  给你n个点 m条边  求最小生成树的权

这是最裸的最小生成树了

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. using namespace std;
  5. const int N = 55, M = 3000;
  6. int par[N], n, m, ans;
  7. struct edge{int u, v, w;} e[M];
  8. bool cmp(edge a, edge b){return a.w < b.w;}
  9.  
  10. int Find(int x)
  11. {
  12. int r = x, tmp;
  13. while(par[r] >= 0) r = par[r];
  14. while(x != r)
  15. {
  16. tmp = par[x];
  17. par[x] = r;
  18. x = tmp;
  19. }
  20. return r;
  21. }
  22.  
  23. void Union (int u, int v)
  24. {
  25. int ru = Find(u), rv = Find(v), tmp = par[ru] + par[rv];
  26. if(par[ru] < par[rv])
  27. par[rv] = ru, par[ru] = tmp;
  28. else
  29. par[ru] = rv, par[rv] = tmp;
  30. }
  31.  
  32. void kruskal()
  33. {
  34. memset(par, -1, sizeof(par));
  35. int cnt = 0;
  36. for(int i = 1; i <= m; ++i)
  37. {
  38. int u = e[i].u, v = e[i].v;
  39. if(Find(u) != Find(v))
  40. {
  41. ++cnt;
  42. ans += e[i].w;
  43. Union(u, v);
  44. }
  45. if(cnt >= n - 1) break;
  46. }
  47. }
  48.  
  49. int main()
  50. {
  51. int u, v, w;
  52. while(scanf("%d", &n), n)
  53. {
  54. scanf("%d", &m);
  55. for(int i = 1; i <= m; ++i)
  56. {
  57. scanf("%d%d%d", &u, &v, &w);
  58. e[i].u = u, e[i].v = v, e[i].w = w;
  59. }
  60. sort(e + 1, e + m + 1, cmp);
  61. ans = 0;
  62. kruskal();
  63. printf("%d\n", ans);
  64. }
  65. return 0;
  66. }

Networking


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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 0



2 3

1 2 37

2 1 17

1 2 68

3 7

1 2 19

2 3 11

3 1 7

1 3 5

2 3 89

3 1 91

1 2 32

5 7

1 2 5

2 3 7

2 4 8

4 5 11

3 5 10

1 5 6

4 2 12

0

Sample Output

0

17

16

26

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. POJ.1287 Networking (Prim)

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

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

    Networking 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/B Description You are assigned ...

  6. poj 1287 Networking【最小生成树prime】

    Networking Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7321   Accepted: 3977 Descri ...

  7. [kuangbin带你飞]专题六 最小生成树 POJ 1287 Networking

    最小生成树模板题 跑一次kruskal就可以了 /* *********************************************** Author :Sun Yuefeng Creat ...

  8. POJ - 1287 Networking 【最小生成树Kruskal】

    Networking Description You are assigned to design network connections between certain points in a wi ...

  9. POJ 1287 Networking(最小生成树裸题有重边)

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

随机推荐

  1. csdn的登录框好难看

    不好意思说实话了,新的登陆框样式挺难看的,那种橙不明朗,介于黄和橙之间,跟整个网站主色调红和黑很不搭.不过,倒是有点跟风Win8平实的style,但是比Win8更简陋了点. tooltip要不加都不加 ...

  2. form表单标签的enctype属性的作用

    Enctype是指定将数据回发到server时浏览器使用的编码类型.其编码类型有下面三种 一. application/x-www-form-urlencoded         这是通过表单发送数据 ...

  3. HNCU1101:马的移动(BFS)

    http://hncu.acmclub.com/index.php?app=problem_title&id=111&problem_id=1101 题目描述 小明很喜欢下国际象棋,一 ...

  4. 取CPU序列号,获取网卡,取硬盘系列号,获取目录下的文件,强制删除目录

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  5. Cocos2d-x 地图行走的实现3:A*算法

    本文乃Siliphen原创,转载请注明出处:http://blog.csdn.net/stevenkylelee 上一节<Cocos2d-x 地图行走的实现2:SPFA算法>: http: ...

  6. 【iOS】Swift字符串截取方法的改进

    字符串截取方法是字符串处理中经常使用的基本方法.熟悉iOS的朋友都知道在基础类的NSString中有substringToIndex:,substringFromIndex:以及substringWi ...

  7. JavaScript 中的继承(读书笔记思维导图)

    继承是 OO 语言中的一个最为人津津乐道的概念.许多 OO 语言都支持两种继承方式:接口继承和实现继承.接口继承只继承方法签名,而实现继承则继承实际的方法.由于函数没有签名,在 ECMAScript ...

  8. 如何搭建DNS服务(转)

    继NTP时间服务器后,继续搭建DNS服务,鉴于昨晚撰写时间超过预期,这次改变策略,先把自己需要用到的部分写出来(主要是基于RAC的搭建,只涉及正向和反向DNS解析),后面再添加必要的说明和阐述. 试验 ...

  9. Spring MVC Hello World Example(转)

    Spring 3 You may interest at this Spring 3 MVC hello world example. In Spring MVC web application, i ...

  10. Codeforces Round #246 (Div. 2) —B. Football Kit

    B. Football Kit time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...