POJ2485 Kruskal
来源:互联网 发布:node 虚拟主机 编辑:程序博客网 时间:2024/06/05 16:24
Highways
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 26642 Accepted: 12190
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
题意:
问在一个高速公路,到达所有点的最小路径中最长的那条边长多少。
题解:
直接把poj1789里的Kruskal拿过来改一句话就好。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <map>#include <queue>#include <vector>#define f(i,a,b) for(i = a;i<=b;i++)#define fi(i,a,b) for(i = a;i>=b;i--)using namespace std;struct Edge{ int from,to,v;};int n,cnt;Edge edge[300000];int p[1000];int High[1000];bool cmp(Edge a,Edge b){ if(a.v<b.v) return true; else return false;}int Find(int k){ if(p[k] == k) return k; return p[k] = Find(p[k]);}int Kruskal(){ int i,j,Max = -0x3f3f3f3f; sort(edge,edge+cnt,cmp); f(i,1,n) {p[i] = i;High[i] = 1;} f(i,0,cnt-1){ int root1 = Find(edge[i].from); int root2 = Find(edge[i].to); if(root1 == root2) continue; if(edge[i].v>Max) Max = edge[i].v; if(High[root1]>High[root2]){ p[root2] = root1; High[root1] += High[root2]; }else{ p[root1] = root2; High[root2] += High[root1]; } } return Max;}int main(){ int N; scanf("%d",&N); while(N--) { scanf("%d",&n); int i,j; cnt = 0; f(i,1,n) f(j,1,n){ int k; scanf("%d",&k); if(i == j) continue; edge[cnt].from = i;edge[cnt].to = j;edge[cnt++].v = k; } printf("%d\n", Kruskal()); } return 0;}
- poj2485 Kruskal
- poj2485--Kruskal
- POJ2485 Kruskal
- poj2485 kruskal与prim
- POJ2485--Highways--Kruskal
- poj2485-最小生成树(prime+kruskal)
- poj2485 Highways 最小生成树,kruskal算法
- poj2485 Highways(kruskal求最小生成树)
- poj2485 最小生成树 kruskal算法
- poj2485
- poj2485
- poj2485
- poj2485
- poj2485
- poj2485
- poj2485
- poj2485
- POJ2485
- 团队中“螃蟹”太多,如何解决?
- web 中使用 谷歌 zxing.dll 识别二维码实例
- 关于spinlock和mutex的性能差异
- codeforces Sereja and Mugs
- oc 一些常用命令
- POJ2485 Kruskal
- 喝酒不骑马的Android自学日记(8)-D/TPicker实例&GridView实例
- codeforces Police Patrol
- 使用lrun
- mac 搭建 内网微信服务 ngrok
- mount.nfs: an incorrect mount option was specified
- Scala Everything is an object
- B-树小结汇总
- Regular Expression - Posix