1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| const int N = 1005, M = 105; using PII = pair<int, int>;
class Solution { public: unordered_map<bitset<N>, int> dist[N][M]; int maximalPathQuality(vector<int>& val, vector<vector<int>>& edge, int Mx) { int n = val.size(); vector<vector<PII>> g(n); for (auto& e : edge) { int a = e[0], b = e[1], c = e[2]; g[a].emplace_back(b, c); g[b].emplace_back(a, c); } dist[0][Mx][bitset<N>(1)] = val[0]; for (int Time = Mx; Time >= 0; Time -- ) { for (int i = 0; i < n; i ++ ) for (auto& [st, v] : dist[i][Time]) for (auto& [nxt, cost] : g[i]) { if (Time < cost) continue; if (st[nxt] == 0) { bitset<N> tmp = st; tmp[nxt] = 1; dist[nxt][Time - cost][tmp] = max(dist[nxt][Time - cost][tmp], dist[i][Time][st] + val[nxt]); } else dist[nxt][Time - cost][st] = max(dist[nxt][Time - cost][st], dist[i][Time][st]);
} } int ans = val[0]; for (int r = Mx; r >= 0; r -- ) for (auto& [_, v] : dist[0][r]) ans = max(ans, v); return ans; } };
|