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

#define maxn 105

struct node
{
    int u, v, len;
    friend bool operator < (node a, node b)
    {
        return a.len > b.len;
    }
};
int f[maxn];

int Find(int x)
{
    if(f[x] != x)
        f[x] = Find(f[x]);
    return f[x];
}

int main()
{
    int N, M;

while(scanf("%d", &N) != EOF && N)
    {
        int i, ans=0;
        priority_queue<node>Q;
        node s;

for(i=0; i<=N; i++)
            f[i] = i;
        scanf("%d", &M);

while(M--)
        {
            scanf("%d%d%d", &s.u, &s.v, &s.len);
            Q.push(s);
        }

while(Q.size())
        {
            s = Q.top();Q.pop();

int u = Find(s.u), v = Find(s.v);

if(u != v)
            {
                f[u] = v;
                ans += s.len;
            }
        }

printf("%d\n", ans);
    }

return 0;
}

转载于:https://www.cnblogs.com/liuxin13/p/4674370.html

B - Networking - poj 1287相关推荐

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

    链接: http://poj.org/problem?id=1287 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7494 ...

  2. 22.12.20补卡 POJ - 1287 Networking

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

  3. POJ 1287 Networking

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

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

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

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

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

  6. POJ 1287 Prim算法模板

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

  7. POJ 图论---1_Uriel's Corner Uriel's Coding Learning Cubing Zone

    原文地址: http://www.cppblog.com/Uriel/articles/121814.html 内容: 刚开始学图论不久,这个是自己做过的一点图论水题,不一定全,有什么错误或者大家有其 ...

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

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

  9. kuangbin带你飞专题合集

    题目列表 [kuangbin带你飞]专题一 简单搜索 [kuangbin带你飞]专题二 搜索进阶 [kuangbin带你飞]专题三 Dancing Links [kuangbin带你飞]专题四 最短路 ...

最新文章

  1. Android studio 报错解决:Error:SSL peer shut down incorrectly
  2. 优化内存中DataTable搜索速度,IP区域汇总
  3. 数据结构(C语言实现》课后1-5章习题答案
  4. android imageview 锯齿,android 自定义圆角ImageView以及锯齿的处理
  5. RabbitMq队列 queue
  6. 写一段代码提高内存占用_记录一次生产环境中Redis内存增长异常排查全流程!...
  7. jquery知识点总结二
  8. 【JAVA】java 堆溢出分析
  9. Redis学习笔记3-Redis5个可运行程序命令的使用
  10. MyBatis教程看这一篇就够啦,简单又全面(IDEA版)
  11. 汇编语言——偏移地址超过有效地址FFFFH
  12. PySpark 之 连接变换 union、intersection、subtract、cartesian
  13. 外包,你是外包不是正式员工,麻烦不要随便吃公司零食
  14. 点击复制按钮复制input中的内容
  15. 宇视摄像头默认密码是多少
  16. Eclipse3.5中安装maven插件出现“ the -vm option in eclipse.ini is pointing to a JDK”解决
  17. 如何下载沪深300历史数据
  18. 校验MD5值是什么意思?
  19. O32:头寸管理,我有话说
  20. stm32学习记录之0.96寸OLED显示屏配置

热门文章

  1. flume写入mysql_Flume高级之自定义MySQLSource
  2. android2.2编译,Android NDK编译Box2D_V2.2
  3. 简述计算机科学的核心内容,北京大学-计算机科学与技术(2018秋)作业及复习
  4. kafka 批量 回写mysql_kafka往mysql写数据操作笔记
  5. 平台如何限制ip流量_ABTest 平台设计 - 如何进行流量分桶
  6. java字符串去重复_Java 8新特性:字符串去重
  7. 山东财经大学东方学院有计算机吗,山东财经大学东方学院大一上学期计算机基础期中考试试题...
  8. 在内网中使用maven_maven构建docker镜像三部曲之三:推送到远程仓库(内网和阿里云)-Go语言中文社区...
  9. python语音翻译软件下载_python软件翻译
  10. binlog流程 mysql_小米 MySQL 数据实时同步到大数据数仓的架构与实践