POJ 2485 Highways 最小生成树的最大边
来源:互联网 发布:长沙app软件开发公司 编辑:程序博客网 时间:2024/05/11 23:38
题目描述:
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
130 990 692990 0 179692 179 0
Sample Output
692
Hint
Huge input,scanf is recommended.
题目分析:
最小生成树的最大边,用Kruskal标记最大边而不是求生成树的权值之和就可以。
代码如下:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=510;struct Edge{ int u,v,w;} edge[maxn*maxn];int tol;int f[maxn];int n,T,ans;void addedge(int u,int v,int w){ edge[tol].u=u; edge[tol].v=v; edge[tol++].w=w;}bool cmp(Edge a,Edge b){ return a.w<b.w;}int find(int x){ if (f[x]==-1) return x; else return f[x]=find(f[x]);}int kruskal(){ int ret=0; int cnt=0; for(int i=0; i<tol; i++) { int u=edge[i].u; int v=edge[i].v; int w=edge[i].w; int t1=find(u); int t2=find(v); if (t1!=t2) { ret=max(w,ret); cnt++; f[t1]=t2; } if (cnt==tol-1) break; } return ret;}int main(){ scanf("%d",&T); while(T--) { scanf("%d",&n); memset(f,-1,sizeof(f)); tol=0; for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { int w; scanf("%d",&w); if (i>j) addedge(i,j,w); } }// for(int i=0; i<tol; i++)// {// printf("%d %d %d\n",edge[i].u,edge[i].v,edge[i].w);// } sort(edge,edge+tol,cmp); ans=kruskal(); printf("%d\n",ans); } return 0;}
- POJ 2485 Highways 最小生成树的最大边
- POJ 题目2485 Highways(最小生成树最大边)
- POJ 2485 Highways【最小生成树最大边,Prime算法】
- (kruscal12.3.5)POJ 2485 Highways(使用kruscal来计算最小生成树的最大边)
- poj 2485 Highways prim最小生成树 基础 球最小生成树中得最大边
- Highways 2485(最小生成树的最大边)
- poj-2485-Highways-最小生成树的最大权边
- POJ2485 Highways 最小生成树的最大边
- 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 最小生成树
- Theano Tutorial文档翻译(八) - 怎样标准化Theano的信息
- 深入理解linux内核手记
- HDU 3823 Prime Friend 线性筛选素数
- jQuery中$(this)和this的区别
- Gym 100851L Landscape Improved(二分+脑洞)
- POJ 2485 Highways 最小生成树的最大边
- Linux_transplanation_NOTE_1
- 创建应用的快捷方式
- 扫盲贴(一)——JDK8新特性:Lambda表达式入门
- JZOJ8.16(C组)最短路
- 多线程通讯之Handler实现
- 构造器的真正调用顺序
- hiho1079 : 离散化(线段树+区间离散化)
- Android Studio 优秀插件汇总