zoukankan      html  css  js  c++  java
  • 最小生成树&并查集 POJ 1861 Network

    B - Network
    Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u

    Description

    Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the company, they can be connected to each other using cables. Since each worker of the company must have access to the whole network, each hub must be accessible by cables from any other hub (with possibly some intermediate hubs). 
    Since cables of different types are available and shorter ones are cheaper, it is necessary to make such a plan of hub connection, that the maximum length of a single cable is minimal. There is another problem — not each hub can be connected to any other one because of compatibility problems and building geometry limitations. Of course, Andrew will provide you all necessary information about possible hub connections. 
    You are to help Andrew to find the way to connect hubs so that all above conditions are satisfied. 

    Input

    The first line of the input contains two integer numbers: N - the number of hubs in the network (2 <= N <= 1000) and M - the number of possible hub connections (1 <= M <= 15000). All hubs are numbered from 1 to N. The following M lines contain information about possible connections - the numbers of two hubs, which can be connected and the cable length required to connect them. Length is a positive integer number that does not exceed 106. There will be no more than one way to connect two hubs. A hub cannot be connected to itself. There will always be at least one way to connect all hubs.

    Output

    Output first the maximum length of a single cable in your hub connection plan (the value you should minimize). Then output your plan: first output P - the number of cables used, then output P pairs of integer numbers - numbers of hubs connected by the corresponding cable. Separate numbers by spaces and/or line breaks.

    Sample Input

    4 6
    1 2 1
    1 3 1
    1 4 2
    2 3 1
    3 4 1
    2 4 1
    

    Sample Output

    1
    4
    1 2
    1 3
    2 3
    3 4

    解题思路:按照平常思路读懂题意就可以解决了!

    解题代码:
    View Code
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <stdio.h>
     4 #include <string.h>
     5 using namespace std;
     6 
     7 const int maxn = 15005;
     8 int fa[maxn], rank[maxn];
     9 
    10 struct node
    11 {
    12     int x, y;
    13     int w;
    14     bool operator < (const node T) const
    15     {
    16         return w < T.w;
    17     }
    18 }hub[maxn];
    19 int temp[maxn];
    20 
    21 int Find (int x)
    22 {
    23     if (x != fa[x])
    24         return fa[x] = Find(fa[x]);
    25     return x;
    26 }
    27 
    28 void Union (int x, int y)
    29 {
    30     if (rank[x] > rank[y])
    31     {
    32         fa[y] = x;
    33     }
    34     else
    35     {
    36         if (rank[x] == rank[y])
    37             rank[y] ++;
    38         fa[x] = fa[y];
    39     }
    40 }
    41 
    42 int main ()
    43 {
    44     int n, m;
    45     int x, y, w;
    46     int cun;
    47     while (~scanf ("%d%d", &n, &m))
    48     {
    49         cun = 0;
    50         for (int i = 0; i < m; i ++)
    51         {
    52             scanf ("%d%d%d", &x, &y, &w);
    53             hub[i] = (node){x, y, w};
    54         }
    55         for (int i = 1; i <= n; i ++)
    56         {
    57             rank[i] = 0;
    58             fa[i] = i;
    59         }
    60         sort (hub, hub + m);
    61         int Max = 0;
    62         for (int i = 0; i < m; i ++)
    63         {
    64             x = Find(hub[i].x);
    65             y = Find(hub[i].y);
    66             if  (x != y)
    67             {
    68                 temp[cun++] = i;
    69                 Union (x, y);
    70                 Max = max (Max, hub[i].w);
    71             }
    72         }
    73         printf ("%d\n%d\n", Max, cun);
    74         for (int i = 0; i < cun; i ++)
    75             printf ("%d %d\n",hub[temp[i]].x, hub[temp[i]].y);
    76     }
    77     return 0;
    78 }
  • 相关阅读:
    史上最走心的Webpack4.0中级教程——配置之外你应该知道事
    javascript基础修炼(11)——DOM-DIFF的实现
    一统江湖的大前端(7)React.js-从开发者到工程师
    express中间件系统的基本实现
    js中如何在不影响既有事件监听的前提下新增监听器
    Zabbix的应用(6)----常见错误
    Ansible初级应用
    一个好用的小工具 thefuck
    使用python读取word文件里的表格信息
    01010_Eclipse中项目的jar包导入与导出
  • 原文地址:https://www.cnblogs.com/shengshouzhaixing/p/3034374.html
Copyright © 2011-2022 走看看