zoukankan      html  css  js  c++  java
  • POJ1861 Network (Kruskal算法 +并查集)

    Network

    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
    3
    1 2
    1 3
    3 4

    题解:原题目数据输出有问题,该题就是典型的Krusal算法,这里使用并查集判断有没有环形成。

    #include <iostream>
    #include<algorithm>
    #include<stdio.h>
    #include<cstring>
    using namespace std;
    typedef long long ll;
    const int maxn=20000;
    int pre[maxn],height[maxn];
    void init_set(int n){
        for (int i = 1; i <= n; i++){
            pre[i]=i;    
        }
        memset(height,0,sizeof(height));
    }
    
    int find_set(int x){
        return x==pre[x]?x:pre[x]=find_set(pre[x]);
    }
    void union_set(int x,int y){
        x= find_set(x);
        y= find_set(y);
        if(x==y)return;
        if(height[x]==height[y]){
            height[x]=height[x]+1;
            pre[y]=x;
        }else{
            if(height[x]<height[y]) pre[x]=y;
            else{
                pre[y]=x;
            }
        }
    }
    
    struct edge{
        int u,v,w;
        bool operator <(const edge& a)const{
            return w<a.w;
        }
    };
    int main(){
        int n,m;
        while(~scanf("%d%d",&n,&m)){
            init_set(n);
            edge* edges = new edge[m+1];
            bool *record=new bool[m+1]; 
            // memset(record,false,sizeof(record));
            for (int i = 1; i <=m; i++){
                record[i]=false;
                scanf("%d%d%d",&edges[i].u,&edges[i].v,&edges[i].w);
            }
            sort(edges+1,edges+m+1);
            int maxcable=edges[0].w,sums=0,num=0;
            int u,v;
            for (int i = 1; i <=m; i++){
                u=edges[i].u;
                v=edges[i].v;
                if(find_set(u)!=find_set(v)){
                    union_set(u,v);
                    num++;
                    record[i]=true;
                }
                if(num>=n-1){
                    maxcable=edges[i].w;
                    break;
                }
            }    
            cout<<maxcable<<endl;
            cout<<n-1<<endl;
            for (int i = 1; i<=m; i++){
                if(record[i]){
                    cout<<edges[i].u<<" "<<edges[i].v<<endl;
                }
            }
        }
    }

     

    因上求缘,果上努力~~~~ 作者:每天卷学习,转载请注明原文链接:https://www.cnblogs.com/BlairGrowing/p/14288232.html

  • 相关阅读:
    C语言函数手册—函数分类列表
    HTTP协议详解(一直在用可是这篇太好了转一下)
    Substance风格实例大全javaswing皮肤风格大全(原)
    Component creation must be done on Event Dispatch Thread错误解决方法
    用开源Look&Feel (Substance)写 漂亮的Swing应用程序
    Swift翻译之-关于Swift
    Swift翻译之-Swift语法入门 Swift语法介绍
    Swift语言简介+快速上手
    JDK各个版本的新特性jdk1.5-jdk8
    5个常用Java代码混淆器 助你保护你的代码
  • 原文地址:https://www.cnblogs.com/BlairGrowing/p/14288232.html
Copyright © 2011-2022 走看看