• 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;
    }
    


  • 相关阅读:
    一款垃圾的仿资源管理器+局域网Ping网段的工具(winform+异步线程) 留了问题
    ruby实现按键精灵的功能
    委托与事件 在.net的争霸战 ,你选择了谁?(异步委托产生的原因)
    小记C语言指针p与*p
    奔波的这 3月份
    ruby实现删除自定义后缀名文件Find
    "new" 出一个新未来——关于new的揭秘
    学了一招半式策略模式(设计模式)
    配置类的定义与使用
    js编写
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3296990.html
Copyright © 2020-2023  润新知