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 ...
随机推荐
- 利用KeyVault来加强存储Azure Storage访问密钥管理
很多时候管理Azure的存储账号我们都需要通过下面的界面管理访问密钥,大部分情况下通过密钥的轮替使用更新就可以做到安全管理了. 但是很多时候存储账号的Key就会不小心分发到开人员.测试人员.和管理员手 ...
- 【练习】数据文件的更改:改名或改路径 users01.dbf-->users01_bak.dbf
方法一:1.将数据文件的状态offline SQL> select file_name,tablespace_name from dba_data_files where file_name l ...
- php中未定义变量的使用
在php中,若变量$test没有定义,则: empty($test)返回true: !empty($test)返回false: 具体可参考http://www.jb51.net/article/246 ...
- yii中第三方库
yii中存在一些路径别名:ext:表示包含了所有第三方扩展的目录 参考:http://www.yiiframework.com/doc/guide/1.1/zh_cn/basics.namespac ...
- mysql显示乱码问题
在select * from table:时往往会出现上图所示乱码现象 此时,输入status,会发现: 此时只要SET NAMES utf8即可解决该问题.此时,再次输入status: 总结:S ...
- java学习之(内部类)
大部分时候,类被定义成一个独立的程序单元,在某些情况下,也会把一个类放在另一个类的内部定义,这个定义在其他类内部的类就被称为内部类,内部类有如下特点: 1.内部类提供了更好的封闭,可以把内部类隐 ...
- haproxy配置文件简单管理
版本:python3功能:对haproxy配置文件进行简单的查询.添加以及删除功能操作流程:1.根据提示选择相应的选项2.进入所选项后,根据提示写入相应的参数3.查询功能会返回查询结果,添加.删除以及 ...
- .NET Reflector反编译的方法
首先启动.NET Reflector,然后添加进入dll或exe.然后选择Export Source Code...,将反编译后的代码文件,生成到指定目录. 到这一步骤时,稍等一会,就能够在指定目录就 ...
- 用verilog模拟DDS产生正弦波信号
前言: DDS:直接数字频率合成,正弦波0-2pi周期内,相位到幅度是一一对应的(这里我们使用放大后的整数幅度). 主要思路: 个人理解,FPGA不擅长直接做数字信号计算,那样太占用片上逻辑资源,所以 ...
- 第九周java学习总结
20145306<java程序设计>第九周学习总结 教材学习内容总结 第十六章 一.JDBC入门 1.JDBC简介 JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数据 ...