解题思路:Kruskal模板题,重复输入的情况,本题是无向图。

见代码:

 #include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn = ;
int n, m, father[],w[][]; int Find(int x)
{
return father[x] == x ? x : father[x] = Find(father[x]);
} struct node{
int x, y, w;
}p[maxn]; int cmp(node A, node B)
{
return A.w < B.w;
} int main()
{
int a, b, x;
while(~scanf("%d", &n) && n)
{
scanf("%d", &m);
for(int i = ; i <= n; i++)
{
for(int j = i; j <= n; j++)
{
if(i == j) w[i][j] = ;
else w[i][j] = w[j][i] = inf;
}
}
for(int i = ; i < m; i++)
{
scanf("%d%d%d", &a, &b, &x);
p[i].x = a, p[i].y = b, p[i].w = x;
//如果两点有多条路,这步取更小的值
if(w[a][b] > x) w[a][b] = w[b][a] = x;
}
sort(p, p + m, cmp); //从小到大
//并查集初始化
for(int i = ; i <= n; i++) father[i] = i;
int sum = ;
for(int i = ; i < m; i++)
{
int rootx = Find(p[i].x);
int rooty = Find(p[i].y);
//不在同一个集合就加起来
if(rootx != rooty)
{
sum += p[i].w;
father[rootx] = rooty;
}
}
printf("%d\n", sum); }
return ;
}

POJ1287 Networking的更多相关文章

  1. poj-1287 Networking(Prim)

    题目链接:http://poj.org/problem?id=1287 题目描述: 请先参考关于prim算法求最小生成树的讲解博客:https://www.cnblogs.com/LJHAHA/p/1 ...

  2. POJ1287 Networking【最小生成树】

    题意: 给出n个节点,再有m条边,这m条边代表从a节点到b节点电缆的长度,现在要你将所有节点都连起来,并且使长度最小 思路: 这是个标准的最小生成树的问题,用prim的时候需要注意的是他有重边,取边最 ...

  3. 最小生成树练习1(克鲁斯卡尔算法Kruskal)

    今天刷一下水题练手入门,明天继续. poj1861 Network(最小生成树)新手入门题. 题意:输出连接方案中最长的单根网线长度(必须使这个值是所有方案中最小的),然后输出方案. 题解:本题没有直 ...

  4. poj图论解题报告索引

    最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman- ...

  5. 信息中心网络 ,Information-centric networking, ICN

  6. Unity 官网教程 -- Multiplayer Networking

    教程网址:https://unity3d.com/cn/learn/tutorials/topics/multiplayer-networking/introduction-simple-multip ...

  7. 最小生成树 prime poj1287

    poj1287 裸最小生成树 代码 #include "map" #include "queue" #include "math.h" #i ...

  8. 延迟容忍网络(Delay-tolerant networking)

    标签: 网络networking存储工作network路由器 2012-03-24 10:01 3702人阅读 评论(0) 收藏 举报 分类: 计算机网络(12) 版权声明:本文为博主原创文章,对文章 ...

  9. OpenStack Networking overview

    原文地址:http://docs.openstack.org/newton/install-guide-ubuntu/neutron-concepts.html Networking service ...

随机推荐

  1. PAT 天梯赛 L1-017. 到底有多二 【水】

    题目链接 https://www.patest.cn/contests/gplt/L1-017 AC代码 #include <iostream> #include <cstdio&g ...

  2. canvas笔记1

    w3c定义: <canvas> 标签定义图形,比如图表和其他图像. <canvas> 标签只是图形容器,您必须使用脚本来绘制图形. canvas 对象 属性: width he ...

  3. hadoop08---读写锁

    ReentrantLock 直接使用lock接口的话,我们需要实现很多方法,不太方便,ReentrantLock是唯一实现了Lock接口的类,并且ReentrantLock提供了更多的方法,Reen ...

  4. C# 获取当前星期几

    private static string GetWeekName() { string weekName = ""; switch (DateTime.Now.DayOfWeek ...

  5. idea使用maven骨架创建maven项目

    Maven 骨架创建 Java Web 项目 1) File -> New -> Project... 2) 如下图 3)如下图 GroupId和ArtifactId<项目名> ...

  6. IIS Manager could not load type for module provider 'SharedConfig' that is declared in administration.config

    https://support.microsoft.com/en-ie/help/3151973/iis-shared-configuration-feature-requires-all-serve ...

  7. Java解析XML的四种方法详解 - 转载

    XML现在已经成为一种通用的数据交换格式,平台的无关性使得很多场合都需要用到XML.本文将详细介绍用Java解析XML的四种方法 在做一般的XML数据交换过程中,我更乐意传递XML字符串,而不是格式化 ...

  8. 交换机上的trunk,hybrid,access配置和应用(转)

    交换机上的trunk,hybrid,access配置和应用 以太网端口的链路类型: Access类型:端口只能属于一个vlan,一般用于连接计算机. Trunk类型:端口可以属于端个vlan,可以接收 ...

  9. python urllib2库的简单总结

    urllib2的简单介绍参考网址:http://www.voidspace.org.uk/python/articles/urllib2.shtml Fetching URLsThe simplest ...

  10. 第一章 Burp Suite 安装和环境配置

    Burp Suite是一个集成化的渗透测试工具,它集合了多种渗透测试组件,使我们自动化地或手工地能更好的完成对web应用的渗透测试和攻击.在渗透测试中,我们使用Burp Suite将使得测试工作变得更 ...