poj2485
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 27912 | Accepted: 12734 |
Description
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
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
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
Source
题意:Flatopia岛要修路,这个岛上有n个城市,要求修完路后,各城市之间可以相互到达,且修的总
路程最短.
求所修路中的最长的路段
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define N 510
struct node{
int x,y,v;
}e[N*N];
int t,fa[N];
bool cmp(const node &a,const node &b){
return a.v<b.v;
}
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
int main(){
scanf("%d",&t);
while(t--){
int n,cnt=;
scanf("%d",&n);
for(int i=;i<=n;i++) fa[i]=i;
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
int x;
scanf("%d",&x);
if(x){
e[++cnt].x=i;e[cnt].y=j;e[cnt].v=x;
}
}
}
int k=,res=;
sort(e+,e+cnt+,cmp);
for(int i=;i<=cnt;i++){
int fx=find(e[i].x),fy=find(e[i].y);
if(fx!=fy){
fa[fy]=fx;
k++;
}
if(k==n-){
res=e[i].v;//因为排完序了,所以最后一个就是最大的
break;
}
}
printf("%d\n",res);
for(int i=;i<=n;i++) fa[i]=;//养成清零好习惯
for(int i=;i<=n;i++) e[i]=e[];
}
return ;
}
poj2485的更多相关文章
- poj2485&&poj2395 kruskal
题意:最小生成树的最大边最小,sort从小到大即可 poj2485 #include<stdio.h> #include<string.h> #include<algor ...
- POJ2485 最小生成树
问题:POJ2485 本题求解生成树最大边的最小值 分析: 首先证明生成树最大边的最小值即最小生成树的最大边. 假设:生成树最大边的最小值比最小生成树的最大边更小. 不妨设C为G的一个最小生成树,e是 ...
- POJ-2485 Highways---最小生成树中最大边
题目链接: https://vjudge.net/problem/POJ-2485 题目大意: 求最小生成树中的最大边 思路: 是稠密图,用prim更好,但是规模不大,kruskal也可以过 #inc ...
- 最小生成树Prim poj1258 poj2485 poj1789
poj:1258 Agri-Net Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u ...
- poj2485 kruskal与prim
Kruskal: #include<iostream> #include<cstdio> #include<algorithm> using namespace s ...
- poj2485 Highways
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...
- POJ2485 Highways(最小生成树)
题目链接. 分析: 比POJ2253要简单些. AC代码: #include <iostream> #include <cstdio> #include <cstring ...
- POJ2485——Highways
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...
- poj2485 highwaysC语言编写
/*HighwaysTime Limit: 1000MSMemory Limit: 65536KTotal Submissions: 33595Accepted: 15194DescriptionTh ...
- POJ2485:Highways(模板题)
http://poj.org/problem?id=2485 Description The island nation of Flatopia is perfectly flat. Unfortun ...
随机推荐
- hive bucket
转载:https://www.cnblogs.com/end/archive/2013/01/09/2852413.html hive中table可以拆分成partition,table和partit ...
- 什么是ICE (Internet Communications Engine)
http://user.qzone.qq.com/77811970 直接在google上搜索ICE,出来的结果并不多,所以很多人就认为ICE是个神秘的东西,其实,国内已经有很多大型应用在使用ICE了. ...
- 将 xml 文件 转为 DataTable
private static DataTable CreateDataTable(string table) { DataSet dataSet = new DataSet(); string dat ...
- 向git库提交代码出现”There are no staged files"怎么办?
1.选择菜单“Window”->"Preference" 2.左边树菜单选择“Team”->"Git"->"Committing&q ...
- php闭包使用例子
一.依据闭包实现一个容器 class Di { private $factory; public function set($id, $value) { $this->factory[$id] ...
- Wamp访问缓慢、本地主机访问缓慢解决方案
Wamp访问缓慢.本地主机访问缓慢解决方案 Wamp访问速度缓慢可能的原因: 1.一般原因: Wamp的日志文件太大.处理办法,将位于wamp/logs/下的日志文件清空. ...
- wps文档忘记保存关闭了怎么恢复
wps文档忘记保存关闭了怎么恢复 点击程序左上角的''WPS文字/表格/演示''选择备份管理,根据需要尝试右侧下面的"查看其他备份"功能就能找了. 点击"开始-运行&qu ...
- ubuntu 备份清理tomcat日志,并且删除七天之前的日志
创建文件clear_tomcat_log.sh #!/bin/bash logs_paths[0]="app_tomcat7_8080"; logs_paths[1]=" ...
- js中数组的合并和对象的合并
1 数组合并 1.1 concat 方法 var a = [1,2,3], b = [4,5,6]; var c = a.concat(b); console.log(c);// 1,2,3,4,5, ...
- JavaScript对象this指向(普通键this指向 非指向函数的键)
1.结论 JavaScript对象普通键(非指向函数的键)this指向是window. 2.示例 <!DOCTYPE html> <html lang="zh"& ...