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

    Network
    Time Limit: 1000MS  Memory Limit: 30000K
    Total Submissions: 13456  Accepted: 5209  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

     
    /*******************************************
    
           author   :   Grant Yuan
           time     :   2014.7.31
           algorithm:   Kruskal
           source   :   POJ 1861
           notice   :   sample is error!
    
    ********************************************/
    
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<queue>
    #include<vector>
    #define INF 0x3fffffff
    #define MAX1 1003
    #define MAX2 15003
    
    using namespace std;
    
    queue<int>q;
    int par[MAX1];
    struct edge{int u;int v;int cost;};
    edge e[MAX2];
    int n,m,ans;
    int ml;
    bool cmp(edge aa,edge bb)
    {
        return aa.cost<bb.cost;
    }
    
    int findp(int aa)
    {
        if(aa==par[aa]) return aa;
        else
            return par[aa]=findp(par[aa]);
    }
    
    void unite(int aa,int bb)
    {
        int a1,b1;
        a1=findp(aa);
        b1=findp(bb);
        par[b1]=a1;
    }
    
    bool same(int aa,int bb)
    {
        return findp(aa)==findp(bb);
    }
    
    int main()
    {
       while(~scanf("%d%d",&n,&m)){
        ans=0;ml=0;
        while(!q.empty()) q.pop();
        for(int i=1;i<=n;i++)
        {
            par[i]=i;
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].cost);
        }
        sort(e+1,e+m+1,cmp);
        for(int i=1;i<=m;i++)
        {
            if(!same(e[i].u,e[i].v)){
                unite(e[i].u,e[i].v);
                ans++;
                if(e[i].cost>ml)
                     ml=e[i].cost;
                q.push(i);
            }
        }
        int t;
        printf("%d
    ",ml);
        printf("%d
    ",ans);
        while(!q.empty()){
            t=q.front();
            q.pop();
            printf("%d %d
    ",e[t].u,e[t].v);
        }
       }
        return 0;
    }
    


  • 相关阅读:
    永无乡「HNOI2012」
    ruby基础知识之 class&module
    Linux命令集锦
    Ruby知识总结-一般变量+操作符+if+数组和哈希
    VMware通过VMnet8共享本地网络
    VMware Workstation 不可恢复错误 (vcpu-0)
    WIN10安装时msxml4.0提示2502、2503解决办法
    C# 委托知识总结
    request.servervariables参数
    判断MS SQLSERVER临时表是否存在
  • 原文地址:https://www.cnblogs.com/codeyuan/p/4254468.html
Copyright © 2011-2022 走看看