zoukankan      html  css  js  c++  java
  • 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.



    给出n个点分别到每个点的距离   给出的实例中的三行三个数字   分别是第一个点分别到第1 2 3个点的距离  第二个点到第1 . 2 3个点的距离  第三个点到第1 2 3 个点的距离      题目就是这么的迷人

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    struct node
    {
    	int f,to,val;
    }bian[100001];
    int pre[10001];
    int find(int p)
    {
    	while(p!=pre[p])
    	{
    		p=pre[p];
    	}
    	return p;
    }
    bool cmp(node a,node b)
    {
    	return a.val <b.val ;
    }
    int main()
    {
    	int t;
    	scanf("%d",&t);
    	while(t--)
    	{
    		int n,m,shu=0;;
    		scanf("%d",&n);
    		for(int i=1;i<=n;i++)
    		{
    			pre[i]=i;
    		}
    		for(int i=1;i<=n;i++)
    		{
    			for(int j=1;j<=n;j++)
    			{
    				scanf("%d",&m);
    				bian[shu].f =i;
    				bian[shu].to =j;
    				bian[shu].val =m;
    				shu++;
    			}
    		}
    		sort(bian,bian+shu,cmp);
    		int ma=0;
    		for(int i=0;i<shu;i++)
    		{
    			int fx=find(bian[i].f );
    			int fy=find(bian[i].to);
    			if(fx!=fy)
    			{
    				if(ma<bian[i].val )
    				{
    					ma=bian[i].val ;
    				}
    				pre[fx]=fy;
    			}
    		}
    		printf("%d
    ",ma);
    	}
    	return 0;
    }


  • 相关阅读:
    js日期加减得到新的日期的自定义函数
    c# winform 一个可以用鼠标改变控件位置和大小的类,调用即可
    颜色英文代码全集
    你的Web系统有多少安全漏洞
    robots协议和禁止搜索引擎收录
    Linux下./configure错误详解
    c# winform 关于DataGridView的一些操作
    通过JSONP实现完美跨域
    tabs选项卡
    jQuery 1.4的十五大新功能实例精讲
  • 原文地址:https://www.cnblogs.com/kingjordan/p/12027092.html
Copyright © 2011-2022 走看看