其他分享
首页 > 其他分享> > poj2485prim

poj2485prim

作者:互联网

Highways
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 40303   Accepted: 17615

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

Hint

Huge input,scanf is recommended.

 

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=502;
int mp[maxn][maxn];//the distance (the distance should be an integer within [1, 65536]) between village i and village j. 
int mindis[maxn];
bool vis[maxn];
int N;//the number of villages N (3 <= N <= 500)
const int Inifity=70000;//0xfffff溢出 
int prim()
{//每次找距离树最近的结点 
    int ans;
    memset(vis,0,sizeof(vis));//初始化 
    memset(mindis,Inifity,sizeof(mindis));
    mindis[0]=0;
    ans=0;
    for(int i=0;i<N;i++){//遍历每一个结点 
        int tmp=Inifity,k=0;
        for(int j=0;j<N;j++){
            if(!vis[j]&&mindis[j]<tmp){//如果该结点没有被访问过,并且目前最短距离 
                tmp=mindis[j];
                k=j;//记录当前到生成树的最短距离的结点 
            }
        }
        if(tmp==Inifity) return 0;//没有最短距离,即无路可走,最小生成树建立失败 
        vis[k]=true;//访问标记
         
        if(ans<tmp){
            ans=tmp;//保存答案 
        }
        
        for(int j=0;j<N;j++){
            if(!vis[j]&&mindis[j]>mp[k][j]){//出度到未遍历结点的最短距离 
                mindis[j]=mp[k][j];//记录未遍历的结点到生成树的最短距离
            }
        }
    }
    return ans;
}
 
int main()
{
    int T;//测试用例 
    cin>>T;
    while(T--){
        cin>>N;
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                cin>>mp[i][j];//地图
            }
        }
        cout<<prim()<<endl;
    }
    return 0;
}

 

标签:poj2485prim,int,highways,maxn,mp,integer,highway
来源: https://www.cnblogs.com/littleyyhome/p/11923699.html