fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. ios::sync_with_stdio(false);
  6. cin.tie(nullptr);
  7.  
  8. int T;
  9. if (!(cin >> T)) return 0;
  10. while (T--) {
  11. int n, m;
  12. cin >> n >> m;
  13. vector<long long> p(n + 1);
  14. for (int i = 1; i <= n; ++i) cin >> p[i];
  15.  
  16. vector<vector<pair<int,long long>>> g(n + 1);
  17. for (int i = 0; i < m; ++i) {
  18. int a, b;
  19. long long w;
  20. cin >> a >> b >> w;
  21. g[a].push_back({b, w});
  22. }
  23.  
  24. vector<unordered_set<long long>> seen(n + 1);
  25. queue<pair<int,long long>> q;
  26.  
  27. long long ans = -1;
  28.  
  29. if (1 <= p[1]) {
  30. q.push({1, 1});
  31. seen[1].insert(1);
  32. }
  33.  
  34. while (!q.empty()) {
  35. auto [u, val] = q.front();
  36. q.pop();
  37.  
  38. if (u == n) ans = max(ans, val);
  39.  
  40. for (auto [v, w] : g[u]) {
  41. // kiểm tra val * w <= p[v] mà không bị tràn
  42. if (val > p[v] / w) continue;
  43. long long newVal = val * w;
  44. if (!seen[v].count(newVal)) {
  45. seen[v].insert(newVal);
  46. q.push({v, newVal});
  47. }
  48. }
  49. }
  50.  
  51. cout << ans << "\n";
  52. }
  53.  
  54. return 0;
  55. }
Success #stdin #stdout 0.01s 5288KB
stdin
4
2 3
25 100
1 1 2
1 2 3
2 1 10
3 5
50 80 110
1 1 2
2 2 3
2 3 1
1 2 1
3 3 5
2 2
4 4
1 1 2
1 2 1
2 1
10 10
1 2 999999
stdout
48
100
4
-1