bfs , 图论
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#define x first
#define y second
using namespace std;
typedef long long LL;//距离可能会爆int
typedef pair<int, int> PII;
const int N = 210, M = N * N;//M为边数
int n, m, k, r;
int h[N], e[M], ne[M], idx;
PII p[N];
int dist[N][N];
bool check(PII a, PII b)
{
LL dx = a.x - b.x;
LL dy = a.y - b.y;
return dx * dx + dy * dy <= (LL)r * r;
}
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}
int bfs()
{
queue<PII> q;
q.push({1, 0});//最开始在1号点,没有经过特殊点
memset(dist, 0x3f, sizeof dist);
dist[1][0] = 0;//到 1 这个点没有经过任何特殊点的距离为0
while (q.size())
{
auto t = q.front();
q.pop();
for (int i = h[t.x]; ~i; i = ne[i])//枚举t的所有邻边
{
int x = e[i], y = t.y;//x表示节点编号,y表示走过的特殊点数量
if (x > n) y ++ ;//是特殊点
if (y <= k)//特殊点数量不能超过 k
{
if (dist[x][y] > dist[t.x][t.y] + 1)//经过这个点距离更短
{
dist[x][y] = dist[t.x][t.y] + 1;
q.push({x, y});
}
}
}
}
int res = 1e8;
for (int i = 0; i <= k; i ++ )
res = min(res, dist[2][i]);//到2这个点经过特殊点i的距离,这里每个点距离都为1
return res - 1;//算1到2之间的数量,不算1和2
}
int main()
{
cin >> n >> m >> k >> r;
memset(h, -1, sizeof h);
for (int i = 1; i <= n; i ++ ) cin >> p[i].x >> p[i].y;//普通点
for (int i = n + 1; i <= n + m; i ++ ) cin >> p[i].x >> p[i].y;//特殊点
for (int i = 1; i <= n + m; i ++ )
for (int j = i + 1; j <= n + m; j ++ )
if (check(p[i], p[j]))
add(i, j), add(j, i);//距离 <=r 可以连接边
cout << bfs() << endl;
return 0;
}