{"id":8685,"date":"2021-11-07T14:09:43","date_gmt":"2021-11-07T22:09:43","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8685"},"modified":"2021-11-07T14:10:45","modified_gmt":"2021-11-07T22:10:45","slug":"leetcode-2065-maximum-path-quality-of-a-graph","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-2065-maximum-path-quality-of-a-graph\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2065. Maximum Path Quality of a Graph"},"content":{"rendered":"\n<p>There is an&nbsp;<strong>undirected<\/strong>&nbsp;graph with&nbsp;<code>n<\/code>&nbsp;nodes numbered from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>n - 1<\/code>&nbsp;(<strong>inclusive<\/strong>). You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>values<\/code>&nbsp;where&nbsp;<code>values[i]<\/code>&nbsp;is the&nbsp;<strong>value&nbsp;<\/strong>of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;node. You are also given a&nbsp;<strong>0-indexed<\/strong>&nbsp;2D integer array&nbsp;<code>edges<\/code>, where each&nbsp;<code>edges[j] = [u<sub>j<\/sub>, v<sub>j<\/sub>, time<sub>j<\/sub>]<\/code>&nbsp;indicates that there is an undirected edge between the nodes&nbsp;<code>u<sub>j<\/sub><\/code>&nbsp;and&nbsp;<code>v<sub>j<\/sub><\/code>,and it takes&nbsp;<code>time<sub>j<\/sub><\/code>&nbsp;seconds to travel between the two nodes. Finally, you are given an integer&nbsp;<code>maxTime<\/code>.<\/p>\n\n\n\n<p>A&nbsp;<strong>valid<\/strong>&nbsp;<strong>path<\/strong>&nbsp;in the graph is any path that starts at node&nbsp;<code>0<\/code>, ends at node&nbsp;<code>0<\/code>, and takes&nbsp;<strong>at most<\/strong>&nbsp;<code>maxTime<\/code>&nbsp;seconds to complete. You may visit the same node multiple times. The&nbsp;<strong>quality<\/strong>&nbsp;of a valid path is the&nbsp;<strong>sum<\/strong>&nbsp;of the values of the&nbsp;<strong>unique nodes<\/strong>&nbsp;visited in the path (each node&#8217;s value is added&nbsp;<strong>at most once<\/strong>&nbsp;to the sum).<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>maximum<\/strong>&nbsp;quality of a valid path<\/em>.<\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;There are&nbsp;<strong>at most four<\/strong>&nbsp;edges connected to each node.<\/p>\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\/10\/19\/ex1drawio.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> values = [0,32,10,43], edges = [[0,1,10],[1,2,15],[0,3,10]], maxTime = 49\n<strong>Output:<\/strong> 75\n<strong>Explanation:<\/strong>\nOne possible path is 0 -&gt; 1 -&gt; 0 -&gt; 3 -&gt; 0. The total time taken is 10 + 10 + 10 + 10 = 40 &lt;= 49.\nThe nodes visited are 0, 1, and 3, giving a maximal path quality of 0 + 32 + 43 = 75.\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\/10\/19\/ex2drawio.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> values = [5,10,15,20], edges = [[0,1,10],[1,2,10],[0,3,10]], maxTime = 30\n<strong>Output:<\/strong> 25\n<strong>Explanation:<\/strong>\nOne possible path is 0 -&gt; 3 -&gt; 0. The total time taken is 10 + 10 = 20 &lt;= 30.\nThe nodes visited are 0 and 3, giving a maximal path quality of 5 + 20 = 25.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/10\/19\/ex31drawio.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> values = [1,2,3,4], edges = [[0,1,10],[1,2,11],[2,3,12],[1,3,13]], maxTime = 50\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong>\nOne possible path is 0 -&gt; 1 -&gt; 3 -&gt; 1 -&gt; 0. The total time taken is 10 + 13 + 13 + 10 = 46 &lt;= 50.\nThe nodes visited are 0, 1, and 3, giving a maximal path quality of 1 + 2 + 4 = 7.<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/10\/21\/ex4drawio.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> values = [0,1,2], edges = [[1,2,10]], maxTime = 10\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> \nThe only path is 0. The total time taken is 0.\nThe only node visited is 0, giving a maximal path quality of 0.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == values.length<\/code><\/li><li><code>1 &lt;= n &lt;= 1000<\/code><\/li><li><code>0 &lt;= values[i] &lt;= 10<sup>8<\/sup><\/code><\/li><li><code>0 &lt;= edges.length &lt;= 2000<\/code><\/li><li><code>edges[j].length == 3<\/code><\/li><li><code>0 &lt;= u<sub>j&nbsp;<\/sub>&lt; v<sub>j<\/sub>&nbsp;&lt;= n - 1<\/code><\/li><li><code>10 &lt;= time<sub>j<\/sub>, maxTime &lt;= 100<\/code><\/li><li>All the pairs&nbsp;<code>[u<sub>j<\/sub>, v<sub>j<\/sub>]<\/code>&nbsp;are&nbsp;<strong>unique<\/strong>.<\/li><li>There are&nbsp;<strong>at most four<\/strong>&nbsp;edges connected to each node.<\/li><li>The graph may not be connected.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DFS<\/strong><\/h2>\n\n\n\n<p>Given time &gt;= 10 and maxTime &lt;= 100, the path length is at most 10, given at most four edges connected to each node.<br>Time complexity: O(4<sup>10<\/sup>)<br>Space complexity: O(n)<\/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 maximalPathQuality(vector<int>& values, vector<vector<int>>& edges, int maxTime) {\n    const int n = values.size();\n    vector<vector<pair<int, int>>> g(n);\n    for (const auto& e : edges) {\n      g[e[0]].emplace_back(e[1], e[2]);\n      g[e[1]].emplace_back(e[0], e[2]);\n    }\n    vector<int> seen(n);\n    int ans = 0;\n    function<void(int, int, int)> dfs = [&](int u, int t, int s) {\n      if (++seen[u] == 1) s += values[u];\n      if (u == 0) ans = max(ans, s);\n      for (auto [v, d] : g[u])\n        if (t + d <= maxTime) dfs(v, t + d, s);\n      if (--seen[u] == 0) s -= values[u];\n    };\n    dfs(0, 0, 0);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There is an&nbsp;undirected&nbsp;graph with&nbsp;n&nbsp;nodes numbered from&nbsp;0&nbsp;to&nbsp;n &#8211; 1&nbsp;(inclusive). You are given a&nbsp;0-indexed&nbsp;integer array&nbsp;values&nbsp;where&nbsp;values[i]&nbsp;is the&nbsp;value&nbsp;of the&nbsp;ith&nbsp;node. You are also given a&nbsp;0-indexed&nbsp;2D integer array&nbsp;edges, where each&nbsp;edges[j] =&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[33,217,42],"class_list":["post-8685","post","type-post","status-publish","format-standard","hentry","category-searching","tag-dfs","tag-hard","tag-search","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8685","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=8685"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8685\/revisions"}],"predecessor-version":[{"id":8688,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8685\/revisions\/8688"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}