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



  • 相关阅读:
    maven pom
    Amazon(vpc)对等网络
    AWS IAM用户启用MFA
    AWS系列-EC2实例镜像选择
    AWS系列-EC2实例添加磁盘
    AWS系列-AWS EC2实例类型改配(机器配置升级)
    AWS必要了解知识
    AWS系列-EC2默认限制说明
    AWS系列-S3实现文件服务页面展示
    AWS 区域与可用区
  • 原文地址:https://www.cnblogs.com/playboy307/p/5273834.html
Copyright © 2011-2022 走看看