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 ...
随机推荐
- Laxcus大数据管理系统2.0(2)- 第一章 基础概述 1.1 基于现状的一些思考
第一章 基础概述 1.1 基于现状的一些思考 在过去十几年里,随着互联网产业的普及和高速发展,各种格式的互联网数据也呈现爆炸性增长之势.与此同时,在数据应用的另一个重要领域:商业和科学计算,在各种新兴 ...
- Crystal Report 处理当前系统时间
Changed the print date format to HH/yy/MM/mm/SS/dd (For Example:2014/01/23 16:30:45= 161430014523) ...
- notepad++ tab键用空格缩进
从工作那天开始到现在,写python代码一直用notepad++来写,尝试几次都改不回eclipse.o(╯□╰)o python脚本中,如果用制表符缩进,经常会报错,必须改用空格缩进代替. 之前设置 ...
- math对象和date对象
math对象的函数方法,记住Math首字母要大写 console.log(Math.abs(-5)); //取绝对值 console.log(Math.ceil(1.1)); //向上取舍 conso ...
- php中若干模块的安装
1.php的mysql扩展pdo_mysql(在php源码所在目录) 1.1 进入 PHP 的软件包 pdo 扩展目录中 cd /usr/local/services/php-5.6.5/ext/ ...
- Eclipse 安装反编译插件jadclipse
下载jadClipse地址: 链接: http://pan.baidu.com/s/1kTN4TPd 提取码: 3fvd 将net.sf.jadclipse_3.3.0.jar拷贝到eclipse的 ...
- 华为OJ平台——求最大连续bit数
题目描述: 求一个byte数字对应的二进制数字中1的最大连续数,例如3的二进制为00000011,最大连续2个1 输入: 一个byte型的数字 输出: 对应的二进制数字中1的最大连续数 思路: ...
- powerdesigner逆向导出oracle数据库结构显示备注
最近接到命令,要将oracle数据库的结构导出为pdm文件供其他同事使用,逆向工程导出数据库结构比较方便,但是发现导出的数据库结构没有注释,这是很郁闷的事情: 查过网上很多资料都是sqlserver的 ...
- C#中messagebox用法
[函数] <整型> MessageBox(<字符串 Text, <字符串> Title, <整型> nType,MessageBoxIcon);[函数说明] ...
- C# 排列组合
排列组合的概念 排列:从n个不同元素中取出m(m≤n)个元素,按照一定的顺序排成一列,叫做从n个元素中取出m个元素的一个排列(Arrangement). 组合:从m个不同的元素中,任取n(n≤m)个元 ...