CCF_ 201403-4_无线网络
分散点的bfs,先建立一个互相是否可达的二维数组,vis[i][j]代表到第i个点,走了j步的状态,注意判断新增路由器数量是否超过K。
#include<cstdio> #include<iostream> #include<queue> using namespace std; struct point{ long long x,y,num,counts,sets; }a[205]; int cango[205][205] = {0},vis[205][205] = {0}; int main() { int n,m,k; long long r; cin >> n >> m >> k >> r; for(int i = 1;i <= m+n;i++) { cin >> a[i].x >> a[i].y; a[i].num = i; } for(int i = 1;i < m+n;i++) { for(int j = i;j <= m+n;j++) { if((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y) <= r*r) { cango[i][j] = cango[j][i] = 1; } } } a[1].counts = 0; a[1].sets = 0; vis[1][0] = 1; queue<point> q; q.push(a[1]); while(!q.empty()) { long long num = q.front().num,counts = q.front().counts; q.pop(); if(num == 2) { cout << counts-1 << endl; return 0; } for(int i = 1;i <= m+n;i++) { long long sets; if(i > n) sets = q.front().sets+1; else sets = q.front().sets; if(cango[num][i] && !vis[i][counts+1] && sets <= k) { a[i].sets = sets; a[i].counts = counts+1; q.push(a[i]); vis[i][counts+1] = 1; } } } }