• POJ2485——Prim——Highways


    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.

    Source

    POJ Contest,Author:Mathematica@ZSU
    大意:有T组测试例,每一组先输入一个数表示村庄的个数,在下面n*n类似矩阵图形里面输入离本编号的距离比如0 990 692 就是编号为1的到1,到2,到3的距离.要求输出最短路径连通所有村庄后的最长的一条路是多少.-0-...不知道
    There is an empty line after each test case.什么意思,在输入T后面输出一个回车结果pe去掉后就AC了
    大循环n-1次
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int inf = 0x3f3f3f3f,MAX = 3000;
    int map[MAX][MAX],k[MAX];
    void prim (int map[MAX][MAX],int n)
    {
        int m,min1;
       int  ans = 0;
         for(int i = 1; i <= n ;i++)
            k[i] = map[1][i];
    
         for(int i = 1; i < n ;i++){
                min1 = inf;
                m = 0;
             for(int j = 1; j <= n;j++){
                  if(min1 > k[j]&&k[j]!=0){
                     min1 = k[j];
                     m = j;
                  }
               }
               if(min1 > ans)
            ans = min1;
            k[m] = 0;
            for(int j = 1; j <= n ;j++){
                 if(map[m][j] < k[j] && k[j] != 0)
                    k[j] = map[m][j];
            }
         }
         printf("%d
    ",ans);
    }
    int main()
    {
        int T,n;
        scanf("%d",&T);
        while(T--){
                scanf("%d",&n);
                for(int i = 1; i <= n ;i++){
                     for(int j = 1; j <= n ;j++){
                            scanf("%d",&map[i][j]);
                     }
                }
                prim(map,n);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    算法的时间与空间复杂度(一看就懂)
    技术人更应该学会即兴表达
    架构设计之「数据库集群方案」
    架构设计之「数据库从主备到主主的高可用方案」
    网络中的「动态路由算法」,你了解吗?
    当你「ping 一下」的时候,你知道它背后的逻辑吗?
    不懂高性能的负载均衡设计?没关系,架构师带你飞
    piwik流量统计系统搭建(apache2.4+piwik+mysql5.6+php5.6.14)
    SQL Server 之 GROUP BY、GROUPING SETS、ROLLUP、CUBE
    CAS 实现单点登录 .NET MVC
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4321746.html
Copyright © 2020-2023  润新知