POJ2485 - Highways - Kruscal最小生成树
来源:互联网 发布:男尊女卑 日本 知乎 编辑:程序博客网 时间:2024/05/22 13:28
Highways
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 26829 Accepted: 12275
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.
Kruskal算法加排序,并查集路径压缩优化
#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;}
0 0
- POJ2485 - Highways - Kruscal最小生成树
- poj2485 Highways 最小生成树 水题 kruscal
- poj2485 Highways(最小生成树)
- POJ2485 Highways(最小生成树)
- POJ2485 Highways (最小生成树)
- POJ2485 Highways 最小生成树
- POJ2485,Highways,最小生成树继续Prim
- poj2485 Highways 最小生成树,kruskal算法
- POJ2485——Highways(最小生成树)
- poj2485 Highways(kruskal求最小生成树)
- POJ2485-Highways(最小生成树)
- poj2485 Highways(最小生成树水题)
- ACM-最小生成树之Highways——poj2485
- POJ2485 Highways 最小生成树的最大边
- poj2485 Highways 最小生成树求最大边
- poj2485——Highways(最小生成树+prim)
- poj 2485 Highways(kruscal or prim最小生成树)
- 最小生成树 poj2485
- char *p="%s"; printf(p);的问题
- Educational Codeforces Round 7--D. Optimal Number Permutation
- Linux进程间通信 -- mmap函数的使用
- 蓝桥杯-基础练习-回文数
- NodeJS Connect URL重写中间件组件
- POJ2485 - Highways - Kruscal最小生成树
- share_ptr
- 【VB】窗体透明二:窗体逐渐变透明(包括控件)
- Symmetry UVa 1595(水题)
- ActiveMQ初体验
- noj 1046 第K回文数
- Objective-C 和 C的对比
- LeetCode 100. Same Tree
- Nginx工作原理和优化、漏洞