无线网络 (第四题)

3200. 无线网络 - AcWing题库 

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值