LeetCode 2065. Maximize the path value in a graph

Keywords: Algorithm leetcode

Here's an undirected picture for you   Figure with n   Nodes, node number from 0   To n - 1   (both included). And give you a subscript from 0   Starting integer array   values  , among   values[i]   It's number I   Value of nodes  . And give you a subscript from 0   Starting 2D integer array   edges  , among   edges[j] = [uj, vj, timej]   Represents a node   UJ and   vj   There is a need   timej   The undirected edge that can pass in seconds. Finally, I'll give you an integer   maxTime  .

Legal path   It refers to any slave node in the graph   0   Start and finally return to node 0  , And the total time spent shall not exceed   maxTime is a path of seconds. You can access a node any number of times. The value of a legal path   Defined as different nodes in the path   Sum of values   (the value of each node is up to   Included in the sum of values (once).

Please return the maximum number of a legal path   Value.

Note: each node can be at most   There are four   The edge is 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  .
There are at most four per node   Side.
The graph may not be connected.

Source: LeetCode
Link: https://leetcode-cn.com/problems/maximum-path-quality-of-a-graph
 

Solution:

The problem seems complex, but it is actually simple. You can search directly through dfs, because there will not be many searches according to the data range of timej and maxTime. I first get the shortest path through bfs, and then use dfs to search.

class Solution {
	vector<vector<pair<int, int>>> gra;
	vector<int> d;

	struct cmp
	{
		bool operator()(pair<int, int> a, pair<int, int> b)
		{
			return a.second > b.second;
		}
	};

	void bfs(int s, int n)
	{
		priority_queue < pair<int, int>, vector < pair<int, int> >, cmp >q;
		q.push(make_pair(s, 0));
		d[s] = 0;
		vector<bool> vis(n);
		vis[s] = true;
		while (!q.empty())
		{
			
			int u = q.top().first, dd = q.top().second;
			q.pop();
			for (auto &edg : gra[u])
			{
				int v = edg.first, t = edg.second;
				if (!vis[v])
				{
					vis[v] = true;
					d[v] = dd + t;
					q.push(make_pair(v, d[v]));
				}
			}
		}
		return;
	}

	void dfs(int u, int &ans, int maxTime, int time, int value, vector<int> &values, vector<bool> &vis)
	{
		if (time + d[u] > maxTime) //Time + if the time to return to 0 is greater than the maximum time, return
			return;
		if (value > ans)
			ans = value;
		for (auto &edg : gra[u])
		{
			if (!vis[edg.first])//for the first time
			{
				vis[edg.first] = true;
				dfs(edg.first, ans, maxTime, time + edg.second, value + values[edg.first], values, vis);
				vis[edg.first] = false;
			}
			else                //Not the first time
			{
				dfs(edg.first, ans, maxTime, time + edg.second, value , values, vis);
			}
			
		}
	}

public:
	int maximalPathQuality(vector<int>& values, vector<vector<int>>& edges, int maxTime) {
		int n = values.size();
		gra.resize(n, vector<pair<int, int>>());
		d.resize(n, INT_MAX);
		for (vector<int> edg : edges)
		{
			int u = edg[0], v = edg[1], t = edg[2];
			gra[u].push_back(make_pair(v, t));
			gra[v].push_back(make_pair(u, t));
		}
		bfs(0, n);
		int ans = 0;
		vector<bool> vis(n);
        vis[0]=true;
		dfs(0, ans, maxTime, 0, values[0], values, vis);
		return ans;
	}
};

Posted by pixelsoul on Mon, 08 Nov 2021 12:43:36 -0800