• 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英语面试题(核心知识篇)
    Java常用英语汇总(面试必备)
    字符压缩编码
    外排序
    基数排序
    Windows Server 2008 R2 部署服务
    LINUX中常用操作命令
    我的学习笔记_Windows_HOOK编程 2009-12-03 11:19
    CSDN-Code平台使用过程中的5点经验教训
  • 原文地址:https://www.cnblogs.com/mingrigongchang/p/6246253.html
Copyright © 2020-2023  润新知