Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <stack>
- #include <cstring>
- using namespace std;
- int n, m;
- vector<pair<int, int> > graph[300005];
- int dp[300005];
- int rec(int node) {
- if(dp[node] != -1) {
- return dp[node];
- }
- int result = 0;
- for(int i = 0; i < (int) graph[node].size(); i++) {
- int neigh = graph[node][i].first;
- int weight = graph[node][i].second;
- result = max(result, rec(neigh) + weight);
- }
- return dp[node] = result;
- }
- int main()
- {
- cin >> n >> m;
- for(int i = 0; i < m; i++) {
- int a, b, c;
- cin >> a >> b >> c;
- a--; b--;
- graph[a].push_back({b, c});
- }
- memset(dp, -1, sizeof dp);
- cout << rec(0) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement