poj 2485 Highways 最小生成树
来源:互联网 发布:木村拓哉竹内结子知乎 编辑:程序博客网 时间:2024/06/10 16:15
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
题意:给出邻接矩阵,求组成最小生成树的最大边
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int f[66000],sum,maxx;struct p{ int u,v,w;} e[66000];bool comp(p x,p y){ return x.w<y.w;}int getf(int v){ if(f[v]==v) return v; else { f[v]=getf(f[v]); return f[v]; }}int merge(int v,int u){ int t1,t2; t1=getf(v); t2=getf(u); if(t1!=t2) { f[t1]=t2; return 1; } return 0;}int main(){ int T; scanf("%d",&T); while(T--) { sum=0; maxx=0; int n,m,t=0; scanf("%d",&n); for(int i=0; i<n; i++) for(int j=0; j<n; j++) { scanf("%d",&m); e[t].u=i; e[t].v=j; e[t++].w=m; } sort(e,e+t,comp); for(int i=0; i<n; i++) f[i]=i; for(int i=0; i<t; i++) { if(merge(e[i].u,e[i].v)) { sum++; if(maxx<e[i].w) maxx=e[i].w; } if(sum==n*n-1) break; } printf("%d\n",maxx); } return 0;}
0 0
- POJ 2485 Highways【最小生成树】
- poj 2485Highways(最小生成树 Kruskal)
- poj 2485 Highways (最小生成树)
- poj 2485 Highways 最小生成树
- poj 2485 Highways prim最小生成树
- POJ 2485 Highways (最小生成树)
- POJ 2485 Highways 最小生成树
- POJ 2485 Highways(最小生成树)
- poj 2485 Highways (最小生成树)
- POJ 2485 Highways 最小生成树
- poj 2485 Highways 最小生成树
- POJ 2485 Highways (最小生成树)
- POJ 2485 Highways (prim最小生成树)
- poj-2485-Highways-最小生成树
- POJ 2485 Highways(最小生成树)
- POJ 2485 Highways【最小生成树】
- POJ 2485 Highways 最小生成树
- POJ 2485 Highways 【最小生成树Prim】
- 【一篇无聊的影评】吐槽《从你的全世界路过》
- 剑指Offer面试题9(java版):斐波那契数列
- java中MD5加密工具类
- c# 用字符串分割字符串
- 求长度为n的数组中,加和最大的子数组
- poj 2485 Highways 最小生成树
- form表达提交checkbox问题
- Web前端 - webpack 打包图片
- rhel6下组建两台主机的HA集群
- 计算机视觉的错觉
- 【POJ 3264】【RMQ 或者线段树】Balanced Lineup 【查询区间内最大最小值的差】
- Python读写图像
- 《iOS移动开发从入门到精通》图书连载3:如何创建一个基本的iOS项目?
- 斐波那契数列理论