Leetcode 5921. Maximize the path value anonymous function dfs or bfs with bitset in a graph

Keywords: Algorithm dfs bfs

5921. Maximize the path value in a graph

meaning of the title

Here is an undirected graph with n nodes numbered from 0 to n - 1 (both included). At the same time, give you an integer array values with subscript starting from 0, where values[i] is the value of the ith node. At the same time, give you a two-dimensional integer array edges with subscript starting from 0, where edges[j] = [uj, vj, timej] indicates that there is an undirected edge between nodes uj and vj that takes timej seconds to pass. Finally, give you an integer maxTime.

Legal path refers to any path in the graph that starts from node 0 and finally returns to node 0, and the total time spent does not exceed maxTime seconds. You can access a node any number of times. The value of a legal path is defined as the sum of the values of different nodes in the path (the value of each node is calculated into the sum of values at most once).

Please return the maximum value of a legal path.

Note: each node has at most four edges connected to it.

Example 1:

Input: values = [0,32,10,43], edges = [[0,1,10],[1,2,15],[0,3,10]], maxTime = 49
Output: 75
Explanation:
One possible path is: 0 - > 1 - > 0 - > 3 - > 0. The total time spent is 10 + 10 + 10 + 10 = 40 < = 49.
The visited nodes are 0, 1 and 3, and the maximum path value is 0 + 32 + 43 = 75.
Example 2:

Input: values = [5,10,15,20], edges = [[0,1,10],[1,2,10],[0,3,10]], maxTime = 30
Output: 25
Explanation:
One possible path is: 0 - > 3 - > 0. The total time spent is 10 + 10 = 20 < = 30.
The visited nodes are 0 and 3, and the maximum path value is 5 + 20 = 25.
Example 3:

Input: values = [1,2,3,4], edges = [[0,1,10],[1,2,11],[2,3,12],[1,3,13]], maxTime = 50
Output: 7
Explanation:
One possible path is: 0 - > 1 - > 3 - > 1 - > 0. The total time spent is 10 + 13 + 13 + 10 = 46 < = 50.
The visited nodes are 0, 1 and 3, and the maximum path value is 1 + 2 + 4 = 7.
Example 4:

Input: values = [0,1,2], edges = [[1,2,10]], maxTime = 10
Output: 0
Explanation:
The only path is 0. The total time spent is 0.
The only visited node is 0, and the maximum path value is 0.

Tips:

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] all node pairs are different from each other.
Each node has at most four edges.
The graph may not be connected.

algorithm

Key points:

  • 10 <= timej, maxTime <= 100
  • Each node has at most four edges.

According to the data range of the topic, it can go up to 10 sides, which means that the number of search layers is at most 10; At the same time, the topic ensures that each node has at most four edges connected to it, so it will recurse at most 4 times each time. Therefore, the calculation amount is at most \ (4 ^ {10} \) and can be completed within the time limit.

Both dfs and bfs are OK

dfs take a look at anonymous functions

bfs uses 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;
        
    }
};

Posted by balacay on Sun, 07 Nov 2021 13:56:59 -0800