POJ 2485-Highways(并查集)
来源:互联网 发布:人工智能出现表明 编辑:程序博客网 时间:2024/06/03 21:39
H - Highways
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64uAppoint description:
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.
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.
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
130 990 692990 0 179692 179 0
Sample Output
692
Hint
Huge input,scanf is recommended.
思路:
这次不是求最小生成树,而是求最小生成树中的最大边,模板题。
AC代码:
#include<iostream>#include<algorithm>#include<cstring>#include<cstdio>#include<vector>#include<queue>#include<cmath>typedef long long ll;using namespace std;#define T 55000#define inf 0x3f3f3f3fint n,c;struct node{int u,v,val;bool operator<(const node& a)const{return val<a.val;}node(){}node(int _u,int _v,int _val):u(_u),v(_v),val(_val){}}v[T];int par[T];void Init(int n){for(int i=0;i<=n;++i){par[i] = i;}}int find(int x){int xx = x;while(par[xx]!=xx){xx = par[xx];}while(xx!=x){int tmp = par[x];par[x] = xx;x = tmp;}return xx;}int kruskal(){Init(c);int ma = 0,cnt=0;int tx,ty,i;for(i=0;i<c;++i){tx = find(v[i].u);ty = find(v[i].v);if(tx!=ty){cnt++;ma = max(ma,v[i].val);par[tx] = ty;}}return ma;}int main(){#ifdef zsc freopen("input.txt","r",stdin);#endif int N,i,j,k; scanf("%d",&N); while(N--) { scanf("%d",&n); c = 0; for(i=0;i<n;++i){ for(j=0;j<n;++j){ scanf("%d",&k); if(i<j){ v[c++] = node(i,j,k); } } } sort(v,v+c); printf("%d\n",kruskal()); } return 0;}
0 0
- POJ 2485-Highways(并查集)
- POJ 2485 Highways Kruskal+并查集 最小生成树
- poj 1751 -- Highways (并查集+最小生成树)
- 文章标题 POJ 2485 : Highways(最小生成树--kruskal+并查集)
- poj.2485并查集
- poj 2485 Highways(Kruskal!)
- poj 2485 Highways(Kruskal)
- POJ-2485-Highways(kruskal)
- poj 2485 Highways(MST)
- poj poj 2485 Highways
- Poj 并查集
- poj并查集
- POJ 并查集
- POJ 2485 : 最小生成树(kruskal+并查集)
- POJ 2588(解析几何+并查集)
- POJ 1611(并查集)
- poj 1611 (并查集)
- 种类并查集(POJ 1703)
- iOS ViewController之间传值方法三(利用NSNotification)
- 3. Spring Boot 1.2.5使用redis做数据缓存
- [Kali Linux 2.0] DSL配置问题
- Python读取配置文件
- 4. Spring Boot 1.2.5,Spring Data JPA多数据源支持
- POJ 2485-Highways(并查集)
- 读《第一行代码——安卓》2.3.3
- IO多路复用的几种实现机制的分析
- 编码
- 杭电ACM1003,1004,1005 java解答
- 【bzoj3123】 [Sdoi2013]森林
- 恶梦射手学习笔记
- 如何使用XML作为小型数据库
- 设计模式实例学习-模板方法