题目连接

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的更多相关文章

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

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

  2. POJ 2485 Highways【最小生成树最大权——简单模板】

    链接: http://poj.org/problem?id=2485 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  3. POJ 1258 Agri-Net|| POJ 2485 Highways MST

    POJ 1258 Agri-Net http://poj.org/problem?id=1258 水题. 题目就是让你求MST,连矩阵都给你了. prim版 #include<cstdio> ...

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

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

  5. poj 2485 Highways 最小生成树

    点击打开链接 Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19004   Accepted: 8815 ...

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

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

  7. 快速切题 poj 2485 Highways prim算法+堆 不完全优化 难度:0

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

  8. POJ 2485 Highways(最小生成树+ 输出该最小生成树里的最长的边权)

                                                                                                         ...

  9. POJ 2485 Highways (求最小生成树中最大的边)

    Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...

随机推荐

  1. 全局变量&局部变量

    一.局部变量: 定义在函数内部的变量以及函数的形参成为局部变量 作用于:从定义那一行开始知道与其所在的代码块结束 生命周期:从程序运行到定义那一行开始分配存储空间到程序离开该变量所在的作用域 特点: ...

  2. 使用POI导入Excel异常Cannot get a text value from a numeric cell 解决

    POI操作Excel时因为Excel数据Cell有不同的类型,会出现Cannot get a text value from a numeric cell的异常错误. 异常原因:Excel数据Cell ...

  3. VS2010插件及快捷键设置

    几个常用的Visual Studio插件,番茄助手以及如下的插件,具体作用可用通过Google自行获取. 安装番茄助手后,可用在源文件和头文件中快速切换.但为了更方便使用,建议设置快捷键. vs201 ...

  4. Windbg CLR基础小测 《第六篇》

    首先写一段代码如下: namespace ConsoleApplication3 { class Program { static void Main(string[] args) { Console ...

  5. Vim中的正则表达式[转]

    来自:http://blog.csdn.net/endall/archive/2007/08/29/1764554.aspx Vim中的正则表达式功能很强大,如果能自由运用,则可以完成很多难以想象的操 ...

  6. hdu2444

    #include <stdio.h> #include <string.h> #define black 1 #define white -1 ]; ]; ][]; int n ...

  7. oracle 如何恢复误删的表记录数据

    --开启行移动功能 ALTER TABLE tablename ENABLE row movement ; --恢复表数据,时间为删除或修改的时间点 flashback table tablename ...

  8. 十八、ValueStack&ActionContext(非常重点:值栈)

    ValueStack&ActionContext(非常重点:值栈) 1.针对用户的每次动作访问,都会创建属于自己的ValueStack对象 2.ValueStack中的的常用方法:功能一定要知 ...

  9. Android开发教程 录音和播放

    首先要了解andriod开发中andriod多媒体框架包含了什么,它包含了获取和编码多种音频格式的支持,因此你几耍轻松把音频合并到你的应用中,若设备支持,使用MediaRecorder APIs便可以 ...

  10. Hive[1] 初识 及 安装

    本文前提是Hadoop & Java & mysql 数据库,已经安装配置好,并且 环境变量均已经配置到位   声明:本笔记参照 学习<Hive 编程指南>而来,如果有错误 ...