无线电网络

  题目大意:就是地震后,所有的电脑都坏了,现在可以修复,而且要重新连成一个网络,两台电脑之间最大连接距离为D,两台电脑可以有中继电脑,按O修复电脑,按S测试两台电脑是否有链接,如果有就输出SUCCESS,不行就FAIL

  一看到电脑,我就联想到了查并集,这一题也不例外,我们维护在距离内的查并集就可以了,具体思路不难,每一次修复都扫描所有的节点看能否连接即可

  

 #include <iostream>
#include <functional>
#include <algorithm>
#include <math.h>
#define MAX 1004 using namespace std;
typedef int Position;
typedef struct _set
{
int x;
int y;
}Point; static Point Comp[MAX];
static Position Set[MAX];
static bool used[MAX];
static int max_dist; Position Find(Position);
void Unite_Set(Position, Position);
void Repaired(const int, Position);
void Test(Position, Position);
double Dist_Cal(Position, Position); int main(void)
{
int n, tmp_p, tmp_x, tmp_y;
char choice;
while (~scanf("%d%d", &n, &max_dist))
{
memset(Set, -, sizeof(Set));
memset(used, , sizeof(used));
for (int i = ; i <= n; i++)
scanf("%d%d", &Comp[i].x, &Comp[i].y);
getchar();
while (~scanf("%c", &choice))
{
if (choice == 'O')
{
scanf("%d", &tmp_p);
Repaired(n, tmp_p);
}
else
{
scanf("%d%d", &tmp_x, &tmp_y);
Test(tmp_x, tmp_y);
}
getchar();
}
}
return ;
} double Dist_Cal(Position i, Position j)
{
return sqrt((double)((Comp[i].x - Comp[j].x)*(Comp[i].x - Comp[j].x)) +
(double)((Comp[i].y - Comp[j].y)*(Comp[i].y - Comp[j].y)));
} void Unite_Set(Position x, Position y)
{
Position px, py;
px = Find(x); py = Find(y);
if (px != py)
{
if (Set[px] < Set[py])//按大小求并
{
Set[px] += Set[py];
Set[py] = px;
}
else
{
Set[py] += Set[px];
Set[px] = py;
}
}
} Position Find(Position x)
{
if (Set[x] < )
return x;
else return Set[x] = Find(Set[x]);
} void Repaired(const int n, Position x)
{
used[x] = ;
for (int i = ; i <= n; i++)
{
if (i == x || !used[i]) continue;
if (Dist_Cal(x, i) <= (double)max_dist)
Unite_Set(x, i);
}
} void Test(Position x, Position y)
{
Position px, py;
px = Find(x);
py = Find(y); if (px == py) puts("SUCCESS");
else puts("FAIL");
}

                    

DisJSet:Wireless Network(POJ 2236)的更多相关文章

  1. Wireless Network(POJ 2236)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 20724   Accepted: 871 ...

  2. Day5 - B - Wireless Network POJ - 2236

    An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wi ...

  3. (并查集) Wireless Network --POJ --2236

    链接: http://poj.org/problem?id=2236 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...

  4. Wireless Network POJ - 2236 (并查集)

    #include<iostream> #include<vector> #include<string> #include<cmath> #includ ...

  5. A - Wireless Network POJ - 2236

    题目大意:有n台坏掉的电脑,给出每台电脑的坐标,然后每次询问输入0(字符) x,表示电脑x恢复正常,输入S x y 询问x和y是否可以联网.只要是x和y的距离小于距离d,那么就可以联网,如果有个中介c ...

  6. A - Wireless Network POJ - 2236-kuangbin带你飞

    A - Wireless Network POJ - 2236 Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 50348 ...

  7. POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集

    POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...

  8. [并查集] POJ 2236 Wireless Network

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 25022   Accepted: 103 ...

  9. poj 2236:Wireless Network(并查集,提高题)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 16065   Accepted: 677 ...

随机推荐

  1. NOIP2008普及组题解

    NOIP2008普及组题解 从我在其他站的博客直接搬过来的 posted @ 2016-04-16 01:11 然后我又搬回博客园了233333 posted @ 2016-06-05 19:19 T ...

  2. STL Iterators

    Summary of Chapter 33 STL Iterators from The C++ Programming Language 4th. Ed., Bjarne Stroustrup. - ...

  3. 通过HTTP协议实现多线程下载

    1. 基本原理,每条线程从文件不同的位置开始下载,最后合并出完整的数据. 2. 使用多线程下载的好处     下载速度快.为什么呢?很好理解,以往我是一条线程在服务器上下载.也就是说,对应在服务器上, ...

  4. 车牌号对应归属地及城市JSON带简码

    车牌号对应归属地及城市JSON带简码 car_city.json [ { "code": "冀A", "city": "石家庄&q ...

  5. 【转】KMP算法

    转载请注明来源,并包含相关链接.http://www.cnblogs.com/yjiyjige/p/3263858.html 网上有很多讲解KMP算法的博客,我就不浪费时间再写一份了.直接推荐一个当初 ...

  6. Python序列的切片操作与技巧

    切片操作 对于具有序列结构的数据来说,切片操作的方法是:consequence[start_index: end_index: step]. start_index: 表示是第一个元素对象,正索引位置 ...

  7. 有关基于模型的设计(MBD)一些概念和理解(zz)

    http://www.matlabsky.com/thread-38774-1-1.html 本文转载于MathWorks中国高级工程师董淑成的帖子内容.为了方便阅读,对原文进行了重新整理编辑. 之前 ...

  8. pyenv 以及 virtualenv

    244 pyenv global 3.5.1 245 which python 246 python 247 pip install virtualenv 248 ls 249 pwd 250 ls ...

  9. linux 定时 svn 代码更新,配置文件不修改

    普通参数: 普通参数为正常的传参数:  例子:  f1("111") 指定参数: 指定参数为指定哪个参数给函数方法里面某个形式参数专用,优点:不受传参数的位置约束.   例子:  ...

  10. Linux创建线程

    #include"stdio.h" #include"pthread.h" #include"unistd.h" ; void *creat ...