链接:

http://poj.org/problem?id=1287

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7494   Accepted: 4090

Description

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

Sample Output

  1. 0
  2. 17
  3. 16
  4. 26

代码:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. const int N = ;
  9. const int INF = 0xfffffff;
  10.  
  11. int n;
  12. int J[N][N], dist[N];
  13. bool vis[N];
  14.  
  15. int Prim()
  16. {
  17. int i, j, ans=;
  18. dist[]=;
  19. memset(vis, , sizeof(vis));
  20. vis[]=;
  21.  
  22. for(i=; i<=n; i++)
  23. dist[i]=J[][i];
  24.  
  25. for(i=; i<n; i++)
  26. {
  27. int index=;
  28. int MIN=INF;
  29. for(j=; j<=n; j++)
  30. {
  31. if(!vis[j] && dist[j]<MIN)
  32. {
  33. index=j;
  34. MIN=dist[j];
  35. }
  36. }
  37. vis[index]=;
  38. ans += MIN;
  39. for(j=; j<=n; j++)
  40. {
  41. if(!vis[j] && dist[j]>J[index][j])
  42. dist[j]=J[index][j];
  43. }
  44. }
  45. return ans;
  46. }
  47.  
  48. int main ()
  49. {
  50. while(scanf("%d", &n), n)
  51. {
  52. int m, i, j, a, b, t;
  53. scanf("%d", &m);
  54.  
  55. for(i=; i<=n; i++)
  56. for(j=; j<=i; j++)
  57. J[i][j]=J[j][i]=INF;
  58.  
  59. for(i=; i<=m; i++)
  60. {
  61. scanf("%d%d%d", &a, &b, &t);
  62. J[a][b]=J[b][a]=min(J[a][b], t);
  63. }
  64.  
  65. int ans=Prim();
  66.  
  67. printf("%d\n", ans);
  68. }
  69. return ;
  70. }

(最小生成树) Networking -- POJ -- 1287的更多相关文章

  1. Networking POJ - 1287 最小生成树板子题

    #include<iostream> #include<algorithm> using namespace std; const int N=1e5; struct edge ...

  2. Networking POJ - 1287

    题目链接:https://vjudge.net/problem/POJ-1287 思路:最小生成树板子题 #include <iostream> #include <cstdio&g ...

  3. B - Networking - poj 1287

    有一些地方需要铺盖电缆,这些地方两点间的路可能不止一条,需要求出来至少需要多少电缆才能让所有的点都连接起来,当然,间接连接也算. /////////////////////////////////// ...

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

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

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

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

  6. poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题

    poj 1251  && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...

  7. POJ.1287 Networking (Prim)

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

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

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

  9. POJ 1287:Networking(最小生成树Kruskal)

    id=1287">Networking Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5976   Acce ...

随机推荐

  1. TortoiseGit 使用 HTTP 方式每次 PUSH 无需输入密码的方法

    由于 BitBucket 被墙,导致使用时只能用HTTPS代理的方式,但TortoiseGit貌似没有记忆密码的功能,以至于每次push时都要求输入密码,很是麻烦!在网上搜到的保存密码的方式也有点笨. ...

  2. JPEG和Variant的转换

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

  3. 吴裕雄 实战python编程(1)

    import sqlite3 conn = sqlite3.connect('E:\\test.sqlite') # 建立数据库联接cursor = conn.cursor() # 建立 cursor ...

  4. python处理分隔大文件

    4个.sql格式的文件,2G大小,直接插入mysql数据中,文件太大了,导入不进去. 太大的文件用python处理也很麻烦,处理不了,只能先分隔成小文件处理. 文件中数据格式:其中values里面的数 ...

  5. Mysql 内部默认排序

    mysql默认的排序: https://forums.mysql.com/read.php?21,239471,239688#msg-239688 Do not depend on order whe ...

  6. Spring Boot中使用Websocket搭建即时聊天系统

    1.首先在pom文件中引入Webscoekt的依赖 <!-- websocket依赖 --> <dependency> <groupId>org.springfra ...

  7. nginx accept() failed (24: Too many open files)

    nginx服务器出现如下信息: [crit] 17221#0: accept4() failed (24: Too many open files) [crit] 17221#0: accept4() ...

  8. Spring框架的JDBC模板技术概述

    1. Spring框架中提供了很多持久层的模板类来简化编程,使用模板类编写程序会变的简单 2. 提供了JDBC模板,Spring框架提供的 * JdbcTemplate类 3. Spring框架可以整 ...

  9. Spring框架之log日志的使用

    1.Spring框架也需要引入日志相关的jar包 * 在spring-framework-3.0.2.RELEASE-dependencies/org.apache.commons/com.sprin ...

  10. Linq和EF 做 单一条件查询 和 复合条件 查询 以及 多表 联合查询 示例

    单一条件查询: var table2Object = (from t1 in db.table1 join t2 in db.table2 on t1.id equals t2.id select t ...