快速切题 poj 2485 Highways prim算法+堆 不完全优化 难度:0
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 23033 | Accepted: 10612 |
Description
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 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
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的更多相关文章
- poj 1258 Agri-Net 最小生成树 prim算法+heap不完全优化 难度:0
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41230 Accepted: 16810 Descri ...
- POJ 2485 Highways (prim最小生成树)
对于终于生成的最小生成树中最长边所连接的两点来说 不存在更短的边使得该两点以不论什么方式联通 对于本题来说 最小生成树中的最长边的边长就是使整个图联通的最长边的边长 由此可知仅仅要对给出城市所抽象出的 ...
- 快速切题CF 158B taxi 构造 && 82A double cola 数学观察 难度:0
实在太冷了今天 taxi :错误原因1 忽略了 1 1 1 1 和 1 2 1 这种情况,直接认为最多两组一车了 2 语句顺序错 double cola: 忘了减去n的序号1,即n-- B. Taxi ...
- poj 2485 Highways
题目连接 http://poj.org/problem?id=2485 Highways Description The island nation of Flatopia is perfectly ...
- poj 2485 Highways (最小生成树)
链接:poj 2485 题意:输入n个城镇相互之间的距离,输出将n个城镇连通费用最小的方案中修的最长的路的长度 这个也是最小生成树的题,仅仅只是要求的不是最小价值,而是最小生成树中的最大权值.仅仅须要 ...
- POJ 2485 Highways( 最小生成树)
题目链接 Description The islandnation of Flatopia is perfectly flat. Unfortunately, Flatopia has no publ ...
- Prim算法堆优化
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> ...
- hiho一下 第二十九周 最小生成树三·堆优化的Prim算法【14年寒假弄了好长时间没搞懂的prim优化:prim算法+堆优化 】
题目1 : 最小生成树三·堆优化的Prim算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 回到两个星期之前,在成功的使用Kruscal算法解决了问题之后,小Ho产生 ...
- POJ 2485 Highways && HDU1102(20/200)
题目链接:Highways 没看题,看了输入输出.就有种似曾相识的感觉,果然和HDU1102 题相似度99%,可是也遇到一坑 cin输入居然TLE,cin的缓存不至于这么狠吧,题目非常水.矩阵已经告诉 ...
随机推荐
- HDU - 4786 Fibonacci Tree (MST)
题意:给一张由白边和黑边构成的无向图,求是否存在一个生成树,使白边的数量为一个斐波那契数. 分析:白边权值为1,黑边权值为0.求出该图的最小生成树和最大生成树,若这两个值之间存在斐波那契数,则可以,若 ...
- CSS中定义a:link、a:visited、a:hover、a:active顺序
a :link.a:hover.a:visited这几个元素,定义CSS时候的顺序不同,也会直接导致链接显示的效果不同. eg:让未访问链接颜色为red,活动链接为yellow,已访问链接为green ...
- bash 获取时间段内的日志内容
需求,获取时段内的/var/log/messages文件内出现错误的消息,支持多行的消息,支持天,小时分钟,秒级的区间,可以修改监控的日志对象 #!/bin/bash if [ $# != 1 ] ; ...
- JS监听checkbox的选择获取取消事件代码案列
function OncheckBox(index){ if ($(index).attr("checked") == "checked") { alert($ ...
- iOS 截屏,openGL ES 截图,以及像素颜色判断
代码整理了2种截图,类似.(没苹果自带那种截图彻底) 方法一: +(UIImage *)fullScreenshots{ UIWindow *screenWindow = [[UIApplicatio ...
- Dbus组成和原理
DBUS是实质上一个适用于桌面应用的进程间的通讯机制,即所谓的IPC机制.适合在同一台机器,不适合于INTERNET的IPC机制.DBUS不是一个为所有可能的应用的通用的IPC机制,不支持其他IPC机 ...
- Django---ModelForm详解
示例: from django.db import models from django.forms import ModelForm TITLE_CHOICES = ( ('MR', 'Mr.'), ...
- 完成了C++作业,本博客现在开始全面记录acm学习历程,真正的acm之路,现在开始
以下以目前遇到题目开始记录,按发布时间排序 ACM之递推递归 ACM之数学题 拓扑排序 ACM之最短路径做题笔记与记录 STL学习笔记不(定期更新) 八皇后问题解题报告
- COJS:1829. [Tyvj 1728]普通平衡树
★★★ 输入文件:phs.in 输出文件:phs.out 简单对比 时间限制:1 s 内存限制:128 MB [题目描述] 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需 ...
- 浅析ProcessBuilder
概述 ProcessBuilder类是J2SE 1.5在java.lang中新添加的一个新类,此类用于创建操作系统进程,它提供一种启动和管理进程(也就是应用程序)的方法.在J2SE 1.5之前,都是由 ...