aboutsummaryrefslogtreecommitdiff
path: root/min_score_path_city.cpp
blob: 16f4c875ffdcdc2e9526cebd9dd16cd7975f180c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// 2492. Minimum Score of a Path Between Two Cities
#include "leetcode.h"

class Solution {
public:
  int minScore(int n, vvd & roads) {
    unordered_map<int, unordered_map<int, int>> graph;
    for (auto r : roads)
      graph[r[0]][r[1]] = graph[r[1]][r[0]] = r[2];
    unordered_set<int> vis;
    function<int> dfs;
    dfs = [&](int n) -> int {
      int res = INT_MAX;
      vis.insert(n);
      for (auto [adj, scr] : graph[n]) {
        if (!vis.count(adj))
          res = min(res, dfs(adj));
        res = min(res, scr);
      }
      return res;
    };
    return dfs(1);
  }
};

int main() {
  Solution obj;
  vvd r1 = {{1, 2, 9}, {2, 3, 6}, {2, 4, 5}, {1, 4, 7}};
  vvd r2 = {{1, 2, 2}, {1, 3, 4}, {3, 4, 7}};
  printf("%d\n", obj.minScore(4, r1)); // expect: 5
  printf("%d\n", obj.minScore(4, r2)); // expect: 2
}