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;
    }

  • 相关阅读:
    黑客悬赏活动第二期 | 百万ELF赏金,aelf跨链转账标准协议CCTP等你挑战!
    2020年aelf首场全民公测,有奖狂欢四重好礼大放送!
    使用aelf最新稳定测试币AEUSD试玩BingoGame Demo,赢取体验奖金!
    开发者大赛 | aelf轻型DApp开发训练大赛结果公布!
    黑客赏金第一期 | aelf跨链转账标准协议准备就绪,88888ELF赏金等你挑战!
    Twitter AMA预告 | aelf 创始人马昊伯将以【aelf治理与发展】为主题进行在线答疑!
    aelf Enterprise 1.0.0 Preview 2 版正式发布!
    aelf技术点解读 | 分红合约接口实现方案
    深入浅出索引--Mysql45讲笔记记录 打卡day3
    一条SQL语句是如何执行的?--Mysql45讲笔记记录 打卡day1
  • 原文地址:https://www.cnblogs.com/372465774y/p/2588344.html
Copyright © 2011-2022 走看看