zoukankan      html  css  js  c++  java
  • POJ2485——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.
    分析:最小生成树求最大权值~
    源码:
    #include<iostream>
    #include<string.h>
    #include<stdio.h>
    #include<ctype.h>
    #include<algorithm>
    #include<stack>
    #include<queue>
    #include<set>
    #include<math.h>
    #include<vector>
    #include<deque>
    #include<list>
    using namespace std;
    int n;
    int i,j;
    int map[550][550];
    bool mark[550];
    int prim()
    {
        int w=n;
        int k;
        int len;
        int max=-1;
        while(--w)
        {
            int min=999999;
            int y=0;
            for(i=2;i<=n;i++)
            {
                if(!mark[i]&&map[1][i]<min)
                {
                    min=map[1][i];
                    k=i;
                }
            }
            mark[k]=1;
            if(max<min)
            max=min;
            for(i=2;i<=n;i++)
            {
                if(!mark[i]&&map[k][i]<map[1][i])
                map[1][i]=map[k][i];
            }
        }
        return max;
    }
    int main()
    {
        int test;
        scanf("%d",&test);
        while(test--)
        {
            memset(map,0,sizeof(map));
            memset(mark,0,sizeof(mark));
            scanf("%d",&n);
            for(i=1; i<=n; i++)
                for(j=1; j<=n; j++)
                    scanf("%d",&map[i][j]);
                    printf("%d
    ",prim());
        }
        return 0;
    }
    


  • 相关阅读:
    判断输入的是否是汉字(中文,不包括中文符号)
    第XX行将截断字符串或二进制数据。语句已终止
    Joomla学习之旅开始啦
    bean加载与注入之重新理解 L
    Excelsior JET 7.6 MP5 发布
    Squid Analyzer 5.1 发布,Squid日志统计
    flashrd 1.3 发布,OpenBSD 安装器
    Oracle 宣布 MySQL 5.6 正式版发布
    Spring Hateoas 0.4 发布
    MacPorts 2.1.3 发布,Mac 软件包管理
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3296990.html
Copyright © 2011-2022 走看看