POJ2485 - Highways - Kruscal最小生成树
2016-02-11 15:38
381 查看
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
Sample Output
Hint
Huge input,scanf is recommended.
Kruscal算法加排序,并查集路径压缩优化
用vector只保留上三角矩阵或下三角矩阵
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 26829 | Accepted: 12275 |
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
Hint
Huge input,scanf is recommended.
Kruscal算法加排序,并查集路径压缩优化
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <limits> using namespace std; int T, N, mapp[510][510]; int p[250010], u[250010], v[250010], w[250010], r[250010]; bool 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 maxn = numeric_limits<int>::min(); for (int i = 0; i < N * N; i++) p[i] = i; for (int i = 0; i < N * N; i++) r[i] = i; for (int i = 0, cou = 0; i < N; i++) { for (int j = 0; j < N; j++) { u[cou] = i; v[cou] = j; w[cou] = mapp[i][j]; cou++; } } sort(r, r + N * N, cmp); //?? for (int i = 0; i < N * N; i++) { int e = r[i]; int x = find(u[e]), y = find(v[e]); if (x != y) { p[y] = x; maxn = max(maxn, w[e]); } } return maxn; } int main() { scanf("%d", &T); while (T--) { scanf("%d", &N); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { scanf("%d", &mapp[i][j]); } } printf("%d\n", Kruskal()); } return 0; }
用vector只保留上三角矩阵或下三角矩阵
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <limits> #include <vector> using namespace std; int T, N, p[510]; struct Edge { int u, v, w; Edge(int u, int v, int w) : u(u), v(v), w(w) {} }; vector<Edge> edges; int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); } bool cmp(const Edge a, const Edge b) { return a.w < b.w; } int Kruskal() { int maxn = numeric_limits<int>::min(); for (int i = 0; i < N; i++) p[i] = i; sort(edges.begin(), edges.end(), cmp); for (int i = 0; i < edges.size(); i++) { int x = find(edges[i].u), y = find(edges[i].v); if (x != y) { p[y] = x; maxn = max(maxn, edges.at(i).w); } } return maxn; } int main() { scanf("%d", &T); while (T--) { edges.clear(); //注意初始化 scanf("%d", &N); int w; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { scanf("%d", &w); if (i > j) { edges.push_back(Edge(i, j, w)); } } } printf("%d\n", Kruskal()); } return 0; }
相关文章推荐
- J2EE中web.xml配置文件详解
- 跟着猫哥学Golang 14 - error
- centos 初始配置 网卡自启动、SSH、开机启动命令行
- NYOJ832 合并游戏 状压DP+记忆化搜索
- 数据结构图文解析之:哈夫曼树与哈夫曼编码详解及C++模板实现
- 蓝桥杯-基础练习-回文数
- NodeJS Connect URL重写中间件组件
- share_ptr
- AbstractQueuedSynchronizer(AQS)
- Linux进程间通信 -- mmap函数的使用
- 国内行业盈利分析
- char *p="%s"; printf(p);的问题
- FTP 服务器(vsftpd)搭建鸟哥详细教材
- 51nod(1264)——线段相交
- 【VB】窗体透明一:窗体透明,控件不透明(穿过)
- 蓝桥杯-基础练习-特殊的数字
- ListView控件(一)--增,删,改,查
- 【资源链接合集】不定期更新~
- Hibernate【1】(安装 JBoss Tools,集成Hibernate)
- Alex-Word-Filter-MFC网络版(修正版)