5921. 最大化一張圖中的路徑價值
題意
給你一張 無向 圖,圖中有 n 個節點,節點編號從 0 到 n - 1 (都包括)。同時給你一個下標從 0 開始的整數數組 values ,其中 values[i] 是第 i 個節點的 價值 。同時給你一個下標從 0 開始的二維整數數組 edges ,其中 edges[j] = [uj, vj, timej] 表示節點 uj 和 vj 之間有一條需要 timej 秒才能通過的無向邊。最后,給你一個整數 maxTime 。
合法路徑 指的是圖中任意一條從節點 0 開始,最終回到節點 0 ,且花費的總時間 不超過 maxTime 秒的一條路徑。你可以訪問一個節點任意次。一條合法路徑的 價值 定義為路徑中 不同節點 的價值 之和 (每個節點的價值 至多 算入價值總和中一次)。
請你返回一條合法路徑的 最大 價值。
注意:每個節點 至多 有 四條 邊與之相連。
示例 1:
輸入:values = [0,32,10,43], edges = [[0,1,10],[1,2,15],[0,3,10]], maxTime = 49
輸出:75
解釋:
一條可能的路徑為:0 -> 1 -> 0 -> 3 -> 0 。總花費時間為 10 + 10 + 10 + 10 = 40 <= 49 。
訪問過的節點為 0 ,1 和 3 ,最大路徑價值為 0 + 32 + 43 = 75 。
示例 2:
輸入:values = [5,10,15,20], edges = [[0,1,10],[1,2,10],[0,3,10]], maxTime = 30
輸出:25
解釋:
一條可能的路徑為:0 -> 3 -> 0 。總花費時間為 10 + 10 = 20 <= 30 。
訪問過的節點為 0 和 3 ,最大路徑價值為 5 + 20 = 25 。
示例 3:
輸入:values = [1,2,3,4], edges = [[0,1,10],[1,2,11],[2,3,12],[1,3,13]], maxTime = 50
輸出:7
解釋:
一條可能的路徑為:0 -> 1 -> 3 -> 1 -> 0 。總花費時間為 10 + 13 + 13 + 10 = 46 <= 50 。
訪問過的節點為 0 ,1 和 3 ,最大路徑價值為 1 + 2 + 4 = 7 。
示例 4:
輸入:values = [0,1,2], edges = [[1,2,10]], maxTime = 10
輸出:0
解釋:
唯一一條路徑為 0 。總花費時間為 0 。
唯一訪問過的節點為 0 ,最大路徑價值為 0 。
提示:
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
[uj, vj] 所有節點對 互不相同 。
每個節點 至多有四條 邊。
圖可能不連通。
算法
關鍵點:
10 <= timej, maxTime <= 100
每個節點至多有四條邊。
根據題目的數據范圍,至多能走 10 條邊,這意味着搜索的層數至多為 10 ;同時,題目保證每個節點至多有 四條邊與之相連,因此每次搜索時至多會遞歸 4 次。因此計算量至多為 \(4^{10}\) ,可以在時限內跑完。
dfs和bfs都可以
dfs看一下匿名函數
bfs使用了bitset
class Solution {
public:
int maximalPathQuality(vector<int>& values, vector<vector<int>>& edges, int maxTime) {
int n = values.size();
long long s = 0;
for(int i = 0; i < n; i++)
s+= values[i];
vector<int> st(n, 0);
vector<vector<pair<int,int>>> g(n, vector<pair<int,int>>(0));
for(auto x: edges){
g[x[0]].push_back({x[1],x[2]});
g[x[1]].push_back({x[0],x[2]});
}
int res = 0;
function<void(int,int,int)> dfs =[&](int u, int time, int tot){
if(time > maxTime) return;
st[u]++;
if(st[u] == 1) tot += values[u];
if(u == 0){
res = max(res, tot);
}
for(auto x: g[u]){
dfs(x.first, time + x.second, tot);
}
st[u]--;
};
dfs(0, 0, 0);
return res;
}
};
typedef long long LL;
const int N = 1010;
class Solution {
public:
int maximalPathQuality(vector<int>& values, vector<vector<int>>& edges, int maxTime) {
int n = values.size();
queue<pair<int,pair<int,pair<LL, bitset<N>>>>> q;
vector<vector<pair<int, int>>> g(n, vector<pair<int, int>>(0));
for(auto x: edges){
g[x[0]].push_back({x[1], x[2]});
g[x[1]].push_back({x[0], x[2]});
}
LL res = 0;
bitset<N> s;
s.reset();
s.set(0, 1);
q.push({0, {0, {values[0], s}}});
while(!q.empty()){
auto t = q.front();
q.pop();
int time = t.first;
int x = t.second.first;
LL tot = t.second.second.first;
bitset<N> s = t.second.second.second;
if(x == 0){
res = max(res, tot);
}
for(auto y : g[x]){
if(time + y.second <= maxTime){
if(s[y.first] == 1){
q.push({time + y.second, {y.first, {tot, s}}});
}else{
s.set(y.first, 1);
q.push({time + y.second, {y.first, {tot + values[y.first], s}}});
s.set(y.first, 0);
}
}
}
}
return res;
}
};