Highways

来源:互联网 发布:vim c语言配置 编辑:程序博客网 时间:2024/05/21 10:20

Highways

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

      最小生成树问题,Prem算法一套下来解决。

代码如下:

#include<iostream>#include<stdio.h>#include<cstring>using namespace std;const int MAX=1e9;int main(){    int t,n;    int p[510][510],dis[510],vis[510];    cin>>t;    while(t--)    {        memset(p,0,sizeof(p));        memset(vis,0,sizeof(vis));        cin>>n;        for(int i=1;i<=n;i++)        {            for(int j=1;j<=n;j++)            {                scanf("%d",&p[i][j]);            }        }        memset(dis,0x7f,sizeof(dis));        dis[1]=0;        int maxn=0,k;        for(int i=1;i<=n;i++)        {            k=0;            for(int j=1;j<=n;j++)            {                if(vis[j]==0 && dis[k]>dis[j])                {                    k=j;                }            }            vis[k]=1;//标记成白点            for(int j=1;j<=n;j++)            {                if(vis[j]==0 && p[k][j]<dis[j])                {                    dis[j]=p[k][j];                }            }        }        for(int i=1;i<=n;i++)            if(maxn<dis[i])                maxn=dis[i];        cout<<maxn<<endl;    }    return 0;}


原创粉丝点击