ccf 无线网络

问题描述
  目前在一个很大的平面房间里有 n 个无线路由器,每个无线路由器都固定在某个点上。任何两个无线路由器只要距离不超过 r 就能互相建立网络连接。
  除此以外,另有 m 个可以摆放无线路由器的位置。你可以在这些位置中选择至多 k 个增设新的路由器。
  你的目标是使得第 1 个路由器和第 2 个路由器之间的网络连接经过尽量少的中转路由器。请问在最优方案下中转路由器的最少个数是多少?
输入格式
  第一行包含四个正整数 n,m,k,r。(2 ≤ n ≤ 100,1 ≤ k ≤ m ≤ 100, 1 ≤ r ≤ 10 8)。
  接下来 n 行,每行包含两个整数 x i 和 y i,表示一个已经放置好的无线 路由器在 (x i, y i) 点处。输入数据保证第 1 和第 2 个路由器在仅有这 n 个路由器的情况下已经可以互相连接(经过一系列的中转路由器)。
  接下来 m 行,每行包含两个整数 x i 和 y i,表示 (x i, y i) 点处可以增设 一个路由器。
  输入中所有的坐标的绝对值不超过 10 8,保证输入中的坐标各不相同。
输出格式
  输出只有一个数,即在指定的位置中增设 k 个路由器后,从第 1 个路 由器到第 2 个路由器最少经过的中转路由器的个数。
样例输入
5 3 1 3
0 0
5 5
0 3
0 5
3 5
3 3
4 4
3 0
样例输出
2


解题思路:这道题目最开始我是想用dp的,但是状态感觉不太好表示,所以就用BFS+优先队列处理,中转路由器个数少的排在队列前面。可是那个系统居然报编译错误,还不说明原因。。。。。无语。。。。希望哪位路过的大神能帮小弟我看一下。。。感激不尽!!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

int n,m,k,r,xe,ye,xs,ys;
bool vis[210];
struct Router
{
	int x,y,id; 
	int num;   //表示使用m个位置的个数
	int sum;   //表示目前总共用了的路由器个数
};
Router rt[210];

bool operator < (const Router &t1, const Router &t2)
{
	return t1.sum > t2.sum;
}

int BFS()
{
	double dis;
	int x,y;
	priority_queue<Router> q;
	Router now,next;
	for(int i = 2; i <= n+m; i++)
	{
		dis = sqrt(((rt[1].x - rt[i].x)*(rt[1].x - rt[i].x) + (rt[1].y - rt[i].y)*(rt[1].y - rt[i].y))*1.0);
		if(dis <= r)
		{
			now.x = rt[i].x; now.y = rt[i].y; now.id = i;
			if(i > n)
				now.num = 0;
			else now.num = 1;
			if(i == 2)
				now.sum = 0;
			else now.sum = 1;
			q.push(now);
		}
	}
	while(!q.empty())
	{
		now = q.top();
		q.pop();
		x = now.x, y = now.y;
		if(now.num > k) continue;
		if(now.id == 2) return now.sum;
		vis[now.id] = true;
		for(int i = 2; i <= n+m; i++)
		{
			if(vis[i] == true) continue;
			dis = sqrt(((x - rt[i].x)*(x - rt[i].x) + (y - rt[i].y)*(y - rt[i].y))*1.0);
			if(dis <= r)
			{
				next.x = rt[i].x; next.y = rt[i].y; next.id = i;
				if(i > n)
					next.num = now.num + 1;
				else next.num = now.num;
				if(i == 2)
					next.sum = now.sum;
				else next.sum = now.sum + 1;
				q.push(next);
			}
		}
	}
	return -1;
}

int main()
{	
	while(scanf("%d%d%d%d",&n,&m,&k,&r)!=EOF)
	{
		for(int i = 1; i <= n+m; i++)
			scanf("%d%d",&rt[i].x,&rt[i].y);
		memset(vis,false,sizeof(vis));
		int ans = BFS();
		printf("%d\n",ans);
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值