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

												

最新文章

  1. 基本shell编程【2】-服务端发布脚本
  2. LoadRunner培训初级教程
  3. NSOperation基本概念
  4. java 线性规划 和lingo 比较
  5. WinForm使用皮肤图文步骤
  6. [BZOJ 3622]已经没有什么好害怕的了
  7. Oracle 手动收集统计信息
  8. Handler发送Message
  9. JDynamic :支持Json反序列化为Dynamic对象
  10. access数据库的连接字符串以及数据库操作类
  11. 刷爆github小绿点
  12. jmeter之自定义java请求性能测试
  13. 神经网络NN
  14. JS数组根据属性来实现排序
  15. javaWeb学习总结(5)- HttpServletRequest应用
  16. 动手写个数字输入框1:input[type=number]的遗憾
  17. C语言可变参实现参数累加返回
  18. python基础知识之zip
  19. XSS跨站攻击(二)
  20. Python内部机制。

热门文章

  1. oracle的row_number() OVER (ORDER BY COL2 asc)和row_number() OVER (PARTITION BY COL1 ORDER BY COL2)的用法
  2. Leetcode-Combinations Sum II
  3. js处理事件冒泡
  4. Vulnerabilities by Type
  5. python线程池/进程池创建
  6. Vue(5)- axios、vuex
  7. Andrew Ng机器学习总结(自用)
  8. Python基础-面向对象1
  9. ansible安装及使用
  10. [intellij]create gradle project