• Highways


    Highways

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 7   Accepted Submission(s) : 4
    Problem 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
     

    Source
    PKU
     

    裸的map,题中要求计算最小生成树中最长的边,只需要记录一下就好,代码:


    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    #define INF 0xfffffff
    int map[1010][1010],mark[1010],maxx;
    int main()
    {
    	int t;
    	scanf("%d",&t);
    	while(t--)
    	{
    		int m,n,i,j;
    		scanf("%d",&m);
    		for(i=1;i<=m;i++)
    		for(j=1;j<=m;j++)
    		{
    			scanf("%d",&n);
    			map[i][j]=map[j][i]=n;
    		}
    		int sum=0,flog;
    		int p=m;
    		maxx=0;
    		memset(mark,0,sizeof(mark));
    		while(--p)
    		{
    			int min=INF;
    			flog=-1;
    			for(i=2;i<=m;i++)
    			{
    				if(!mark[i]&&map[1][i]<min)
    				{
    					min=map[1][i];
    					flog=i;
    				}
    			}
    				if(maxx<min) maxx=min;
    				sum+=min;
    				mark[flog]=1;
    			
    			for(i=2;i<=m;i++)
    			{
    				if(!mark[i]&&map[1][i]>map[flog][i])
    				map[1][i]=map[flog][i];
    			}
    		}
    		printf("%d
    ",maxx);
    	}
    	return 0;
    }



  • 相关阅读:
    类的特殊获取方式——《Thinking in Java》随笔008
    方法排序规范——《Thinking in Java》随笔007
    权限修饰符(访问指示符)——《Thinking in Java》随笔006
    史上最强,万能类型:Object——《Thinking in Java》随笔005
    七天C#小结
    编译原理10 消除左递归
    编译原理9 DFA最小化,语法分析初步
    编译原理8 非确定的自动机NFA确定化为DFA
    编译原理7 正规式、正规文法与自动机
    编译原理6 正规文法与正规式
  • 原文地址:https://www.cnblogs.com/playboy307/p/5273834.html
Copyright © 2020-2023  润新知