POJ--2236 Wireless Network

Wireless Network

An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftershock attacked, all computers in the network were all broken. The computers are repaired one by one, and the network gradually began to work again. Because of the hardware restricts, each computer can only directly communicate with the computers that are not farther than d meters from it. But every computer can be regarded as the intermediary of the communication between two other computers ,
that is to say computer A and computer B can communicate if computer A and computer B can communicate directly or there is a computer C that can communicate with both A and B

In the process of repairing the network, workers can take two kinds of operations at every moment, repairing a computer, or testing if two computers can communicate. Your job is to answer all the testing operations.
Input
The first line contains two integers N and d (1 <= N <= 1001, 0 <= d <= 20000). Here N is the number of computers, which are numbered from 1 to N, and D is the maximum distance two computers can communicate directly. In the next N lines, each contains two integers xi, yi (0 <= xi, yi <= 10000), which is the coordinate of N computers. From the (N+1)-th line to the end of input, there are operations, which are carried out one by one. Each line contains an operation in one of following two formats:

1. “O p” (1 <= p <= N), which means repairing computer p.
2. “S p q” (1 <= p, q <= N), which means testing whether computer p and q can communicate.
The input will not exceed 300000 lines.
Output
For each Testing operation, print “SUCCESS” if the two computers can communicate, or “FAIL” if not.
Sample Input
4 1
0 1
0 2
0 3
0 4
O 1
O 2
O 4
S 1 4
O 3
S 1 4
Sample Output
FAIL
SUCCESS

本题题意:首先输入两个数分别代表电脑个数n,和两台电脑之间能连接的最大距离D,接下来n行代表每个电脑所在的坐标位置,输入‘O’代表维修某一台电脑,输入‘S’代表测试某两台电脑能否连接,(连接的条件为:某两台电脑已经修好,且距离小于最大连接距离。或者是那两台电脑之间可以通过他们之间的媒介连接比如说A能和B通信,B能和C通信,那么A就能和C通信)。

本题思路:我们要建立连接首先要满足的条件是该电脑已经修好,并且与其连接的另一台电脑也已经修好,且他们之间的距离小于他们互相之间的连接的最大距离,然后再将他们合并为一个集合。这就意味着每修好一台电脑,如果要判断连接的话,都要遍历所有的已修好的电脑,能连接则合并。

AC代码如下:

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = 1e3 + 5;
int par[maxn];

struct node{

	int x, y;
	bool connect;//表示是否修好
}a[maxn];


double distance1( node a, node b)
{
	return sqrt((double)(a.x - b.x)*(a.x - b.x)+(a.y - b.y)*(a.y - b.y));
	//用到sqrt 注意转型为double
}

void init(int n)
{
	for (int i = 1; i <= n; i++)
	{
		par[i] = i;
	}
}

int find(int x)
{

	if (par[x] != x)x = find(par[x]);
	return par[x];
}

void unite(int x, int m, int distant)
{
	x = find(x);
	for (int i = 1; i <= m; i++)
	{//遍历所有电脑找可以联合的
		if (a[i].connect&&distance1(a[i], a[x]) <= distant)
		{	//如果第i个电脑已经修好,则判断距离是否可以连接,可以连接合并为一个集合
			int y = find(i);
			par[y] = x;//将y归属于x
		}
	}
}

int main()
{
	int m, dis;
	while (cin >> m >> dis)
	{
		init(m);
		for (int i = 1; i <= m; i++)
		{
			cin >> a[i].x >> a[i].y;
			a[i].connect = false;
			//初始时都未修好,为false
		}
		char work;
		while (cin >> work)
		{
			if (work == 'O')
			{
				int p;
				cin >> p;
				a[p].connect = true;//修好建立连接
				unite(p, m, dis);//看可以和哪些合并
			}
			else{
				int m1, m2;
				cin >> m1 >> m2;
				int x1 = find(m1);
				 int x2 = find(m2);
				if (x1 == x2)
					cout << "SUCCESS" << endl;
				else
					cout << "FAIL" << endl;
			}
		}
	}
	return 0;
}

发布了40 篇原创文章 · 获赞 10 · 访问量 2563

猜你喜欢

转载自blog.csdn.net/lsdstone/article/details/101348562
今日推荐