時間限制: 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;
}