Advertisement
Josif_tepe

Untitled

Nov 13th, 2022
693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <stack>
  5. #include <cstring>
  6. using namespace std;
  7. int n, m;
  8. vector<pair<int, int> >  graph[300005];
  9. int dp[300005];
  10. int rec(int node) {
  11.     if(dp[node] != -1) {
  12.         return dp[node];
  13.     }
  14.     int result = 0;
  15.     for(int i = 0; i < (int) graph[node].size(); i++) {
  16.         int neigh = graph[node][i].first;
  17.         int weight = graph[node][i].second;
  18.         result = max(result, rec(neigh) + weight);
  19.     }
  20.     return dp[node] = result;
  21. }
  22. int main()
  23. {
  24.     cin >> n >> m;
  25.     for(int i = 0; i < m; i++) {
  26.         int a, b, c;
  27.         cin >> a >> b >> c;
  28.         a--; b--;
  29.         graph[a].push_back({b, c});
  30.     }
  31.     memset(dp, -1, sizeof dp);
  32.     cout << rec(0) << endl;
  33.     return 0;
  34. }
  35.  
  36.  
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement