算法题代码模板

102 阅读1分钟

算法题代码模板

1. 将在图内的所有边遍历一次

    HashMap<Integer, List<int[]>> map = new HashMap<>();
    public int minScore(int n, int[][] roads) {
            for (int[] road : roads) {
                map.computeIfAbsent(road[0], i -> new ArrayList<>()).add(new int[] { road[1], road[2] });
                map.computeIfAbsent(road[1], i -> new ArrayList<>()).add(new int[] { road[0], road[2] });
            }
            return minScore(1);
        }

        private int minScore(int n) {
            int min = Integer.MAX_VALUE;
            for (int[] next : map.replace(n, new ArrayList<>())) {
                min = Math.min(min, Math.min(next[1], minScore(next[0])));
            }
            return min;
        }