zoukankan      html  css  js  c++  java
  • POJ 1861 Network

    Network
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 10104   Accepted: 3796   Special Judge

    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
    

    Source

    Northeastern Europe 2001, Northern Subregion
    //开始还以为样例错了、呵呵、
    //考验英语水平、、呵呵

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #define M 150003
    #define N 1003
    #include <algorithm>
    using namespace std;
    struct node
    {
        int u,v,cost;
    };
    node kr[M];
    bool cmp(const node&a,const node&b)
    {
        return a.cost<b.cost;
    }
    int n,m;
    int f[N],r[N];
    int Find(int x)
    {
        if(x!=f[x])
          return f[x]=Find(f[x]);
        return x;
    }
    int E[N];
    void kruskal()
    {
        int i;
        int Max,k;
        for(i=1;i<=n;i++)
          f[i]=i,r[i]=0;
        sort(kr,kr+m,cmp);
        Max=k=0;
        int x,y;
        for(i=0;i<m;i++)
        {
           x=Find(kr[i].u);
           y=Find(kr[i].v);
           if(x!=y)
           {
               E[k++]=i;
              if(kr[i].cost>Max)
                 Max=kr[i].cost;
             if(r[x]>r[y])
               f[y]=x;
              else if(r[y]>r[x])
                   f[x]=y;
                   else
                   {
                       f[y]=x;
                       r[x]++;
                   }
           }
        }
            printf("%d\n",Max);
            printf("%d\n",k);
            for(int j=0;j<k;j++)
             printf("%d %d\n",kr[E[j]].u,kr[E[j]].v);
    }
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            for(int i=0;i<m;i++)
             scanf("%d%d%d",&kr[i].u,&kr[i].v,&kr[i].cost);
             kruskal();
        }
        return 0;
    }

  • 相关阅读:
    Linux下如何查看版本信息
    linux的top命令参数详解
    浅谈Linux内存管理机制
    python3 判断字符串是否为纯空格组成的方法
    python3 字符串/列表/元组(str/list/tuple)相互转换方法及join()函数的使用
    python3 json数据格式的转换(dumps/loads的使用、dict to str/str to dict、json字符串/字典的相互转换)
    python3 list列表随机选取一个元素、随机选择一个user-agent
    windows系统中,在当前目录下打开cmd命令行的两种方法
    name 'reload' is not defined解决方法
    Vue.js05:vue内联样式
  • 原文地址:https://www.cnblogs.com/372465774y/p/2588344.html
Copyright © 2011-2022 走看看