文章标题 POJ 2485 : Highways(最小生成树--kruskal+并查集)
来源:互联网 发布:tcp监听端口被堵塞 编辑:程序博客网 时间:2024/06/06 03:17
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
1
3
0 990 692
990 0 179
692 179 0
Sample Output
692
Hint
Huge input,scanf is recommended.
题意:要给n个城镇建铁路,使得花费最小,用一个矩阵给出每两个城镇建一条铁路需要的花费。要我们求出将这些城镇连起来中最小花费,将这写铁路中最长的一条输出来
分析:花费通过矩阵已经给我们了,所以只需要将这些路排一下序,直接用kruskal得到最小生成树,最后一条边就是所求的最长的铁路
代码:
#include<iostream>#include<string>#include<cstdio>#include<cstring>#include<vector>#include<math.h>#include<map>#include<queue> #include<algorithm>using namespace std;const int inf = 0x3f3f3f3f;int n;int mp[505][505];int fa[505];//并查集int find(int x){ return fa[x]==x?x:fa[x]=find(fa[x]);}struct node { int x,y;//铁路连的两个城镇 int dis;//距离};node a[250005];bool cmp(node a,node b){ return a.dis<b.dis;}int main (){ int t; scanf ("%d",&t); while (t--){ scanf ("%d",&n); for (int i=1;i<=n;i++){ fa[i]=i; for (int j=1;j<=n;j++){ scanf ("%d",&mp[i][j]); } } int cnt=0; for (int i=1;i<=n-1;i++){ for (int j=i+1;j<=n;j++){//找出所有城镇亮亮相连的边保存起来 a[cnt].x=i; a[cnt].y=j; a[cnt++].dis=mp[i][j]; } } sort (a,a+cnt,cmp);//排序 int connect=0; int ans=0; for(int i=0;i<cnt;i++){ int x=a[i].x; int y=a[i].y; int fx=find(x),fy=find(y); if (fx!=fy){ connect++; fa[fx]=fy; ans=max(ans,a[i].dis); } if (connect==n-1){// 单找到n-1条边时结束 break; } } printf ("%d\n",ans); } return 0;}
- 文章标题 POJ 2485 : Highways(最小生成树--kruskal+并查集)
- POJ 2485 Highways Kruskal+并查集 最小生成树
- 文章标题 POJ 2349:Arctic Network ( 最小生成树Kruskal算法+并查集)
- POJ 2485 : 最小生成树(kruskal+并查集)
- poj 1751 -- Highways (并查集+最小生成树)
- 文章标题 HDU 3371 : Connect the Cities(最小生成树--Kruskal+并查集)
- 文章标题 HDU 4786 : Fibonacci Tree(最小生成树--kruskal+并查集)
- poj 2485Highways(最小生成树 Kruskal)
- POJ 2485 Highways 最小生成树 (Kruskal)
- POJ:2485 Highways(最小生成树kruskal算法)
- POJ 2485 Highways (最小生成树 Kruskal算法)
- 【POJ】2485--Highways(最小生成树 --Kruskal)
- POJ 1861 Networks 最小生成树 Kruskal+并查集
- POJ 1751 Highways (Kruskal 最小生成树)
- POJ 2485 Highways 最小生成树 Kruskal && Prim
- POJ:1751 Highways(最小生成树kruskal算法)
- Poj --1751 highways (最小生成树,kruskal算法)
- 最小生成树+并查集+ kruskal
- Linux下C开发工具介绍
- java基础- 异常的层次结构
- 二进制
- JZOJ 4777. 【NOIP2016提高A组模拟9.14】灌水
- 携程火车票业务在 React Native 实践中踩过的坑
- 文章标题 POJ 2485 : Highways(最小生成树--kruskal+并查集)
- 使用matlab和c++读取文本数据混合文档
- 收集一些好用的网站
- 我的第一篇博客
- 好用且实用的链式前向星(好像就是邻接表)
- 控制一个高消耗CPU的线程数控制方案
- 数据地图工具
- 欢迎使用CSDN-markdown编辑器
- Issues of Zeppelin