(最小生成树) Networking -- POJ -- 1287
链接:
http://poj.org/problem?id=1287
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 7494 | Accepted: 4090 |
Description
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 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
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相关推荐
- B - Networking - poj 1287
有一些地方需要铺盖电缆,这些地方两点间的路可能不止一条,需要求出来至少需要多少电缆才能让所有的点都连接起来,当然,间接连接也算. / #include<iostream> #include ...
- 22.12.20补卡 POJ - 1287 Networking
Networking - POJ 1287 - Virtual Judge 纯模板题, 没什么好解释的 /* ⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟ ...
- 【POJ - 1287】 Networking (并查集 + 最小生成树)
题干: You are assigned to design network connections between certain points in a wide area. You are gi ...
- poj 1287 Networking 最小生成树 Kruskal Prim
关于Kruskal和Prim在前面已经有详细的解释以及模板了 有关于需要注意的地方,以及在代码中注释出来. //Kruskal //用结构体保存起始点以及耗费,然后排序后,根据Kruskal #inc ...
- POJ 1287 Networking
传送门:http://poj.org/problem?id=1287 解题思路: 简答的最小生成树 实现代码: #include <iostream> #include <cstdi ...
- 最小生成树模板 POJ——1258
最小生成树是一个比较简单数据结构,形成最小生成树的方式有两种. 最小生成树是有图生成树,保证树的每条边的权值之和最小的生成树就叫做最小生成树,这一类的题目起初比较基础,主要是熟悉模板,POJ 1258 ...
- POJ 1287 Prim算法模板
原题链接:POJ1287 解析:这题我用来练习Prim算法的,算是当作一个模板来看.以下代码有几点要说明的: 我使用了优先队列,并没有使用dist[u]数组来保存当前子树到 u 的最短距离,这样省去了 ...
- 【kuangbin带你飞】专题六 最小生成树
[kuangbin带你飞]专题六 最小生成树 A.POJ - 1251 Jungle Roads (最小生成树模板) The Head Elder of the tropical island of ...
- 2019.9.18最小生成树知识点总结
HDU 4081 Qin Shi Huang's National Road System(次小生成树-Kruskal) 博主的方法很好,但是有疑问,为什么不能将最多人口的两城市的距离设置为0,在进行 ...
最新文章
- 数据结构源码笔记(C语言):基数排序
- 【Paper】2020_异构无人机编队防御及评估策略研究_左剑凯
- [ARC062F]Painting Graphs with AtCoDeer
- Sherri Sparks
- html 自定义字段,HTML 标签自定义属性的问题
- EXC_BAD_ACCESS
- mysql5.6.39的安装_如何安装MySQL Community Server 5.6.39
- java获取列族的列_在cassandra-cli中如何获取表中的所有列名以及如何在java中使用hector获取它?...
- java三种循环结构的关键字,Java循环结构_常量_关键字
- [引]VS2005帮助文档 : 加密 概述
- java什么是继承_JAVA中什么是继承?
- linux双系统无u盘安装教程视频教程,U盘安装Windows和Ubuntu 15.04双系统图解教程
- vim实用指南(9)vimdiff好用的可视化文本对比工具
- 码斗士的修炼之路 -- 如何保持并提升战斗力
- 电商资讯 | 黑鲨大幅裁员,2022年游戏手机销量大跳水,降幅近40%
- 物理内存占用多少正常
- ffmpeg获取音频信息
- Android2.0 Release 1 Eclair API变化预览
- 支付渠道接入设计及实现
- Tryhackme-Starters