poj 2349(最小生成树应用)
题目链接:http://poj.org/problem?id=2349
思路:由于有S个专门的通道,我们可以先求一次最小生成树,然后对于最小生成树上的边从大到小排序,前S-1条边用S-1个卫星通道连接,那么第S大条边就是我们要找的最小的D了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define MAXN 555
#define inf 1LL<<60 struct Node{
int x,y;
}node[MAXN];
double map[MAXN][MAXN];
double lowcost[MAXN];
double dist[MAXN];
bool mark[MAXN];
int n,m; double Cal(int i,int j)
{
double d1=1.0*(node[i].x-node[j].x)*(node[i].x-node[j].x);
double d2=1.0*(node[i].y-node[j].y)*(node[i].y-node[j].y);
return sqrt(d1+d2);
} int cmp(const double &p,const double &q)
{
return p>q;
} double Prim(int u0)
{
int cnt=;
memset(mark,false,sizeof(mark));
for(int i=;i<=m;i++){
lowcost[i]=map[u0][i];
}
lowcost[u0]=;
mark[u0]=true;
for(int i=;i<m;i++){
double MIN=inf;
int v=u0;
for(int j=;j<=m;j++){
if(!mark[j]&&lowcost[j]<MIN){
MIN=lowcost[j],v=j;
}
}
if(MIN==inf)break;
mark[v]=true;
dist[cnt++]=lowcost[v];
for(int k=;k<=m;k++){
if(!mark[k]&&map[v][k]<lowcost[k]){
lowcost[k]=map[v][k];
}
}
}
sort(dist,dist+cnt,cmp);
//for(int i=0;i<cnt;i++)printf("%.2lf\n",dist[i]);
return dist[n-];
} int main()
{
int _case,a,b;
scanf("%d",&_case);
while(_case--){
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
for(int j=;j<=m;j++)
map[i][j]=map[j][i]=(i==j?:inf);
for(int i=;i<=m;i++){
scanf("%d%d",&node[i].x,&node[i].y);
for(int j=;j<i;j++){
map[i][j]=map[j][i]=Cal(i,j);
}
}
printf("%.2lf\n",Prim());
}
return ;
}
poj 2349(最小生成树应用)的更多相关文章
- Arctic Network POJ 2349 (最小生成树思想)
Description The Department of National Defence (DND) wishes to connect several northern outposts by ...
- POJ 2349 Arctic Network (最小生成树)
Arctic Network Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Subm ...
- Poj(2349),最小生成树的变形
题目链接:http://poj.org/problem?id=2349 Arctic Network Time Limit: 2000MS Memory Limit: 65536K Total S ...
- POJ 2349 Arctic Network(最小生成树+求第k大边)
题目链接:http://poj.org/problem?id=2349 题目大意:有n个前哨,和s个卫星通讯装置,任何两个装了卫星通讯装置的前哨都可以通过卫星进行通信,而不管他们的位置. 否则,只有两 ...
- poj 2349 Arctic Network(最小生成树的第k大边证明)
题目链接: http://poj.org/problem?id=2349 题目大意: 有n个警戒部队,现在要把这n个警戒部队编入一个通信网络, 有两种方式链接警戒部队:1,用卫星信道可以链接无穷远的部 ...
- POJ 2349 Arctic Network(最小生成树中第s大的边)
题目链接:http://poj.org/problem?id=2349 Description The Department of National Defence (DND) wishes to c ...
- (最小生成树) Arctic Network -- POJ --2349
链接: http://poj.org/problem?id=2349 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1371 ...
- POJ 2349 Arctic Network(最小生成树,第k大边权,基础)
题目 /*********题意解说——来自discuss——by sixshine**************/ 有卫星电台的城市之间可以任意联络.没有卫星电台的城市只能和距离小于等于D的城市联络.题 ...
- POJ 2349 Arctic Network (最小生成树)
Arctic Network 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/F Description The Departme ...
随机推荐
- JavaScript高级程序设计之Date类型
ECMAScript 中的 Date 类型是在早期 Java 的 java.util.Date 类基础上构建的. Date 类型使用自 UTC (国际协调时间)1970年1月1日午夜(零时)开始经过的 ...
- WIN服务器出现 php-cgi.exe - FastCGI 进程意外退出
既然是不能解析PHP,那就直接运行一下PHP,看会报什么错,再对症下药,于是,在命令提示符窗口进入php安装的根目录,然后运行php -v的命令,这时窗口弹出计算机丢失msvcr110.dll的错误, ...
- Windows Phone中Wallet钱包的使用
前言 Windows Phone 8中加入了钱包Wallet这个功能,这个功能非常的有意思,开发者可以通过Wallet提供的API创建获取Wallet中的商品.统一管理用户的收集优惠券.信用卡.成员资 ...
- OpenStack: 安装准备
>安装准备1. 安装MySQL# apt-get install python-mysqldb mysql-server将/etc/mysql/my.cnf修改bind-address为&quo ...
- Karaf 依赖equinox and felix,karaf 本Apache的很多项目作为基础框架
6月17日是Apache Karaf作为Apache顶级项目.Karaf是个运行时包,包含了一个OSGi框架(Equinox或Felix).一个命令shell(Felix Gogo)及默认情况下内置的 ...
- ios9网络请求https适配
发现问题:今天升级Xcode 7.0 bata发现网络访问失败.输出错误信息: The resource could not be loaded because the App Transport S ...
- linux CentOS 系统下如何将php和mysql命令加入到环境变量中
在Linux CentOS系统上安装完php和MySQL后,为了使用方便,需要将php和mysql命令加到系统命令中,如果在没有添加到环境变量之前,执行“php -v”命令查看当前php版本信息时时, ...
- html <input>标签类型属性type(file、text、radio、hidden等)详细介绍
html <input>标签类型属性type(file.text.radio.hidden等)详细介绍 转载请注明:文章转载自:[169IT-最新最全的IT资讯] html <inp ...
- 【工作总结】LLDB调试技巧 - 篇一
备忘命令 : 命令“l”可以查看程序当前运行的位置 (lldb) l } - (void)rightBarButtonAction { 命令“bt”也能查看程序运行的调用栈 (lldb) bt * t ...
- 新安装Ubuntu加载时提示“为/检查磁盘时发生严重错误”的解决方法
本文部分内容转载自: http://jingyan.baidu.com/article/0aa22375bbffbe88cc0d6419.html http://www.aichengxu.com/v ...