搜索 HOJ 1266 Phone Home

Phone Home

My Tags  (Edit)
Source : ACM ICPC East Central North America 2003
Time limit : 1 secMemory limit : 32 M

Submitted : 49, Accepted : 36

When relay towers for mobile telephones communicate with the mobile phones in their area, there is always the possibility of interference. So, when assigning the transmission frequency, the FCC makes sure that nearby towers have frequencies that aren't too close. On the other hand, the FCC does not want to assign too many different frequencies; they want to save as many as possible for other uses. Your job is to find an optimal assignment of frequencies.

In this problem, the frequencies will be integers. Nearby towers must be assigned frequencies that differ by at least 2. You'll find an assignment using as few frequencies as possible. For example, consider the following two arrangements of towers. Two towers near each other are indicated by the connecting line.

搜索 HOJ 1266 Phone Home - 恶魔仁 - 恶魔仁

"""

Note that the following are legal frequency assignments to these two tower configurations. However, the second arrangement does not use the fewest number of frequencies possible, since the tower with frequency 5 could have frequency 1.

搜索 HOJ 1266 Phone Home - 恶魔仁 - 恶魔仁

"""


Input

There will be multiple test cases. Input for each test case will consist of two lines: the first line will contain the integer n, indicating the number of towers. The next line will be of the form x1 y1 x2 y2 ... xn yn where xi yi are the coordinates of tower i. A pair of towers are considered "near" each other if the distance between them is no more than 20. There will be no more than 12 towers and no tower will have more than 4 towers near it. A value of n = 0 indicates end of input.


Output

For each test case, you should print one line in the format:

The towers in case n can be covered in f frequencies.

where you determine the value for f. The case numbers, n, will start at 1.


Sample Input

5
0 0 5 7.5 1 -3 10.75 -20.1 12.01 -22
6
0 1 19 0 38 1 38 21 19 22 0 21
0


Sample Output

The towers in case 1 can be covered in 3 frequencies.
The towers in case 2 can be covered in 2 frequencies.

题意:给出一个图,然后要你分配数字使得任意两个相邻的点之间的差值不小于2并且使用的数字的数量最少。

思路:我们一开始把所有节点都染成一种颜色,然后我们用另一种颜色开始染,把每个节点的相邻的与自己颜色相同的节点染成当前的颜色,扫完一遍之后,看是否满足题目要求,即任意相邻节点的颜色都不同,如果不是,那么我用下一个颜色,继续上一次的操作,直到满足要求为止,最后我们用到的颜色数量就是答案。。。其实我不知道这样对不对的。。。反正是AC了


代码:
#include<iostream>
#include<algorithm>
#include<vector>
#include<math.h>
#include<cstdio>
#include<string.h>
#include<cstring>
using namespace std;
#define eps 1e-8
const int maxn = 15;
struct Node
{
double x;
double y;
}node[maxn];

int n , cnt , Cas = 0;
int col[maxn];
vector<int> G[maxn];

inline double sqr(double x)
{
return x*x;
}

inline double cal_dis(Node n1 , Node n2)
{
return sqrt (sqr(n1.x-n2.x)+sqr(n1.y-n2.y));
}

void init()
{
for (int i = 0 ; i < n ; ++i) G[i].clear();
memset(col,0,sizeof(col));
cnt = 1;
}

void input()
{
for (int i = 0 ; i < n ; ++i) scanf("%lf%lf",&node[i].x,&node[i].y);
for (int i = 0 ; i < n ; ++i) 
for (int j = i+1 ; j < n ; ++j) if (cal_dis(node[i],node[j]) < 20+eps)
G[i].push_back(j) , G[j].push_back(i);
}

bool ok()
{
for (int i = 0 ; i < n ; ++i) if (col[i]==cnt-1)
{
for (int j = 0 ; j < G[i].size() ; ++j)
{
int y = G[i][j];
if (col[y]==col[i])
col[y] = cnt;
}
}
for (int i = 0 ; i < n ; ++i) if (col[i]==cnt) 
return false;
return true;
}

void solve()
{
while (!ok()) ++cnt;
printf("The towers in case %d can be covered in %d frequencies.\n",Cas,cnt);
}

int main()
{
while (scanf("%d",&n),n)
{
++Cas;
init();
input();
solve();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值