openjudge Highways(最小生成树)
来源:互联网 发布:淘宝上的红牛是真的吗 编辑:程序博客网 时间:2024/06/05 12:48
Highways
- 查看
- 提交
- 统计
- 提问
- 总时间限制:
- 1000ms
- 内存限制:
- 65536kB
- 描述
- 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. - 输入
- 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. - 输出
- 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.
- 样例输入
130 990 692990 0 179692 179 0
- 样例输出
692
- 提示
- Huge input,scanf is recommended.
- tips:求最小生成树中最大的一个边
#include<iostream>#include<cstring>#include<vector>#include<algorithm>#include<cstdio>using namespace std;struct edge{int u,v,w;friend bool operator <(edge n1,edge n2){return n1.w<n2.w;}};vector<edge>edges; int n;int f[555];int find(int x){return f[x]<0?x:(f[x]=find(f[x]));}bool merge(int x,int y){int rx=find(x);int ry=find(y);if(rx!=ry){f[rx]+=f[ry];f[ry]=rx;return true;} return false;}int kruscal(){int ans=0;memset(f,-1,sizeof(f));sort(edges.begin(),edges.end());for(int i=0;i<edges.size();i++){edge e=edges[i];if(merge(e.u,e.v)){ans=max(ans,e.w);}}return ans; } int main(){int t;cin>>t;while(t--){edges.clear();cin>>n;for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){int x;scanf("%d",&x);if(j<i)edges.push_back(edge{i,j,x}); }}cout<<kruscal()<<endl;}return 0;}
阅读全文
0 0
- openjudge Highways(最小生成树)
- 最小生成树Highways
- 2485highways{最小生成树}
- Highways(最小生成树)
- poj2485 Highways(最小生成树)
- 1751 Highways【最小生成树】
- poj1751 Highways(最小生成树)
- POJ2485 Highways(最小生成树)
- HDUOJ Highways (最小生成树)
- POJ2485 Highways (最小生成树)
- POJ2485 Highways 最小生成树
- Highways(最小生成树 )
- Highways (最小生成树)
- Highways (最小生成树)
- Sicily 1090 Highways(Kruskal最小生成树)
- pku 2485 Highways 典型最小生成树
- POJ 2485 Highways【最小生成树】
- poj 2485Highways(最小生成树 Kruskal)
- MVC——ajax发送delete请求 报错404,405
- 开源静态代码分析工具列表---开源介绍
- tomcat服务器解析(二) --- Endpoint
- Tableau 10.3 简单Dashboard创建
- checkbox全选或反选
- openjudge Highways(最小生成树)
- php 文件及目录处理
- 数据结构学习笔记
- 腾讯 Web UI 解决方案 QMUI Web —— 探索与沉淀
- Android studio 上传到 GitHub
- jsp动态include 头部页面
- 使用ffmpeg合并视频文件的三种方法
- java环境变量配置好之后,cmd命令行javac为什么还是无效
- Eclipse Java注释模板设置详解