{"id":8613,"date":"2021-10-17T21:23:21","date_gmt":"2021-10-18T04:23:21","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8613"},"modified":"2021-10-17T21:28:21","modified_gmt":"2021-10-18T04:28:21","slug":"leetcode-2045-second-minimum-time-to-reach-destination","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-2045-second-minimum-time-to-reach-destination\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2045. Second Minimum Time to Reach Destination"},"content":{"rendered":"\n<p>A city is represented as a&nbsp;<strong>bi-directional connected<\/strong>&nbsp;graph with&nbsp;<code>n<\/code>&nbsp;vertices where each vertex is labeled from&nbsp;<code>1<\/code>&nbsp;to&nbsp;<code>n<\/code>&nbsp;(<strong>inclusive<\/strong>). The edges in the graph are represented as a 2D integer array&nbsp;<code>edges<\/code>, where each&nbsp;<code>edges[i] = [u<sub>i<\/sub>, v<sub>i<\/sub>]<\/code>&nbsp;denotes a bi-directional edge between vertex&nbsp;<code>u<sub>i<\/sub><\/code>&nbsp;and vertex&nbsp;<code>v<sub>i<\/sub><\/code>. Every vertex pair is connected by&nbsp;<strong>at most one<\/strong>&nbsp;edge, and no vertex has an edge to itself. The time taken to traverse any edge is&nbsp;<code>time<\/code>&nbsp;minutes.<\/p>\n\n\n\n<p>Each vertex has a traffic signal which changes its color from&nbsp;<strong>green<\/strong>&nbsp;to&nbsp;<strong>red<\/strong>&nbsp;and vice versa every&nbsp;<code>change<\/code>&nbsp;minutes. All signals change&nbsp;<strong>at the same time<\/strong>. You can enter a vertex at&nbsp;<strong>any time<\/strong>, but can leave a vertex&nbsp;<strong>only when the signal is green<\/strong>. You&nbsp;<strong>cannot wait&nbsp;<\/strong>at a vertex if the signal is&nbsp;<strong>green<\/strong>.<\/p>\n\n\n\n<p>The&nbsp;<strong>second minimum value<\/strong>&nbsp;is defined as the smallest value<strong>&nbsp;strictly larger&nbsp;<\/strong>than the minimum value.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example the second minimum value of&nbsp;<code>[2, 3, 4]<\/code>&nbsp;is&nbsp;<code>3<\/code>, and the second minimum value of&nbsp;<code>[2, 2, 4]<\/code>&nbsp;is&nbsp;<code>4<\/code>.<\/li><\/ul>\n\n\n\n<p>Given&nbsp;<code>n<\/code>,&nbsp;<code>edges<\/code>,&nbsp;<code>time<\/code>, and&nbsp;<code>change<\/code>, return&nbsp;<em>the&nbsp;<strong>second minimum time<\/strong>&nbsp;it will take to go from vertex&nbsp;<\/em><code>1<\/code><em>&nbsp;to vertex&nbsp;<\/em><code>n<\/code>.<\/p>\n\n\n\n<p><strong>Notes:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>You can go through any vertex&nbsp;<strong>any<\/strong>&nbsp;number of times,&nbsp;<strong>including<\/strong>&nbsp;<code>1<\/code>&nbsp;and&nbsp;<code>n<\/code>.<\/li><li>You can assume that when the journey&nbsp;<strong>starts<\/strong>, all signals have just turned&nbsp;<strong>green<\/strong>.<\/li><\/ul>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/09\/29\/e1.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/09\/29\/e2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5\n<strong>Output:<\/strong> 13\n<strong>Explanation:<\/strong>\nThe figure on the left shows the given graph.\nThe blue path in the figure on the right is the minimum time path.\nThe time taken is:\n- Start at 1, time elapsed=0\n- 1 -&gt; 4: 3 minutes, time elapsed=3\n- 4 -&gt; 5: 3 minutes, time elapsed=6\nHence the minimum time needed is 6 minutes.\n\nThe red path shows the path to get the second minimum time.\n- Start at 1, time elapsed=0\n- 1 -&gt; 3: 3 minutes, time elapsed=3\n- 3 -&gt; 4: 3 minutes, time elapsed=6\n- Wait at 4 for 4 minutes, time elapsed=10\n- 4 -&gt; 5: 3 minutes, time elapsed=13\nHence the second minimum time is 13 minutes.      \n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/09\/29\/eg2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 2, edges = [[1,2]], time = 3, change = 2\n<strong>Output:<\/strong> 11\n<strong>Explanation:<\/strong>\nThe minimum time path is 1 -&gt; 2 with time = 3 minutes.\nThe second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= n &lt;= 10<sup>4<\/sup><\/code><\/li><li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4<\/sup>, n * (n - 1) \/ 2)<\/code><\/li><li><code>edges[i].length == 2<\/code><\/li><li><code>1 &lt;= u<sub>i<\/sub>, v<sub>i<\/sub>&nbsp;&lt;= n<\/code><\/li><li><code>u<sub>i<\/sub>&nbsp;!= v<sub>i<\/sub><\/code><\/li><li>There are no duplicate edges.<\/li><li>Each vertex can be reached directly or indirectly from every other vertex.<\/li><li><code>1 &lt;= time, change &lt;= 10<sup>3<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Best first search<\/strong><\/h2>\n\n\n\n<p>Since we&#8217;re only looking for second best, to avoid TLE, for each vertex, keep two best time to arrival is sufficient.<\/p>\n\n\n\n<p>Time complexity: O(2ElogE)<br>Space complexity: O(V+E)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n    vector<vector<int>> g(n + 1);\n    for (const auto& e : edges) {\n      g[e[0]].push_back(e[1]);\n      g[e[1]].push_back(e[0]);\n    }\n    priority_queue<pair<int, int>> q; \/\/ {-t, node}\n    q.emplace(0, 1);\n    int min_time = -1;\n    vector<vector<int>> ts(n + 1);\n    ts[1].push_back(0);\n    while (true) {\n      const int t = -q.top().first;\n      const int u = q.top().second;\n      if (u == n) {\n        if (min_time == -1 || t == min_time) { \n          min_time = t;\n        } else {\n          return t;\n        }\n      }\n      q.pop();\n      for (const auto& v : g[u]) {\n        const int tt = (((t \/ change) & 1) ? (t + change - t % change) : t) + time;\n        if ((ts[v].size() > 0 && tt == ts[v].back()) || ts[v].size() >= 2) continue;\n        ts[v].push_back(tt);        \n        q.emplace(-tt, v);        \n      }\n    }\n    return -1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A city is represented as a&nbsp;bi-directional connected&nbsp;graph with&nbsp;n&nbsp;vertices where each vertex is labeled from&nbsp;1&nbsp;to&nbsp;n&nbsp;(inclusive). The edges in the graph are represented as a 2D integer&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[34,77,217],"class_list":["post-8613","post","type-post","status-publish","format-standard","hentry","category-graph","tag-bfs","tag-graph","tag-hard","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8613","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8613"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8613\/revisions"}],"predecessor-version":[{"id":8615,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8613\/revisions\/8615"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}