返回介绍

solution / 2000-2099 / 2065.Maximum Path Quality of a Graph / README_EN

发布于 2024-06-17 01:03:11 字数 5201 浏览 0 评论 0 收藏 0

2065. Maximum Path Quality of a Graph

中文文档

Description

There is an undirected graph with n nodes numbered from 0 to n - 1 (inclusive). You are given a 0-indexed integer array values where values[i] is the value of the ith node. You are also given a 0-indexed 2D integer array edges, where each edges[j] = [uj, vj, timej] indicates that there is an undirected edge between the nodes uj and vj, and it takes timej seconds to travel between the two nodes. Finally, you are given an integer maxTime.

A valid path in the graph is any path that starts at node 0, ends at node 0, and takes at most maxTime seconds to complete. You may visit the same node multiple times. The quality of a valid path is the sum of the values of the unique nodes visited in the path (each node's value is added at most once to the sum).

Return _the maximum quality of a valid path_.

Note: There are at most four edges connected to each node.

 

Example 1:

Input: values = [0,32,10,43], edges = [[0,1,10],[1,2,15],[0,3,10]], maxTime = 49
Output: 75
Explanation:
One possible path is 0 -> 1 -> 0 -> 3 -> 0. The total time taken is 10 + 10 + 10 + 10 = 40 <= 49.
The nodes visited are 0, 1, and 3, giving a maximal path quality of 0 + 32 + 43 = 75.

Example 2:

Input: values = [5,10,15,20], edges = [[0,1,10],[1,2,10],[0,3,10]], maxTime = 30
Output: 25
Explanation:
One possible path is 0 -> 3 -> 0. The total time taken is 10 + 10 = 20 <= 30.
The nodes visited are 0 and 3, giving a maximal path quality of 5 + 20 = 25.

Example 3:

Input: values = [1,2,3,4], edges = [[0,1,10],[1,2,11],[2,3,12],[1,3,13]], maxTime = 50
Output: 7
Explanation:
One possible path is 0 -> 1 -> 3 -> 1 -> 0. The total time taken is 10 + 13 + 13 + 10 = 46 <= 50.
The nodes visited are 0, 1, and 3, giving a maximal path quality of 1 + 2 + 4 = 7.

 

Constraints:

  • n == values.length
  • 1 <= n <= 1000
  • 0 <= values[i] <= 108
  • 0 <= edges.length <= 2000
  • edges[j].length == 3
  • 0 <= uj < vj <= n - 1
  • 10 <= timej, maxTime <= 100
  • All the pairs [uj, vj] are unique.
  • There are at most four edges connected to each node.
  • The graph may not be connected.

Solutions

Solution 1

function maximalPathQuality(values: number[], edges: number[][], maxTime: number): number {
  const n = values.length;
  let g: Array<Array<Array<number>>> = Array.from({ length: n }, v => new Array());
  for (let edge of edges) {
    let [u, v, t] = edge;
    g[u].push([v, t]);
    g[v].push([u, t]);
  }
  let visited = new Array(n).fill(false);
  let ans = 0;

  function dfs(u: number, time: number, value: number): void {
    // 索引0为终点
    if (!u) {
      ans = Math.max(value, ans);
    }
    for (let [v, dist] of g[u]) {
      if (time - dist >= 0) {
        if (!visited[v]) {
          visited[v] = true;
          dfs(v, time - dist, value + values[v]);
          visited[v] = false; // 回溯
        } else {
          dfs(v, time - dist, value);
        }
      }
    }
  }

  // 索引0为起点
  visited[0] = true;
  dfs(0, maxTime, values[0]);

  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文