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



  • 相关阅读:
    逻辑地址、线性地址、物理地址
    查找已知字符串子串
    替换字符串中的空格为%20
    资本的奥秘
    net::ERR_CONNECTION_RESET的处理方法
    SQL Server数据库从低版本向高版本复制数据库
    中式思维的五大逻辑缺陷(转)
    1年PK12年,中国式教育完败(转载)
    有关衣服的想法
    jquery邮箱自动补全
  • 原文地址:https://www.cnblogs.com/playboy307/p/5273834.html
Copyright © 2011-2022 走看看