链接:

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 02 3
1 2 37
2 1 17
1 2 683 7
1 2 19
2 3 11
3 1 7
1 3 5
2 3 89
3 1 91
1 2 325 7
1 2 5
2 3 7
2 4 8
4 5 11
3 5 10
1 5 6
4 2 120

Sample Output

0
17
16
26

代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;const int N = 210;
const int INF = 0xfffffff;int n;
int J[N][N], dist[N];
bool vis[N];int Prim()
{int i, j, ans=0;dist[1]=0;memset(vis, 0, sizeof(vis));vis[1]=1;for(i=1; i<=n; i++)dist[i]=J[1][i];for(i=1; i<n; i++){int index=1;int MIN=INF;for(j=1; j<=n; j++){if(!vis[j] && dist[j]<MIN){index=j;MIN=dist[j];}}vis[index]=1;ans += MIN;for(j=1; j<=n; j++){if(!vis[j] && dist[j]>J[index][j])dist[j]=J[index][j];}}return ans;
}int main ()
{while(scanf("%d", &n), n){int m, i, j, a, b, t;scanf("%d", &m);for(i=1; i<=n; i++)for(j=1; j<=i; j++)J[i][j]=J[j][i]=INF;for(i=1; i<=m; i++){scanf("%d%d%d", &a, &b, &t);J[a][b]=J[b][a]=min(J[a][b], t);}int ans=Prim();printf("%d\n", ans);}return 0;
}

转载于:https://www.cnblogs.com/YY56/p/4735018.html

(最小生成树) Networking -- POJ -- 1287相关推荐

  1. B - Networking - poj 1287

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

  2. 22.12.20补卡 POJ - 1287 Networking

    Networking - POJ 1287 - Virtual Judge 纯模板题, 没什么好解释的 /* ⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟ ...

  3. 【POJ - 1287】 Networking (并查集 + 最小生成树)

    题干: You are assigned to design network connections between certain points in a wide area. You are gi ...

  4. poj 1287 Networking 最小生成树 Kruskal Prim

    关于Kruskal和Prim在前面已经有详细的解释以及模板了 有关于需要注意的地方,以及在代码中注释出来. //Kruskal //用结构体保存起始点以及耗费,然后排序后,根据Kruskal #inc ...

  5. POJ 1287 Networking

    传送门:http://poj.org/problem?id=1287 解题思路: 简答的最小生成树 实现代码: #include <iostream> #include <cstdi ...

  6. 最小生成树模板 POJ——1258

    最小生成树是一个比较简单数据结构,形成最小生成树的方式有两种. 最小生成树是有图生成树,保证树的每条边的权值之和最小的生成树就叫做最小生成树,这一类的题目起初比较基础,主要是熟悉模板,POJ 1258 ...

  7. POJ 1287 Prim算法模板

    原题链接:POJ1287 解析:这题我用来练习Prim算法的,算是当作一个模板来看.以下代码有几点要说明的: 我使用了优先队列,并没有使用dist[u]数组来保存当前子树到 u 的最短距离,这样省去了 ...

  8. 【kuangbin带你飞】专题六 最小生成树

    [kuangbin带你飞]专题六 最小生成树 A.POJ - 1251 Jungle Roads (最小生成树模板) The Head Elder of the tropical island of ...

  9. 2019.9.18最小生成树知识点总结

    HDU 4081 Qin Shi Huang's National Road System(次小生成树-Kruskal) 博主的方法很好,但是有疑问,为什么不能将最多人口的两城市的距离设置为0,在进行 ...

最新文章

  1. 数据结构源码笔记(C语言):基数排序
  2. 【Paper】2020_异构无人机编队防御及评估策略研究_左剑凯
  3. [ARC062F]Painting Graphs with AtCoDeer
  4. Sherri Sparks
  5. html 自定义字段,HTML 标签自定义属性的问题
  6. EXC_BAD_ACCESS
  7. mysql5.6.39的安装_如何安装MySQL Community Server 5.6.39
  8. java获取列族的列_在cassandra-cli中如何获取表中的所有列名以及如何在java中使用hector获取它?...
  9. java三种循环结构的关键字,Java循环结构_常量_关键字
  10. [引]VS2005帮助文档 : 加密 概述
  11. java什么是继承_JAVA中什么是继承?
  12. linux双系统无u盘安装教程视频教程,U盘安装Windows和Ubuntu 15.04双系统图解教程
  13. vim实用指南(9)vimdiff好用的可视化文本对比工具
  14. 码斗士的修炼之路 -- 如何保持并提升战斗力
  15. 电商资讯 | 黑鲨大幅裁员,2022年游戏手机销量大跳水,降幅近40%
  16. 物理内存占用多少正常
  17. ffmpeg获取音频信息
  18. Android2.0 Release 1 Eclair API变化预览
  19. 支付渠道接入设计及实现
  20. Tryhackme-Starters

热门文章

  1. python使用阿里云sdk
  2. 请求转发与请求重定向
  3. Android 浅谈动画
  4. Office Tab免费版:标签化浏览和编辑Office文档
  5. 看了SUMTEC的稍微思考了一下…… 感触颇深。讲一件身边的事:
  6. 剑指offer 算法 (抽象建模能力)
  7. mybatis注解开发
  8. Mock Server
  9. C# 函数参数object sender, EventArgs e
  10. 【贪心】小Y的炮[cannon]题解