时间限制: 1.0s
内存限制: 256.0MB
问题描述: 问题描述
目前在一个很大的平面房间里有 n 个无线路由器,每个无线路由器都固定在某个点上。任何两个无线路由器只要距离不超过 r 就能互相建立网络连接。
除此以外,另有 m 个可以摆放无线路由器的位置。你可以在这些位置中选择至多 k 个增设新的路由器。
你的目标是使得第 1 个路由器和第 2 个路由器之间的网络连接经过尽量少的中转路由器。请问在最优方案下中转路由器的最少个数是多少?
输入格式
第一行包含四个正整数 n,m,k,r。(2 ≤ n ≤ 100,1 ≤ k ≤ m ≤ 100, 1 ≤ r ≤ 108)。
接下来 n 行,每行包含两个整数 xi 和 yi,表示一个已经放置好的无线 路由器在 (xi, yi) 点处。输入数据保证第 1 和第 2 个路由器在仅有这 n 个路由器的情况下已经可以互相连接(经过一系列的中转路由器)。
接下来 m 行,每行包含两个整数 xi 和 yi,表示 (xi, yi) 点处可以增设 一个路由器。
输入中所有的座标的绝对值不超过 108,保证输入中的座标各不相同。
输出格式
输出只有一个数,即在指定的位置中增设 k 个路由器后,从第 1 个路 由器到第 2 个路由器最少经过的中转路由器的个数。
样例输入
5 3 1 3
0 0
5 5
0 3
0 5
3 5
3 3
4 4
3 0
样例输出
2
用数据结构的最短路径迪杰斯特拉算法解决即可。审题不太仔细导致一直出错。第一个是起点,第二个就是终点了。
#include<iostream>
#include<queue>
using namespace std;
class Router{
public:
int x;
int y;
bool v;
Router(){
x = -1;
y = -1;
v = false;
}
};
int path[200][200] = {0};
int len[200]={0};
int dist[200]={0};
int canput[200] = {0};
bool inRange(long long x, long long y , long long r){
return x * x + y * y <= r * r;
}
int abs(int n){ //取绝对值
return (n^(n>>31))-(n>>31);
}
int main(){
int n,m,k,r,x,y,i,j,sum,index,cp;
queue<int> q;
cin>>n>>m>>k>>r;
sum = n+m;
Router *put = new Router[sum];
for(i = -1 ; ++i < sum;){
cin>>x>>y;
put[i].x = x;
put[i].y = y;
}
for(i = -1;++i < sum;){
for(j= i;++j < sum;){
if(inRange(abs(put[i].x - put[j].x),abs(put[i].y-put[j].y),r)){
path[i][len[i]++] = j;
path[j][len[j]++] = i;
}
}
}
/* cout<<"终点:"<<1<<endl;
for(i = -1;++i<sum;){
cout<<i<<": ";
for(j = -1;++j<len[i]-1;){
cout<<path[i][j]<<" ";
}
if(len[i]>0){
cout<<path[i][j];
}
cout<<endl;
}*/
q.push(0);
/* for(j=-1;++j<sum-1;){
cout<<j<<" ";
}
cout<<j<<endl;*/
while(!q.empty()){
index = q.front();
q.pop();
put[index].v = true;
for(i=-1;++i<len[index];){
cp = canput[path[index][i]];
if(path[index][i]>=n){
cp ++;
}
if(cp<=k && (dist[path[index][i]]==0 || dist[path[index][i]] > dist[index] + 1)){
dist[path[index][i]] = dist[index]+1;
if(put[path[index][i]].v == false){
q.push(path[index][i]);
}
}
}
/* cout<<index<<":"<<endl;
for(i=-1;++i<sum-1;){
cout<<dist[i]<<" ";
}
cout<<dist[i]<<endl;*/
}
/* for(j=-1;++j<sum-1;){
cout<<canput[j]<<" ";
}
cout<<canput[j]<<endl;*/
cout<<dist[1]-1<<endl;
delete[]put;
return 0;
}