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

  • 相关阅读:
    Python error: Microsoft Visual C++ 9.0 is required (Unable to find vcvarsall.bat)解决方案
    3-Longest Substring Without Repeating Characters @LeetCode
    2-Add Two Numbers @LeetCode
    1-Two Sum @LeetCode
    DES原理及代码实现
    Linux网络篇,ssh原理及应用
    虚拟机如何通过“仅主机”模式上网
    MySQL入门,第八部分,多表查询(二)
    MySQL入门,第八部分,多表查询(一)
    MySQL入门,第七部分,单表查询
  • 原文地址:https://www.cnblogs.com/372465774y/p/2588344.html
Copyright © 2011-2022 走看看