ccf 201403-4 无线网络 bfs变形

本文介绍了一种使用C++实现的宽度优先搜索(BFS)算法,该算法应用于解决特定图论问题,包括节点间的可达性和最短路径计算。通过实例展示了如何构建图的数据结构,并利用队列来遍历图中的所有节点。

点击打开链接

//bfs
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
typedef long long LL;
struct node{
int time,num,v;
node(int time,int num,int v):time(time),num(num),v(v) {}
};
vector<int>G[205];
bool done[205];
int n,m,k,r;
int bfs()
{
    memset(done,false,sizeof(done));
    done[1]=true;
    queue<node>pq;
    while(!pq.empty()) pq.pop();
    pq.push(node(0,0,1));
    while(!pq.empty())
    {
        node k1=pq.front();
        pq.pop();
        if(k1.v==2) return k1.num-1;
        for(int i=0;i<G[k1.v].size();i++)
        {
            node f=k1;
            f.v=G[k1.v][i];
            if(!done[f.v]) {
                if(f.v>n)
                {
                    if(++f.time<=k) {
                        done[f.v]=true;
                        f.num++;
                        pq.push(f);
                    }
                }
                else {
                    f.num++;
                    done[f.v]=true;
                    pq.push(f);
                }
            }
        }
    }
}
int main()
{
    int x[205],y[205];
    scanf("%d%d%d%d",&n,&m,&k,&r);
    for(int i=1;i<=n+m;i++)
        scanf("%d%d",&x[i],&y[i]);
    for(int i=1;i<n+m;i++)
        for(int j=i+1;j<=n+m;j++)
             if((LL)(x[j]-x[i])*(x[j]-x[i])+(LL)(y[j]-y[i])*(y[j]-y[i])<=(LL)r*r) {
                G[i].push_back(j);
                G[j].push_back(i);
             }
    printf("%d\n",bfs());
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值