2492. Minimum Score of a Path Between Two Cities

```java
class Solution {
    public int minScore(int n, int[][] roads) {
        Map<Integer, List<int[]>> graph = buildGraph(roads);
        boolean[] seen = new boolean[n+1];
        seen[1] = true;
        return dfs(n, 1, graph, seen);
    }
    private int dfs(int n, int city, Map<Integer, List<int[]>> graph, boolean[] seen) {
        int result = Integer.MAX_VALUE;
        for (int[] next : graph.get(city)) {
            int nextCity = next[0];
            int nextDistance = next[1];
            
            result = Math.min(result, nextDistance);
            if (seen[nextCity]) {
                continue;
            }
            seen[nextCity] = true;
            result = Math.min(result, dfs(n, nextCity, graph, seen));
        }
        return result;
    }
    private Map<Integer, List<int[]>> buildGraph(int[][] roads) {
        Map<Integer, List<int[]>> graph = new HashMap<>();
        for (int i = 0; i < roads.length; i++) {
            int from = roads[i][0];
            int to = roads[i][1];
            int distance = roads[i][2];
            graph.putIfAbsent(from, new ArrayList<>());
            graph.get(from).add(new int[]{to, distance});

            graph.putIfAbsent(to, new ArrayList<>());
            graph.get(to).add(new int[]{from, distance});
        }
        return graph;
    }
}
/*
using dfs find all roads in 1 to n, add to minheap
return the min from minheap
*/
```

Last updated