POJ1287 Networking

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

见代码:

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 using namespace std;
 5 #define inf 0x3f3f3f3f
 6 const int maxn = 100005;
 7 int n, m, father[55],w[55][55];
 8
 9 int Find(int x)
10 {
11     return father[x] == x ? x : father[x] = Find(father[x]);
12 }
13
14 struct node{
15     int x, y, w;
16 }p[maxn];
17
18 int cmp(node A, node B)
19 {
20     return A.w < B.w;
21 }
22
23 int main()
24 {
25     int a, b, x;
26     while(~scanf("%d", &n) && n)
27     {
28         scanf("%d", &m);
29         for(int i = 1; i <= n; i++)
30         {
31             for(int j = i; j <= n; j++)
32             {
33                 if(i == j) w[i][j] = 0;
34                 else w[i][j] = w[j][i] = inf;
35             }
36         }
37         for(int i = 0; i < m; i++)
38         {
39             scanf("%d%d%d", &a, &b, &x);
40             p[i].x = a, p[i].y = b, p[i].w = x;
41             //如果两点有多条路,这步取更小的值
42             if(w[a][b] > x) w[a][b] = w[b][a] = x;
43         }
44         sort(p, p + m, cmp); //从小到大
45         //并查集初始化
46         for(int i = 1; i <= n; i++) father[i] = i;
47         int sum = 0;
48         for(int i = 0; i < m; i++)
49         {
50             int rootx = Find(p[i].x);
51             int rooty = Find(p[i].y);
52             //不在同一个集合就加起来
53             if(rootx != rooty)
54             {
55                 sum += p[i].w;
56                 father[rootx] = rooty;
57             }
58         }
59         printf("%d\n", sum);
60
61     }
62     return 0;
63 }

时间: 2024-11-22 19:07:38

POJ1287 Networking的相关文章

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

今天刷一下水题练手入门,明天继续. poj1861 Network(最小生成树)新手入门题. 题意:输出连接方案中最长的单根网线长度(必须使这个值是所有方案中最小的),然后输出方案. 题解:本题没有直接求生成树,但如果连接n个集线器的方案多于n-1条边,那么必存在回路,因此去掉某些边剩下的边和所有顶点构成一个生成树.对于一个图的最小生成树来说,它的最大边满足所有生成树的最大边里最小,正和题意. 吐槽:题目样例是错的... 1 #include<cstdio> 2 #include<cst

poj图论解题报告索引

最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman-ford,spfa) poj1511 - Invitation Cards(单源来回最短路径,spfa邻接表) poj1797 - Heavy Transportation(最大边,最短路变形,dijkstra,spfa,bellman-ford) poj2240 - Arbitrage(汇率问题,

「题解」kuangbin 最小生成树

POJ-1251 Jungle Roads (水题,%c) POJ-1287 Networking (水) POJ-2031 Building a Space Station (%f浮点数尴尬精度,两球间距离) POJ-2421 Constructing Roads (一些边已建好,简单处理一下) ZOJ-1586 QS Network (处理一下边权) HDU-1233 还是畅通工程 (水) HDU-1875 畅通工程再续 (浮点数,条件连边) HDU-1301 Jungle Roads (重

ZOJ1372 POJ 1287 Networking 网络设计 Kruskal算法

题目链接:ZOJ1372 POJ 1287 Networking 网络设计 Networking Time Limit: 2 Seconds      Memory Limit: 65536 KB 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

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

Networking Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5976   Accepted: 3231 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 po

使用Multiplayer Networking做一个简单的多人游戏例子-2/3(Unity3D开发之二十六)

猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/51007512 使用Multiplayer Networking做一个简单的多人游戏例子-1/3 使用Multiplayer Networking做一个简单的多人游戏例子-2/3 使用Multiplayer Networking做一个简单的多人游戏例子-3/3 7. 在网络中控制Player移动 上一篇中,玩家操

networking modes in VirtualBox

Not attached In this mode, VirtualBox reports to the guest that a network card is present, but that there is no connection -- as if no Ethernet cable was plugged into the card. This way it is possible to "pull" the virtual Ethernet cable and dis

High Performance Browser Networking

Table of Contents Foreword Preface About This Book Conventions Used in This Book Safari? Books Online How to Contact Us Content Updates May 23, 2014 I. Networking 101 1. Primer on Latency and Bandwidth Speed Is a Feature The Many Components of Latenc

CentOS7 Failed to start LSB: Bring up/down networking. 已解决!!!

服务器更换了主板之前配置的静态IP发现启动网卡出现异常. 执行 service network restart 出现以下错误 Restarting network (via systemctl):  Job for network.service failed. See 'systemctl status network.service' and 'journalctl -xn' for details.   尝试了好多种方法终于彻底解决掉了. 在centos7系统下执行: systemctl