fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5. #define pi pair<int,int>
  6. #define nn '\n'
  7.  
  8. const int N = 200000 + 5;
  9. const int K = 30; // log2(1e9) < 30
  10. const long long NEG = -1e18;
  11.  
  12. int n, m;
  13. vector<pi> adj[N];
  14. vector<vector<long long>> dp;
  15.  
  16. int32_t main() {
  17. ios::sync_with_stdio(false);
  18. cin.tie(nullptr);
  19.  
  20. cin >> n >> m;
  21. for (int i = 1; i <= m; i++) {
  22. int x, y, w;
  23. cin >> x >> y >> w;
  24. adj[x].push_back({y, w});
  25. }
  26.  
  27. dp.assign(n + 1, vector<long long>(K + 1, NEG));
  28.  
  29. // (profit, node, steps)
  30. priority_queue<tuple<long long,int,int>> pq;
  31.  
  32. dp[1][0] = 0;
  33. pq.push({0, 1, 0});
  34.  
  35. while (!pq.empty()) {
  36. auto [profit, u, k] = pq.top();
  37. pq.pop();
  38.  
  39. if (profit < dp[u][k]) continue;
  40.  
  41. for (auto [v, w] : adj[u]) {
  42. if (k + 1 > K) continue;
  43.  
  44. long long gain = w >> k;
  45. if (gain == 0) continue;
  46.  
  47. if (dp[v][k + 1] < profit + gain) {
  48. dp[v][k + 1] = profit + gain;
  49. pq.push({dp[v][k + 1], v, k + 1});
  50. }
  51. }
  52. }
  53.  
  54. long long ans = 0;
  55. for (int u = 1; u <= n; u++)
  56. for (int k = 0; k <= K; k++)
  57. ans = max(ans, dp[u][k]);
  58.  
  59. cout << ans << nn;
  60. return 0;
  61. }
  62.  
Success #stdin #stdout 0.01s 8264KB
stdin
5 5
1 3 2
3 4 1
4 2 1
2 1 2
3 5 6
stdout
5