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



  • 相关阅读:
    jvm类加载机制
    线程误区-join,wait(里边还是调用的wait)
    线程间通信wait和notify【All】简介
    指令重排序
    window.opener
    scrollIntoView()
    保持饥饿,保持愚蠢
    执行sql语句,不依靠实体 获取string值
    join函数详解
    html常用代码大全
  • 原文地址:https://www.cnblogs.com/playboy307/p/5273834.html
Copyright © 2011-2022 走看看