201403-4 无线网络

问题描述

目前在一个很大的平面房间里有 n 个无线路由器,每个无线路由器都固定在某个点上。任何两个无线路由器只要距离不超过 r 就能互相建立网络连接。

除此以外,另有 m 个可以摆放无线路由器的位置。你可以在这些位置中选择至多 k 个增设新的路由器。

你的目标是使得第 1 个路由器和第 2 个路由器之间的网络连接经过尽量少的中转路由器。请问在最优方案下中转路由器的最少个数是多少?

输入格式

第一行包含四个正整数 n,m,k,r。(2 ≤ n ≤ 100,1 ≤ k ≤ m ≤ 100, 1 ≤ r ≤ 108108)。

接下来 n 行,每行包含两个整数 xi 和 yi,表示一个已经放置好的无线 路由器在 (xi, yi) 点处。输入数据保证第 1 和第 2 个路由器在仅有这 n 个路由器的情况下已经可以互相连接(经过一系列的中转路由器)。

接下来 m 行,每行包含两个整数 xi 和 yi,表示 (xi, yi) 点处可以增设 一个路由器。

输入中所有的坐标的绝对值不超过 108108,保证输入中的坐标各不相同。

输出格式

输出只有一个数,即在指定的位置中增设 k 个路由器后,从第 1 个路 由器到第 2 个路由器最少经过的中转路由器的个数。

样例输入

5 3 1 3
0 0
5 5
0 3
0 5
3 5
3 3
4 4
3 0

样例输出

2

 

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

class Main {
    static class State{
        public int id;
        public int x;
        public int y;
        public int xin;
        public int step;

        public State(int id, int x, int y) {
            this.id = id;
            this.x = x;
            this.y = y;
            this.xin=0;
            this.step=0;
        }
    }
    public static boolean isjin(int x1,int y1,int x2,int y2,int r){
        if(Math.pow(x2-x1,2)+Math.pow(y2-y1,2)<=Math.pow(r,2)){
            return true;
        }
        return false;
    }
    public static int bfs(State[] arr,Queue<State> queue,int k,int r,int n){
        int num=0;
        State first=arr[0];
        queue.add(first);
        int nowstep=1;
        arr[0].step=1;
        while(!queue.isEmpty()){
            nowstep++;
            int nn=queue.size();
            while(nn>0){
                State current=queue.poll();
                current.step=nowstep-1;
                if(current.xin>k){
                    nn--;
                    continue;
                }
                if(current.x==arr[1].x&&current.y==arr[1].y){
                    return current.step-2;
                }
                for(int i=1;i<arr.length;i++){
                    if(isjin(current.x,current.y,arr[i].x,arr[i].y,r)&&arr[i].step==0){
                        arr[i].step=nowstep;
                        if(arr[i].id>=n){
                            arr[i].xin=current.xin+1;
                            if(arr[i].xin>k){
                                arr[i].xin--;
                                continue;
                            }
                        }else{
                            arr[i].xin=current.xin;
                        }
                        queue.add(arr[i]);
                    }
                }
                nn--;
            }


        }
        return num;
    }

    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        int m=in.nextInt();
        int k=in.nextInt();
        int r=in.nextInt();
        State[] arr=new State[n+m];
        Queue<State> queue=new LinkedList<>();
        for(int i=0;i<n+m;i++){
            int x=in.nextInt();
            int y=in.nextInt();
            State s=new State(i,x,y);
            arr[i]=s;
        }
        System.out.println(bfs(arr,queue,k,r,n));

    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值