Skip to content

Commit dfe1210

Browse files
authored
feat: add solutions to lc problem: No.2065 (doocs#3183)
No.2065.Maximum Path Quality of a Graph
1 parent 0de04e4 commit dfe1210

File tree

7 files changed

+522
-68
lines changed

7 files changed

+522
-68
lines changed

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

+186-23
Original file line numberDiff line numberDiff line change
@@ -100,46 +100,209 @@ tags:
100100

101101
<!-- solution:start -->
102102

103-
### 方法一
103+
### 方法一:DFS
104+
105+
我们观察题目的数据范围,可以发现从 $0$ 开始的每条合法路径的边数不超过 $\frac{\text{maxTime}}{\min(time_j)} = \frac{100}{10} = 10$ 条,并且每个节点至多有四条边,所以我们可以直接使用朴素的 DFS 暴力搜索所有合法路径。
106+
107+
我们先将图的边存储在邻接表表 $g$ 中,然后我们设计一个函数 $\text{dfs}(u, \text{cost}, \text{value})$,其中 $u$ 表示当前节点编号,而 $\text{cost}$ 和 $\text{value}$ 分别表示当前路径的花费时间和价值。另外,使用一个长度为 $n$ 的数组 $\text{vis}$ 记录每个节点是否被访问过。初始时,我们将节点 $0$ 标记为已访问。
108+
109+
函数 $\text{dfs}(u, \text{cost}, \text{value})$ 的逻辑如下:
110+
111+
- 如果当前节点编号 $u$ 等于 $0$,表示我们已经回到了起点,那么我们更新答案为 $\max(\text{ans}, \text{value})$;
112+
- 遍历当前节点 $u$ 的所有邻居节点 $v$,如果当前路径的花费时间加上边 $(u, v)$ 的时间 $t$ 不超过 $\text{maxTime}$,那么我们可以选择继续访问节点 $v$;
113+
- 如果节点 $v$ 已经被访问过,那么我们直接递归调用 $\text{dfs}(v, \text{cost} + t, \text{value})$;
114+
- 如果节点 $v$ 没有被访问过,我们标记节点 $v$ 为已访问,然后递归调用 $\text{dfs}(v, \text{cost} + t, \text{value} + \text{values}[v])$,最后恢复节点 $v$ 的访问状态。
115+
116+
在主函数中,我们调用 $\text{dfs}(0, 0, \text{values}[0])$,并返回答案 $\text{ans}$ 即可。
117+
118+
时间复杂度 $O(n + m + 4^{\frac{\text{maxTime}}{\min(time_j)}})$,空间复杂度 $O(n + m + \frac{\text{maxTime}}{\min(time_j)})$。其中 $n$ 和 $m$ 分别表示节点数和边数。
104119

105120
<!-- tabs:start -->
106121

122+
#### Python3
123+
124+
```python
125+
class Solution:
126+
def maximalPathQuality(
127+
self, values: List[int], edges: List[List[int]], maxTime: int
128+
) -> int:
129+
def dfs(u: int, cost: int, value: int):
130+
if u == 0:
131+
nonlocal ans
132+
ans = max(ans, value)
133+
for v, t in g[u]:
134+
if cost + t <= maxTime:
135+
if vis[v]:
136+
dfs(v, cost + t, value)
137+
else:
138+
vis[v] = True
139+
dfs(v, cost + t, value + values[v])
140+
vis[v] = False
141+
142+
n = len(values)
143+
g = [[] for _ in range(n)]
144+
for u, v, t in edges:
145+
g[u].append((v, t))
146+
g[v].append((u, t))
147+
vis = [False] * n
148+
vis[0] = True
149+
ans = 0
150+
dfs(0, 0, values[0])
151+
return ans
152+
```
153+
154+
#### Java
155+
156+
```java
157+
class Solution {
158+
private List<int[]>[] g;
159+
private boolean[] vis;
160+
private int[] values;
161+
private int maxTime;
162+
private int ans;
163+
164+
public int maximalPathQuality(int[] values, int[][] edges, int maxTime) {
165+
int n = values.length;
166+
g = new List[n];
167+
Arrays.setAll(g, k -> new ArrayList<>());
168+
for (var e : edges) {
169+
int u = e[0], v = e[1], t = e[2];
170+
g[u].add(new int[] {v, t});
171+
g[v].add(new int[] {u, t});
172+
}
173+
vis = new boolean[n];
174+
vis[0] = true;
175+
this.values = values;
176+
this.maxTime = maxTime;
177+
dfs(0, 0, values[0]);
178+
return ans;
179+
}
180+
181+
private void dfs(int u, int cost, int value) {
182+
if (u == 0) {
183+
ans = Math.max(ans, value);
184+
}
185+
for (var e : g[u]) {
186+
int v = e[0], t = e[1];
187+
if (cost + t <= maxTime) {
188+
if (vis[v]) {
189+
dfs(v, cost + t, value);
190+
} else {
191+
vis[v] = true;
192+
dfs(v, cost + t, value + values[v]);
193+
vis[v] = false;
194+
}
195+
}
196+
}
197+
}
198+
}
199+
```
200+
201+
#### C++
202+
203+
```cpp
204+
class Solution {
205+
public:
206+
int maximalPathQuality(vector<int>& values, vector<vector<int>>& edges, int maxTime) {
207+
int n = values.size();
208+
vector<pair<int, int>> g[n];
209+
for (auto& e : edges) {
210+
int u = e[0], v = e[1], t = e[2];
211+
g[u].emplace_back(v, t);
212+
g[v].emplace_back(u, t);
213+
}
214+
bool vis[n];
215+
memset(vis, false, sizeof(vis));
216+
vis[0] = true;
217+
int ans = 0;
218+
auto dfs = [&](auto&& dfs, int u, int cost, int value) -> void {
219+
if (u == 0) {
220+
ans = max(ans, value);
221+
}
222+
for (auto& [v, t] : g[u]) {
223+
if (cost + t <= maxTime) {
224+
if (vis[v]) {
225+
dfs(dfs, v, cost + t, value);
226+
} else {
227+
vis[v] = true;
228+
dfs(dfs, v, cost + t, value + values[v]);
229+
vis[v] = false;
230+
}
231+
}
232+
}
233+
};
234+
dfs(dfs, 0, 0, values[0]);
235+
return ans;
236+
}
237+
};
238+
```
239+
240+
#### Go
241+
242+
```go
243+
func maximalPathQuality(values []int, edges [][]int, maxTime int) (ans int) {
244+
n := len(values)
245+
g := make([][][2]int, n)
246+
for _, e := range edges {
247+
u, v, t := e[0], e[1], e[2]
248+
g[u] = append(g[u], [2]int{v, t})
249+
g[v] = append(g[v], [2]int{u, t})
250+
}
251+
vis := make([]bool, n)
252+
vis[0] = true
253+
var dfs func(u, cost, value int)
254+
dfs = func(u, cost, value int) {
255+
if u == 0 {
256+
ans = max(ans, value)
257+
}
258+
for _, e := range g[u] {
259+
v, t := e[0], e[1]
260+
if cost+t <= maxTime {
261+
if vis[v] {
262+
dfs(v, cost+t, value)
263+
} else {
264+
vis[v] = true
265+
dfs(v, cost+t, value+values[v])
266+
vis[v] = false
267+
}
268+
}
269+
}
270+
}
271+
dfs(0, 0, values[0])
272+
return
273+
}
274+
```
275+
107276
#### TypeScript
108277

109278
```ts
110279
function maximalPathQuality(values: number[], edges: number[][], maxTime: number): number {
111280
const n = values.length;
112-
let g: Array<Array<Array<number>>> = Array.from({ length: n }, v => new Array());
113-
for (let edge of edges) {
114-
let [u, v, t] = edge;
281+
const g: [number, number][][] = Array.from({ length: n }, () => []);
282+
for (const [u, v, t] of edges) {
115283
g[u].push([v, t]);
116284
g[v].push([u, t]);
117285
}
118-
let visited = new Array(n).fill(false);
286+
const vis: boolean[] = Array(n).fill(false);
287+
vis[0] = true;
119288
let ans = 0;
120-
121-
function dfs(u: number, time: number, value: number): void {
122-
// 索引0为终点
123-
if (!u) {
124-
ans = Math.max(value, ans);
289+
const dfs = (u: number, cost: number, value: number) => {
290+
if (u === 0) {
291+
ans = Math.max(ans, value);
125292
}
126-
for (let [v, dist] of g[u]) {
127-
if (time - dist >= 0) {
128-
if (!visited[v]) {
129-
visited[v] = true;
130-
dfs(v, time - dist, value + values[v]);
131-
visited[v] = false; // 回溯
293+
for (const [v, t] of g[u]) {
294+
if (cost + t <= maxTime) {
295+
if (vis[v]) {
296+
dfs(v, cost + t, value);
132297
} else {
133-
dfs(v, time - dist, value);
298+
vis[v] = true;
299+
dfs(v, cost + t, value + values[v]);
300+
vis[v] = false;
134301
}
135302
}
136303
}
137-
}
138-
139-
// 索引0为起点
140-
visited[0] = true;
141-
dfs(0, maxTime, values[0]);
142-
304+
};
305+
dfs(0, 0, values[0]);
143306
return ans;
144307
}
145308
```

0 commit comments

Comments
 (0)