19726 星际旅行

19726 星际旅行

⭐️难度:困难
🌟考点:Dijkstra、省赛、最短路问题、期望、2024
📖
在这里插入图片描述
在这里插入图片描述

📚

import java.util.*;

public class Main {
    static int N = 1005;
    static ArrayList<Integer>[] g = new ArrayList[N]; // 无向图,存Integer,有向图存数组

    static boolean[] vis = new boolean[N];
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int m = sc.nextInt();
        int Q = sc.nextInt();
        
        // 无向图
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            addEdge(a,b);
            addEdge(b,a);//无向图,加两次边
        }

        int ans = 0;

        for (int i = 0; i < Q; i++) {
            int x = sc.nextInt();
            int y = sc.nextInt();
            ans += solve(x,y);
        }
        System.out.printf("%.2f",(double)ans / Q);// 将 ans 转换为 double 类型进行除法运算
    }

    static void addEdge(int u,int v){
        if(g[u] == null) g[u] = new ArrayList<>();
        g[u].add(v);
    }

    static int solve(int x,int y){
        Queue<int[]> q = new LinkedList<>();
        Arrays.fill(vis,false);
        q.add(new int[]{x,0});
        vis[x] = true;
        int res = 1;
        while(!q.isEmpty()){
            int[] cur = q.poll();
            if(cur[1] >= y) continue;
            if(g[cur[0]] == null) continue;
            for(int v : g[cur[0]]){
                if(vis[v]) continue;
                vis[v] = true;
                res ++;
                q.add(new int[]{v,cur[1]+1});
            }
        }
        return res;
    }
}

🍎笔记
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值