poj 2485 Highways
题目连接
http://poj.org/problem?id=2485
Highways
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
最下生成树。。
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using std::map;
using std::max;
using std::find;
using std::pair;
using std::vector;
using std::multimap;
using std::priority_queue;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 510;
struct P {
int w, v;
P(int i = 0, int j = 0) :w(i), v(j) {}
inline bool operator<(const P &x) const {
return w > x.w;
}
};
struct Prim {
struct edge { int to, w, next; }G[(N * N) << 1];
int tot, vis[N], head[N], mincost[N];
inline void init() {
tot = 0, cls(vis, false), cls(head, -1), cls(mincost, 0x3f);
}
inline void add_edge(int u, int v, int w) {
G[tot] = (edge){ v, w, head[u] }; head[u] = tot++;
G[tot] = (edge){ u, w, head[v] }; head[v] = tot++;
}
inline void built(int V) {
int w;
rep(i, V) {
rep(j, V) {
scanf("%d", &w);
if(i == j) continue;
add_edge(i + 1, j + 1, w);
}
}
}
inline int prim(int s) {
int ans = -1;
priority_queue<P> q;
q.push(P(0, s));
for(int i = head[s]; ~i; i = G[i].next) {
mincost[G[i].to] = G[i].w;
q.push(P(G[i].w, G[i].to));
}
vis[s] = true;
while(!q.empty()) {
P t = q.top(); q.pop();
int u = t.v;
if(vis[u]) continue;
vis[u] = true;
ans = max(ans, t.w);
for(int i = head[u]; ~i; i = G[i].next) {
int &d = mincost[G[i].to];
if(!vis[G[i].to] && d > G[i].w) {
d = G[i].w;
q.push(P(G[i].w, G[i].to));
}
}
}
return ans;
}
inline void solve(int n) {
init();
built(n);
printf("%d\n", prim(1));
}
}go;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int t, n;
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
go.solve(n);
}
return 0;
}
poj 2485 Highways的更多相关文章
- poj 2485 Highways (最小生成树)
链接:poj 2485 题意:输入n个城镇相互之间的距离,输出将n个城镇连通费用最小的方案中修的最长的路的长度 这个也是最小生成树的题,仅仅只是要求的不是最小价值,而是最小生成树中的最大权值.仅仅须要 ...
- POJ 2485 Highways【最小生成树最大权——简单模板】
链接: http://poj.org/problem?id=2485 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- POJ 1258 Agri-Net|| POJ 2485 Highways MST
POJ 1258 Agri-Net http://poj.org/problem?id=1258 水题. 题目就是让你求MST,连矩阵都给你了. prim版 #include<cstdio> ...
- POJ 2485 Highways && HDU1102(20/200)
题目链接:Highways 没看题,看了输入输出.就有种似曾相识的感觉,果然和HDU1102 题相似度99%,可是也遇到一坑 cin输入居然TLE,cin的缓存不至于这么狠吧,题目非常水.矩阵已经告诉 ...
- poj 2485 Highways 最小生成树
点击打开链接 Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19004 Accepted: 8815 ...
- POJ 2485 Highways( 最小生成树)
题目链接 Description The islandnation of Flatopia is perfectly flat. Unfortunately, Flatopia has no publ ...
- 快速切题 poj 2485 Highways prim算法+堆 不完全优化 难度:0
Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23033 Accepted: 10612 Descri ...
- POJ 2485 Highways(最小生成树+ 输出该最小生成树里的最长的边权)
...
- POJ 2485 Highways (求最小生成树中最大的边)
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...
随机推荐
- 慕课网-安卓工程师初养成-2-1 Java中的关键字
来源:http://www.imooc.com/code/1176 Java 中常用关键字: 问:这么多,记不住啊......-_-|| 答:现在不需要你记住所有,混个眼熟即可,在学习的过程中,你会逐 ...
- 对付ring0 inline hook
对付ring0 inline hook的基本思路是这样的,自己写一个替换的内核函数,以NtOpenProcess为例,就是MyNtOpenProcess.然后修改SSDT表,让系统服务进入自己的函数M ...
- OS版本调研
1引言 1.1 编写目的 本文的主要目的是通过对当前项目中使用的各种版本的操作系统进行比较,分析各自特性和稳定程度,最终推荐合适的版本作为当前的标准系统. 1.2 背景 当前,部门负责管理维护的现网使 ...
- Mac 平台下功能强大的Shimo软件使用指南
年初自从换了MAC工作站后,彻底享受了Apple产品给我们带来的完美体验,可能是刚转过来不适应,在访问网络设备时觉得远程连接不方便,例如ssh,vpn登陆都不是很方便,后来又安装了openvpnfor ...
- MongoDB 2: 安装和使用
导读:上篇博客中简单介绍了MongoDB,本篇文章主要是介绍Mongo的安装和使用(环境为win8).(PS:这是一篇没什么技术含量的文章,仅是个人的笔记式文档)下一篇博客,将介绍Mongo使用过程中 ...
- 使用ffmpeg快速生成视频截图
1 ffmpeg -i input.mkv -ss 00:10:00 -f image2 output.jpg 但是这个命令会花费相当长的时间. 对一个清晰的或者较大的视频文件进行操作, 会花费半分钟 ...
- rem是如何实现自适应中的?
使用rem 然后根据媒体查询实现自适应.跟使用JS来自适应也是同个道理,不过是js更精确一点.使用媒体查询: html { font-size: 62.5% } @media only screen ...
- TortoiseSVN文件夹图标不显示的解决方法
是否遇到过TortoiseSVN安装好后,发现文件夹的图标还是Windows默认的图标? 下面通过简单几步解决图标不显示的问题. 1/6 "Win + R"打开运行框,输入&quo ...
- 按照 where id in ()排序
select * from ibs6_terminal_adv_inf where id in (16,14,15) order by find_in_set(id,'16,14,15')
- Cent OS yum 安装 Adobe flash player
桌面打开浏览器访问:http://get.adobe.com/cn/flashplayer/.网页会判断操作系统和浏览器并下载 Flash Player(支持Firefox浏览器). 或者直接下载: ...