题目连接

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. Oracle 事务

    begin begin savepoint p1; DELETE FROM sys_re_xxx; //红色部分替换为需要一起执行的SQL即可 DELETE FROM SYS_xxxx; ...... ...

  2. js实现自动登陆的按钮

    自动按钮,只要实现当移入是提示用户不要在公共地方使用自动登陆 主要用onmouseover函数,本来提示div隐藏,当移入时div显示. <style type="text/css&q ...

  3. hadoop2.5.1搭建(二)

    第一篇主要是整体的步骤,其实中间遇到很多问题,第二篇将遇到的问题全部列举下来: 1.1包不能加载警告 WARN util.NativeCodeLoader: Unable to load native ...

  4. activity的android:name 设置问题

    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com. ...

  5. 轻松找回Win7桌面“消失”的IE9图标

    打开注册表编辑器(Win+R打开运行窗口,运行regedit命令),依次展开到 [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion ...

  6. URL重写以后发布到IIS找不到页面

    1.读取必须勾选,否则无法加载资源文件(img,css等) c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll

  7. Java学生管理系统项目案例

    这是一个不错的Java学生管理系统项目案例,希望能够帮到大家的学习吧. 分代码如下 package com.student.util; import java.sql.Connection; impo ...

  8. oracle11g导入到10g

    oracle11g导入到10g http://www.doc88.com/p-0827386468478.html

  9. JavaScript模块化---AMD规范

    JavaSript模块化 在了解AMD,CMD规范前,还是需要先来简单地了解下什么是模块化,模块化开发?     模块化是指在解决某一个复杂问题或者一系列的杂糅问题时,依照一种分类的思维把问 题进行系 ...

  10. POJ C++程序设计 编程作业—类和对象 编程题#1

    编程题#1 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面程序输出的结 ...