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 }
  • 相关阅读:
    css !import
    算法
    java web 运动前端
    oauth 2
    js 优化
    js 代码优化 (写的可以)
    2015/08/17 《有两条均线,你应该注意》
    佳能相机操作 EDSDK 教程 C# 版本
    2015/8/14——了2000股,是否正确呢——明天待验证?
    系统架构师考试——程序计数器 PC, 指令寄存器IR、状态寄存器SR、通用寄存器GR
  • 原文地址:https://www.cnblogs.com/shengshouzhaixing/p/3034374.html
Copyright © 2011-2022 走看看