Leetcode 5921. 最大化一张图中的路径价值 匿名函数dfs或者带bitset的bfs
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;
}
};