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 ...
随机推荐
- ios中怎么样判断路径最后的后缀名称
使用hasSuffix属性即可 例如 [photo.thumbnail_pic.lowercaseString hasSuffix:@"gif"]
- iOS学习之C语言数据类型
1.进制 0 1 2 3 4 5 6 7 8 9 A B C D E F 0X123(十六进制) 0123(八进制) 位权:单位数字的基本数值 2.数据类型 关键字 说明 字节大小 char ...
- C Primer Plus学习笔记(二)
1. C的左值用是指用于标志一个特定的数据对象的名字或表达式.“数据对象”是泛指数据存储的术语. 赋值运算符的左边应该是以个可以修改的左值. 右值是指可赋给可修gia的左值的量.右值可以是常量.变量或 ...
- JS中showModalDialog 详细使用
基本介绍: showModalDialog() (IE 4+ 支持) showModelessDialog() (IE 5+ 支持) window.showModalDialog() 方法用来创建一个 ...
- [DHCP服务]——一个验证DHCP原理实验(VMware)
大致实验拓扑图 DHCP Server端的配置 1. 安装DHCP # yum -y install dhcp 2. 拷贝配置文件 # /dhcpd.conf.sample /etc/dhcp/dhc ...
- 避免写出IE Bug
https://github.com/KitWong/KitWong.github.io/tree/master/_posts
- Eclipse+pydev 常用快捷键
多行缩进(减少缩进):tab/shift+tab 复制行: Ctrl+Alt+方向键'↓' 删除行:Ctrl+d 自动完成:Alt+/ 注释:Ctrl+/ 窗口最大小:Ctrl+m 1 几个最重要的 ...
- hdu 4046 Panda 树状数组
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4046 When I wrote down this letter, you may have been ...
- shader 里面的分支
shader 里面的真分支会降低效率 一种方法:构造一个分段函数出来 比如saturate(depth*1.5f)
- JS设计模式——5.单体模式
JS设计模式——5.单体模式 http://www.cnblogs.com/JChen666/p/3610585.html 单体模式的优势 用了这么久的单体模式,竟全然不知!用它具体有哪些好处呢? ...