ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

poj2485prim

2019-11-24 19:07:29  阅读:239  来源: 互联网

标签:poj2485prim int highways maxn mp integer highway


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有