蒻蒻求教(超时了,算法复杂度问题还是细节)

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int h = scanner.nextInt();
        List<List<int[]>> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            List<int[]> ele = new ArrayList<>();
            list.add(ele);
        }
        for (int i = 0; i < m; i++) {
            int u = scanner.nextInt();
            int v = scanner.nextInt();
            int w = scanner.nextInt();
            int d = scanner.nextInt();
            list.get(u - 1).add(new int[]{v,w,d});
            list.get(v - 1).add(new int[]{u,w,d});
        }
        Queue<Map<String,Integer>> queue = new LinkedList<>();
        Map<String,Integer> map = new HashMap<>();
        map.put(1 + "",1);
        map.put("u",1);
        map.put("w",Integer.MAX_VALUE);
        map.put("d",0);
        queue.add(map);
        int max = -1;
        while (!queue.isEmpty()) {
            Map<String,Integer> head = queue.poll();
            if (head.get("u") == n) {
                if (head.get("w") > max) {
                    max = head.get("w");
                }
                continue;
            }
            Integer u = head.get("u");
            List<int[]> ele = list.get(u - 1);
            for (int i = 0; i < ele.size(); i++) {
                int[] next = ele.get(i);
                if (!head.containsKey(next[0]) && next[2] + head.get("d") <= h) {
                    Map<String,Integer> temp = new HashMap<>(head);
                    temp.put(next[0] + "",1);
                    temp.put("u",next[0]);
                    temp.put("w",Math.min(next[1],temp.get("w")));
                    temp.put("d",temp.get("d") + next[2]);
                    queue.add(temp);
                }
            }
        }
        System.out.println(max);
    }
}

#笔试##23届找工作求助阵地#
全部评论
dfs 求所有路径最小值的最大值吧
点赞 回复 分享
发布于 2023-05-05 08:14 湖南
二分加dijkstra
点赞 回复 分享
发布于 2023-05-04 21:35 江苏

相关推荐

05-07 17:58
门头沟学院 Java
wuwuwuoow:1.简历字体有些怪怪的,用啥写的? 2.Redis 一主二从为什么能解决双写一致性? 3.乐观锁指的是 SQL 层面的库存判断?比如 stock > 0。个人认为这种不算乐观锁,更像是乐观锁的思想,写 SQL 避免不了悲观锁的 4.奖项证书如果不是 ACM,说实话没什么必要写 5.逻辑过期时间为什么能解决缓存击穿问题?逻辑过期指的是什么 其实也没什么多大要改的。海投吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务