思路:
暴搜。
实现:
1 class Solution 2 { 3 public: 4 int res = 0; 5 vector<int> vis; 6 void dfs(int x, int time, int val, vector<int>& values, vector<vector<pair<int, int>>>& g) 7 { 8 if (x == 0) res = max(res, val); 9 int n = g[x].size(); 10 int maxn = 0; 11 for (int i = 0; i < n; i++) 12 { 13 auto tmp = g[x][i]; 14 int to = tmp.first, cost = tmp.second; 15 if (cost > time) continue; 16 if (!vis[to]) 17 { 18 vis[to] = true; 19 dfs(to, time - cost, val + values[to], values, g); 20 vis[to] = false; 21 } 22 else dfs(to, time - cost, val, values, g); 23 } 24 } 25 int maximalPathQuality(vector<int>& values, vector<vector<int>>& edges, int maxTime) 26 { 27 res = values[0]; 28 int n = values.size(); 29 vis.resize(n, 0); 30 int m = edges.size(); 31 vector<vector<pair<int, int>>> g(n, vector<pair<int, int>>()); 32 for (int i = 0; i < m; i++) 33 { 34 auto tmp = edges[i]; 35 int a = tmp[0], b = tmp[1], c = tmp[2]; 36 g[a].push_back({b, c}); 37 g[b].push_back({a, c}); 38 } 39 dfs(0, maxTime, 0, values, g); 40 return res; 41 } 42 };