Highways
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 23033   Accepted: 10612

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

本来n就很小,没有这么做的必要,反而加重了插入负担
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
typedef pair<int ,int > P;
int dis[600][600],n;
bool vis[600];
priority_queue<P,vector<P>,greater<P> >que;
int prim(){
int ans=0;
memset(vis,0,sizeof(vis));
vis[0]=true;
while(!que.empty())que.pop();
for(int i=1;i<n;i++)que.push(P(dis[0][i],i));
int num=1;
while(!que.empty()&&num<n){
int tp=que.top().second;int d=que.top().first;que.pop();
// printf("tp %d dis %d\n",tp,d);
if(vis[tp])continue;
vis[tp]=true;num++;ans=max(d,ans);
for(int i=0;i<n;i++)if(!vis[i])que.push(P(dis[tp][i],i));
}
return ans;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=0;i<n;i++)for(int j=0;j<n;j++)scanf("%d",dis[i]+j);
int ans=prim();
printf("%d\n",ans);
}
return 0;
}

  

快速切题 poj 2485 Highways prim算法+堆 不完全优化 难度:0的更多相关文章

  1. poj 1258 Agri-Net 最小生成树 prim算法+heap不完全优化 难度:0

    Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 41230   Accepted: 16810 Descri ...

  2. POJ 2485 Highways (prim最小生成树)

    对于终于生成的最小生成树中最长边所连接的两点来说 不存在更短的边使得该两点以不论什么方式联通 对于本题来说 最小生成树中的最长边的边长就是使整个图联通的最长边的边长 由此可知仅仅要对给出城市所抽象出的 ...

  3. 快速切题CF 158B taxi 构造 && 82A double cola 数学观察 难度:0

    实在太冷了今天 taxi :错误原因1 忽略了 1 1 1 1 和 1 2 1 这种情况,直接认为最多两组一车了 2 语句顺序错 double cola: 忘了减去n的序号1,即n-- B. Taxi ...

  4. poj 2485 Highways

    题目连接 http://poj.org/problem?id=2485 Highways Description The island nation of Flatopia is perfectly ...

  5. poj 2485 Highways (最小生成树)

    链接:poj 2485 题意:输入n个城镇相互之间的距离,输出将n个城镇连通费用最小的方案中修的最长的路的长度 这个也是最小生成树的题,仅仅只是要求的不是最小价值,而是最小生成树中的最大权值.仅仅须要 ...

  6. POJ 2485 Highways( 最小生成树)

    题目链接 Description The islandnation of Flatopia is perfectly flat. Unfortunately, Flatopia has no publ ...

  7. Prim算法堆优化

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> ...

  8. hiho一下 第二十九周 最小生成树三·堆优化的Prim算法【14年寒假弄了好长时间没搞懂的prim优化:prim算法+堆优化 】

    题目1 : 最小生成树三·堆优化的Prim算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 回到两个星期之前,在成功的使用Kruscal算法解决了问题之后,小Ho产生 ...

  9. POJ 2485 Highways &amp;&amp; HDU1102(20/200)

    题目链接:Highways 没看题,看了输入输出.就有种似曾相识的感觉,果然和HDU1102 题相似度99%,可是也遇到一坑 cin输入居然TLE,cin的缓存不至于这么狠吧,题目非常水.矩阵已经告诉 ...

随机推荐

  1. springCloud--1

    电影微服务是服务消费者,用户微服务是服务提供者. Springcloud对eureka的支持很好,eureka本身是一个基于REST的服务, Eureka里面有一个注册表,Application Cl ...

  2. [caffe]caffe资料收集

    1.caffe主页,有各种tutorial. 2.Evan Shelhamer的tutorial,包括视频.

  3. Python笔记 #03# Help!

    源:DataCamp datacamp 的 DAILY PRACTICE  + 日常收集. Functions Built-in functions Help! Multiple arguments ...

  4. maven-surefire-plugin

    本文参考自:https://www.cnblogs.com/qyf404/p/5013694.html surefire是maven里执行测试用例(包括testNG,Junit,pojo)的插件,他能 ...

  5. JPA、JTA与JMS

    三者都属于Java企业级规范 JPA(java persistence API) JPA 通过JDK5.0的注解或XML来描述 对象-关系表的映射关系,并将运行期的实体对象持久化存储到数据库中. JT ...

  6. 20145302张薇《Java程序设计》实验四报告

    20145325张薇 实验四:Andoid开发基础 实验内容 使用 Android Studio 设计"Hello" 设计过程 首先创建项目 选择.xml中的`Design 选中W ...

  7. 20145322 《Java程序设计》第7周学习总结

    20145322何志威 <Java程序设计>第7周学习总结 教材学习内容总结 第十二章 如果使用JDK8的话,可以使用Lambda特性去除重复的信息. 在只有Lambda表达式的情况下,参 ...

  8. RN中有两种方式使用全局变量

    1.通过导入导出文件的方式 新建constants.js文件 const object = { website:'http://www.hao123.com', name:'好123', }; exp ...

  9. 爬虫之Requests: 让 HTTP 服务人类

    Requests: 让 HTTP 服务人类 虽然Python的标准库中 urllib2 模块已经包含了平常我们使用的大多数功能,但是它的 API 使用起来让人感觉不太好,而 Requests 自称 “ ...

  10. 关于Google play无法下载应用

    查看网络连接是否正常 查看账号信息 以上两种都没有问题,那可能是缓存的问题,去应用里面清楚缓存以后,重启手机. 如果还是不行,就在应用程序中 清除全部数据,重启手机,这样就可以了. ps:不管你行不行 ...