2017-08-04 16:19:13

writer:pprp

题意如下:

Description

The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are to be used in establishing the network: every outpost will have a radio transceiver and some outposts will in addition have a satellite channel. 
Any two outposts with a satellite channel can communicate via the satellite, regardless of their location. Otherwise, two outposts can communicate by radio only if the distance between them does not exceed D, which depends of the power of the transceivers. Higher power yields higher D but costs more. Due to purchasing and maintenance considerations, the transceivers at the outposts must be identical; that is, the value of D is the same for every pair of outposts.

Your job is to determine the minimum D required for the transceivers. There must be at least one communication path (direct or indirect) between every pair of outposts.

Input

The first line of input contains N, the number of test cases. The first line of each test case contains 1 <= S <= 100, the number of satellite channels, and S < P <= 500, the number of outposts. P lines follow, giving the (x,y) coordinates of each outpost in km (coordinates are integers between 0 and 10,000).

Output

For each case, output should consist of a single line giving the minimum D required to connect the network. Output should be specified to 2 decimal points.

Sample Input

1
2 4
0 100
0 300
0 600
150 750

Sample Output

212.13

题意简化:
  个人觉得这道题最难的在于理解,简化以后的题意是:  

    • n个站点,s个卫星系统,每个卫星系统只能安排在一个站点
    • 有卫星系统的站点间通讯不需要代价
    • 任意两点(i, j)间皆可通讯,代价为dis[i][j]
    • 请用最小的代价使得任意两个站点间均可以通讯
    • n, s <= 1000

分析:

  n个站点,应该用Kruskal算法进行最小生成树的进行;用ans数组记录下来从小到大的边的边权,

  最终结果就应该是ans[num - s] num是最小生成树的边的数目,等于是最后n个比较大的被舍去,需要最大的就是被舍去以后最大的点


代码及分析:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm> using namespace std;
//n个卫星,m个基站
int n, m;
int x, y;
const int maxn = ;
const int INF = 0x3f3f3f3f;
int parent[maxn];
//有多少个边
int num;
//ans数组中存储的是升序排序的入树的边的权值
double ans[maxn]; struct point
{
int x;
int y;
} p[maxn]; struct ds
{
int u;
int v;
double w;
} d[maxn * maxn + maxn]; double dist(const point& a, const point& b)
{
return sqrt(1.0 * (a.x - b.x) * (a.x - b.x) +
1.0 * (a.y - b.y) * (a.y - b.y));
} bool cmp(const ds &a, const ds& b)
{
return a.w < b.w;
} //并查集中找到父节点的操作
int Find(int x)
{
//递归?
//parent[x] = Find(parent[x]);
//return parent[x]; //自己写的迭代
int tmp = x;
while(parent[tmp] != tmp)
{
tmp = parent[tmp];
} return tmp;
} void merge(int x, int y)
{
int fa = Find(x);
int fb = Find(y);
if(fa != fb)
{
parent[fa] = fb;
}
} void init()
{
scanf("%d%d",&n,&m);
num = ;
for(int i = ; i < m ; i++)
{
scanf("%d%d",&p[i].x,&p[i].y);
} for(int i = ; i < m ; i++)
for(int j = i + ; j < m ; j++)
{
d[num].u = i;
d[num].v = j;
d[num++].w = dist(p[i],p[j]);
} for(int i = ; i <= m ; i++)
parent[i] = i; //从小到大进行排序
sort(d,d+num,cmp);
} //最主要的代码:Kruskal
void kruskal()
{
int cnt = ;
for(int i = ; i < num ; i++)
{
int fa = Find(d[i].u);
int fb = Find(d[i].v); if(fa != fb)
{
merge(d[i].u,d[i].v);
ans[cnt++] = d[i].w;
}
}
printf("%.2f\n",ans[cnt - n]);
} int main()
{
int t; scanf("%d",&t); while(t--)
{
init();
kruskal();
}
return ;
}

注意点:在poj提交的时候对double型的应该用%f\n而不要用%lf\n

												

poj2349 Arctic Network - 最小生成树的更多相关文章

  1. [poj2349]Arctic Network(最小生成树+贪心)

    Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17758   Accepted: 5646 D ...

  2. [Poj2349]Arctic Network(二分,最小生成树)

    [Poj2349]Arctic Network Description 国防部(DND)要用无线网络连接北部几个哨所.两种不同的通信技术被用于建立网络:每一个哨所有一个无线电收发器,一些哨所将有一个卫 ...

  3. POJ 2349 Arctic Network (最小生成树)

    Arctic Network Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

  4. POJ2349 Arctic Network 2017-04-13 20:44 40人阅读 评论(0) 收藏

    Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19113   Accepted: 6023 D ...

  5. POJ2349 Arctic Network(Prim)

    Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16968   Accepted: 5412 D ...

  6. POJ-2349 Arctic Network(最小生成树+减免路径)

    http://poj.org/problem?id=2349 Description The Department of National Defence (DND) wishes to connec ...

  7. TZOJ 2415 Arctic Network(最小生成树第k小边)

    描述 The Department of National Defence (DND) wishes to connect several northern outposts by a wireles ...

  8. POJ2349 Arctic Network

    原题链接 先随便找一棵最小生成树,然后贪心的从大到小选择边,使其没有贡献. 显然固定生成树最长边的一个端点安装卫星频道后,从大到小选择边的一个端点作为卫星频道即可将该边的贡献去除. 所以最后的答案就是 ...

  9. POJ 2349 Arctic Network(最小生成树+求第k大边)

    题目链接:http://poj.org/problem?id=2349 题目大意:有n个前哨,和s个卫星通讯装置,任何两个装了卫星通讯装置的前哨都可以通过卫星进行通信,而不管他们的位置. 否则,只有两 ...

随机推荐

  1. Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum

    题目链接:传送门 题目大意:给n个数,m次询问,每次询问区间 l,r 内出现偶数次数的异或和 题目思路:前缀和+离线处理+树状数组 首先可以知道, l,r 内出现奇数次的数的和,就是把 l,r内所有数 ...

  2. jquery 轮播图实例

    实现效果:1.图片每2秒钟切换1次. 2.当鼠标停留在整个页面上时,图片不进行轮播. 3.当点击右下角的小球时,出现该选项的对应图片,而且切换页选项的背景颜色发生相应的变化. 4.当图片发生轮播切换时 ...

  3. linux禁用触摸板驱动

    Method 1: 终端输入如下命令: sudo modprobe -r psmouse 如果打开触摸板就是: sudo modprobe psmouse ------- Method 2: 第一步: ...

  4. 6号css学习小记

    一.overfloat属性:(四个值) visible :默认值.内容不会被修剪,会呈现在元素匡之外. hidden:内容会被修剪,并且其余内容是不可见的. scroll :内容会被修剪,但是浏览器会 ...

  5. 安装Vmware ESX Server5.5 ——hardware virtualization is not a feature of the cpu or is not enabled in the BIOS

    Error信息: hardware virtualization is not a feature of the cpu or is not enabled in the BIOS 解决方案: F2进 ...

  6. win7(iis7)无法加载运行CSS文件的解决方法

    在打开或关闭window功能中的Internet信息服务里的万维网服务=>常见HTTP功能=>静态内容 ,将其选上 即可了,如下图

  7. 多个StoryBoard之间的跳转

    iOS项目中可以将同一业务流程的页面归置到一个StoryBoard中,项目中必然会包含多个StroryBoard,可以利用跳转,实现项目的不同业务流程页面间的跳转切换. 实现思路: 1,项目(Proj ...

  8. py, pyc, pyw, pyo, pyd 及发布程序时的选择 Compiled Python File (.pyc)

    Python 程序扩展名(py, pyc, pyw, pyo, pyd)及发布程序时的选择 - 司开星的专栏 - CSDN博客 https://blog.csdn.net/chroming/artic ...

  9. SQL中的函数 •Aggregate 函数 •Scalar 函数

    合计函数  :Aggregate是针对一系列值的操作,返回一个单一的值 Scalar 函数是针对一个单一的值的操作,返回基于输入值的一个单一值 合计函数: AVG()返回某列的平均值:COUNT()返 ...

  10. 3*0.1 == 0.3 将会返回什么?true 还是 false?

    false,因为有些浮点数不能完全精确的表示出来 public static void main(String[] args) { System.out.println(3 * 0.1); Syste ...