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


  • 相关阅读:
    开源 免费 java CMS
    运行shell脚本报错 &#39;357273277&#39;: command not found 解决的方法
    Android学习笔记之Spinner下拉列表使用案例
    HDU 1542 Atlantis (线段树 + 扫描线 + 离散化)
    DrawerLayout
    云计算设计模式(十三)——领导人选举模式
    算法之贪心思想
    [Android]Volley源代码分析(叁)Network
    oracle TABLE ACCESS BY INDEX ROWID 你不知道的索引回表-开发系列(三)
    JavaScript No Overloading 函数无重载之说
  • 原文地址:https://www.cnblogs.com/codeyuan/p/4254468.html
Copyright © 2011-2022 走看看