积水的城市

64 篇文章 0 订阅
9 篇文章 0 订阅

题目1 : 积水的城市

时间限制:10000ms

单点时限:1000ms

内存限制:256MB

描述

如下图所示,某市市区由M条南北向的大街和N条东西向的道路组成。其中由北向南第i条路和第i+1条路之间的距离是Bi (1 <= i < N),由西向东第i条街和第i+1条街之间的距离是Ai (1 <= i < M)。

小Ho现在位于第x条路和第y条街的交叉口,他的目的地是第p条路和第q条街的交叉口。由于连日降雨,城市中有K个交叉口积水太深不能通行。小Ho想知道到达目的地的最短路径的长度是多少。

输入

第一行包含两个整数N和M。(1 <= N, M <= 500)  

第二行包含N-1个整数, B1, B2, B3, ... BN-1。(1 <= Bi <= 100)  

第三行包含M-1个整数, A1, A2, A3, ... AM-1。(1 <= Ai <= 100)  

第四行包含一个整数K,表示积水的交叉口的数目。 (0 <= K <= 30)  

以下K行每行包含2个整数,X和Y,表示第X条路和第Y条街的交叉口积水。(1 <= X <= N, 1 <= Y <= M)  

第K+5行包含一个整数Q,表示询问的数目。 (1 <= Q <= 10)  

以下Q行每行包含4个整数x, y, p, q,表示小Ho的起止点。起止点保证不在积水的交叉口处。  (1 <= x, p <= N, 1 <= y, q <= M)

输出

对于每组询问,输出最短路的长度。如果小Ho不能到达目的地,输出-1。

样例输入

4 5  
2 4 1  
3 3 3 2  
3  
1 3  
2 3  
3 2  
1  
1 2 2 4  

样例输出

24

 

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int[] B = new int[n - 1];
        for (int i = 0; i < n - 1; i++) B[i] = in.nextInt();
        int[] A = new int[m - 1];
        for (int i = 0; i < m - 1; i++) A[i] = in.nextInt();
        int k = in.nextInt();
        boolean[][] block = new boolean[n][m];
        for (int i = 0; i < k; i++) {
            int x = in.nextInt();
            int y = in.nextInt();
            x -= 1;
            y -= 1;
            block[x][y] = true;
        }
        int Q = in.nextInt();
        for (int i = 0; i < Q; i++) {
            int x = in.nextInt();
            int y = in.nextInt();
            int p = in.nextInt();
            int q = in.nextInt();
            x -= 1;
            y -= 1;
            p -= 1;
            q -= 1;
            System.out.println(minDistance(x, y, p, q, n, m, A, B, block));
        }

    }

    private static int minDistance(int si, int sj, int ti, int tj, int n, int m, int[] a, int[] b, boolean[][] block) {
        int[][] dis = new int[n][m];
        for (int[] d : dis) Arrays.fill(d, Integer.MAX_VALUE);
        boolean[][] inQueue = new boolean[n][m];
        for (boolean[] in : inQueue) Arrays.fill(in, false);
        Queue<int[]> queue = new LinkedList<>();
        dis[si][sj] = 0;
        inQueue[si][sj] = true;
        queue.add(new int[]{si, sj});
        while (!queue.isEmpty()) {
            int[] s = queue.poll();
            inQueue[s[0]][s[1]] = false;
            int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
            for (int[] dir : dirs) {
                int[] next = {s[0] + dir[0], s[1] + dir[1]};
                if (next[0] >= 0 && next[0] < n && next[1] >= 0 && next[1] < m && !block[next[0]][next[1]]) {
                                        int w = Integer.MAX_VALUE;
                    if (dir[0] == -1 && dir[1] == 0) {
                        w = b[next[0]];
                    } else if (dir[0] == 1 && dir[1] == 0) {
                        w = b[s[0]];
                    } else if (dir[0] == 0 && dir[1] == 1) {
                        w = a[s[1]];
                    } else if (dir[0] == 0 && dir[1] == -1) {
                        w = a[next[1]];
                    }
                    if (dis[s[0]][s[1]] + w < dis[next[0]][next[1]]) {
                        dis[next[0]][next[1]] = dis[s[0]][s[1]] + w;
                        // 更新距离
                        if (!inQueue[next[0]][next[1]]) {
                            inQueue[next[0]][next[1]] = true;
                            queue.offer(next);
                        }
                    }
                }
            }
        }
                return dis[ti][tj] == Integer.MAX_VALUE ? -1 : dis[ti][tj];
    }

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值