-
Notifications
You must be signed in to change notification settings - Fork 749
/
2. DFSwithStoppingCondition.cpp
41 lines (31 loc) · 1.13 KB
/
2. DFSwithStoppingCondition.cpp
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
//https://leetcode.com/problems/maximum-path-quality-of-a-graph/
/*
In this problem we get to know a differet variety of DFS with a stopping condition
of time rather than all visited.
*/
void dfs(int &ans ,map<int,vector<vector<int>>>&m,int maxTime,int time,vector<int>&visited,int score, vector<int>&values,int node) {
if(time>maxTime) return ;
if(visited[node]==0){
score+=values[node];
}
visited[node]++;
if(node==0) {
ans=max(ans,score);
}
for(auto &nbr:m[node]) {
dfs(ans,m,maxTime,time+nbr[1],visited,score,values,nbr[0]);
}
visited[node]--;
}
int maximalPathQuality(vector<int>& values, vector<vector<int>>& edges, int maxTime) {
map<int,vector<vector<int>>> m;
int n=values.size();
for(auto &a: edges) {
m[a[0]].push_back({a[1],a[2]});
m[a[1]].push_back({a[0],a[2]});
}
vector<int> visited(n,0);
int ans=0;
dfs(ans,m,maxTime,0,visited,0,values,0);
return ans;
}