Highways poj 2485
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
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
Sample Input
1 3
0 990 692
990 0 179
692 179 0
Sample Output
692
题目大意:一个人当村长,想共享光纤,输入的是图,求最小生成树;这题和poj 1258 同一个代码,修改一行就可以
思路: 按无向图处理;
1.图转化成点到点的距离并储存起来
2.按权有小到大排序
3.Kruskal算法+并查集 构建最小生成树
#include<stdio.h>
#include<stdlib.h>
struct node
{
int i,j;
int weight;
} s[];
int cmp(const void*a,const void *b)
{
return (*(node*)a).weight-(*(node*)b).weight;
}
int v[];
int findl(int n)
{
return v[n]==n?n:findl(v[n]);
}
int main()
{
int m,n,i,j,k,a,sum;
scanf("%d",&m);
while(m--)
{
scanf("%d",&n);
n++,k=,sum=;
for(i=; i<; i++)
v[i]=i;
for(i=; i<n; i++)
{
for(j=; j<n; j++)
{
scanf("%d",&a);
if(j>i)//0及以下的数字直接忽略
{
s[k].weight=a;
s[k].i=i,s[k].j=j,k++;
}
}
}
qsort(s,k,sizeof(node),cmp);//按权值从小到大排序
for(i=; i<k; i++)
{
if(findl(s[i].i)!=findl(s[i].j))//简单的并查集
{
if(sum<s[i].weight)
sum=s[i].weight;
v[findl(s[i].i)]=s[i].j;
}
}
printf("%d\n",sum);
}
return ;
}
Highways poj 2485的更多相关文章
- Highways - poj 2485 (Prim 算法)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24383 Accepted: 11243 Description T ...
- Highways POJ 2485【Prim】
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...
- 【POJ 2485】 Highways
[POJ 2485] Highways 最小生成树模板 Prim #include using namespace std; int mp[501][501]; int dis[501]; bool ...
- poj 2485 Highways (最小生成树)
链接:poj 2485 题意:输入n个城镇相互之间的距离,输出将n个城镇连通费用最小的方案中修的最长的路的长度 这个也是最小生成树的题,仅仅只是要求的不是最小价值,而是最小生成树中的最大权值.仅仅须要 ...
- poj 2485 Highways
题目连接 http://poj.org/problem?id=2485 Highways Description The island nation of Flatopia is perfectly ...
- POJ 2485 Highways【最小生成树最大权——简单模板】
链接: http://poj.org/problem?id=2485 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- POJ 1258 Agri-Net|| POJ 2485 Highways MST
POJ 1258 Agri-Net http://poj.org/problem?id=1258 水题. 题目就是让你求MST,连矩阵都给你了. prim版 #include<cstdio> ...
- POJ 2485 Highways && HDU1102(20/200)
题目链接:Highways 没看题,看了输入输出.就有种似曾相识的感觉,果然和HDU1102 题相似度99%,可是也遇到一坑 cin输入居然TLE,cin的缓存不至于这么狠吧,题目非常水.矩阵已经告诉 ...
- poj 2485 Highways 最小生成树
点击打开链接 Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19004 Accepted: 8815 ...
随机推荐
- CentOS iSCSI客户端使用配置
配置步骤: 1.查看安装是否安装iSCSI驱动 rpm -qa|grep iscsi 2.查看yum安装源 yum list |grep iscsi 3.安装iscsi驱动 yum install i ...
- [转] SSH 密钥认证机制
使用 RSA 密钥对进行 SSH 登录验证 使用 RSA 密钥对验证 SSH 的优点是 1) 不用打密码 2) 比密码验证更安全:缺点是 1) 第一次配置的时候有点麻烦 2) 私钥需要小心保存.Any ...
- 一次优化web项目的经历记录(三)
一次优化web项目的经历记录 这段时间以来的总结与反思 前言:最近很长一段时间没有更新博客了,忙于一堆子项目的开发,严重拖慢了学习与思考的进程. 开水倒满了需要提早放下杯子,晚了就会烫手,这段时间以来 ...
- sqlite使用blob类型存储/访问 结构体
/* open fire host and slora report data database */ int open_report_db(void) { ; char sql[SQL_COMMAN ...
- oracle实现自动记录存储过程、自定义函数执行错误
CREATE OR REPLACE Package Pkg_Stm_Prgerrlog As --Purpose: 实现记录系统错误异常信息,便于问题跟踪 Procedure Sp_Stm_Prger ...
- oracle:变长数组varray,嵌套表,集合
创建变长数组类型 ) ); 这个变长数组最多可以容纳两个数据,数据的类型为 varchar2(50) 更改元素类型的大小或精度 可以更改变长数组类型和嵌套表类型 元素的大小. ALTER TYPE ...
- [转帖]音响及DarBee
红外与蓝牙的差别 1.距离 红外:对准.直接.1—2米,单对单 红外线可以用你的手机摄像头看到 蓝牙:10米左右,可加强信号,可以绕弯,可以不对准,可以不在同一间房间,链接最大数目可达7个,同时区分 ...
- treeview右键添加新节点
private void advTree1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Righ ...
- post和get的区别?
1. get是从服务器上获取数据,post是向服务器传送数据.2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到.post是通过H ...
- GUID是什么意思及Guid在sqlserver中的使用
GUID(全球唯一标识)是微软使用的一个术语,由一个特定的算法,给某一个实体,如Word文档,创建一个唯一的标识,GUID值就是这个唯一的标识码.GUID广泛应用于微软的产品中,用于识别接口.复制品. ...