ACM: 图论题 poj2485 (crazy now~!…
2016-05-19 23:16
393 查看
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
题意: 太裸露了这题 , 城市之间修建道路 , 要求联通全部点 , 最短路程就完成这件事
,
统计最大的一条路.
解题思路:
1. 输入直接是邻接矩阵 , 用kruskal算法 +
并查集.
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAX 250005
int n , m;
int u[MAX] , v[MAX] , w[MAX];
int p[MAX] , r[MAX];
int read_graph()
{
scanf("%d",&n);
int i ,
j;
int
ww;
m = 0;
for(i = 1; i
<= n; ++i)
{
for(j = 1; j <= n; ++j)
{
scanf("%d",&ww);
if(i == j)
continue;
else
{
u[++m] = i;
v[m] = j;
w[m] = ww;
}
}
}
}
int cmp(const int i,const int j)
{
return w[i]
< w[j];
}
int find(int x)
{
return p[x]
== x ? x : p[x] = find(p[x]);
}
int kruskal()
{
int i;
int ans =
0;
for(i = 1; i
<= n; ++i)
p[i] = i;
for(i = 1; i
<= m; ++i) r[i] = i;
sort(r,r+m,cmp);
for(i = 1; i
<= m; ++i)
{
int e = r[i];
int x = find(u[e]);
int y = find(v[e]);
if(x != y)
{
if(ans < w[e])
ans = w[e];
p[x] = y;
}
}
相关文章推荐
- ACM: 图论题 BFS + STL + priority…
- ACM: 方向dfs + bfs求最短路 poj 3…
- ACM: 一题组合数学题 poj1850 (头…
- ACM: 一题数论题(扩展欧几里得定理…
- ACM: 一题组合数学题 poj1942 (细…
- ACM: 一题二分搜索题 poj3273 (…
- 网络多线程02
- ACM: 一题二分搜索题. poj3258 & 4000 nbsp;…
- 【LintCode】查找斐波那契数列中的N个数
- ACM: 又是一题二分法题 poj 3122…
- ACM: poj 第50题 一题递推数论题 …
- ACM: 图论基础题 poj2387 裸露的di…
- ACM: 图论题poj 1860 (没心情复习…
- ACM: 图论题 poj3259 bellman_ford…
- ACM: 一道简单数论题 (重要的是建…
- ACM : 一道基础数学题目POJ 1423 …
- ACM : 数论题 poj1061 (扩展欧几…
- ACM: 一道水题 poj1723
- ACM: 一道数论题 poj1730 (做了好…
- ACM: 一题大数求mod poj2635 (…