• POJ 2485 多个数据的最小生成树


    Highways
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 28368   Accepted: 12924

    Description

    The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways so that it will be possible to drive between any pair of towns without leaving the highway system.

    Flatopian towns are numbered from 1 to N. Each highway connects exactly two towns. All highways follow straight lines. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways.

    The Flatopian government wants to minimize the length of the longest highway to be built. However, they want to guarantee that every town is highway-reachable from every other town.

    Input

    The first line of input is an integer T, which tells how many test cases followed.
    The first line of each case is an integer N (3 <= N <= 500), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 65536]) between village i and village j. There is an empty line after each test case.

    Output

    For each test case, you should output a line contains an integer, which is the length of the longest road to be built such that all the villages are connected, and this value is minimum.

    Sample Input

    1
    
    3
    0 990 692
    990 0 179
    692 179 0

    Sample Output

    692
    

    Hint

    Huge input,scanf is recommended.


    事实证明,不能无脑复制模板,一个多数据的while循环的括号问题找半天,还有一开始定义了全局变量n,又在局部又定义了一个,弄的father[n]数组都更新不了
    因为权值已经排好序,直接输出最后一个maxn即可

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <string.h>
    #include <climits>
    using namespace std;
    
    
    int maxn;
    const int N = 600;
    int father[N];
    struct edge{
        int lp,rp,value;
    }ee[N*N]; //边可能会很多,所以一般N*N
    int map[N][N],flag[N][N],numedge,n;
    
    bool cmp(edge a,edge b){
        return a.value < b.value;
    }
    
    //并查集模板
    int find(int x){
        if(father[x] == x)
            return father[x];
           else
                father[x]=find(father[x]);
        return father[x];
    }
    int merge(int x,int y){
        int fx = find(x);
        int fy = find(y);
        if(fx!=fy)
        {
          father[fy] = fx;
          return 1;
        }
        else
            return 0;  //要么bool类型,返回-1也是ture,只有return 0 才是false
    
    }
    
    //克鲁斯卡尔算法
    int kruskal(){
        maxn = 0 ;
        sort(ee,ee+numedge,cmp);
        for(int i = 1; i <= n; ++i)
                father[i] = i;
        int sum = 0;
        for(int i = 0; i < numedge; ++i){
            int lx = ee[i].lp;
            int rx = ee[i].rp;
            if(merge(lx,rx)){
                      maxn=ee[i].value;
    
            }
        }
        return maxn;
    }
    int main(){
    
        int T;
        cin>>T;
        while(T--){
    
            cin>>n;
    
            for(int i = 1; i <= n; ++i){
              for(int j = 1; j <= n; ++j)
                  scanf("%d",&map[i][j]);
            }
            int x,y;
    
    
            numedge = 0;
            //无向图,只用放1-2,1-3,2-3,因为输入是对称的,所以一看就是无向图
            for(int i = 1; i < n; ++i){
                for(int j = i + 1; j <= n; j++){
                        ee[numedge].lp = i;
                        ee[numedge].rp = j;
                        ee[numedge].value = map[i][j];
                        numedge++;
                    }
                }
    
            int ans = kruskal();
            printf("%d
    ",ans);
        }
        return 0;
        }
    
    



  • 相关阅读:
    Java对象初始化
    some tool collections
    链接、单选按钮虚线框与focus:this.blur()与outline
    火狐拓展开发 基础知识
    Multidimensional Array And an Array of Arrays
    Byte Array to Hexadecimal String
    Comparer<T> IComparer<T> IComparable<T>
    Little Puzzlers–List All Anagrams in a Word
    JavaScript-Curry
    OOP in JS Public/Private Variables and Methods
  • 原文地址:https://www.cnblogs.com/zhangmingzhao/p/7256404.html
Copyright © 2020-2023  润新知