Highways

来源:互联网 发布:网络路径错误怎么办 编辑:程序博客网 时间:2024/05/01 02:15

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
130 990 692990 0 179692 179 0
 

Sample Output
692
 

Source
PKU
 

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


#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define INF 0xfffffffint 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\n",maxx);}return 0;}



0 0
原创粉丝点击