#include <bits/stdc++.h> using i64 = long long; using u64 = unsigned long long; using u32 = unsigned; using u128 = unsigned __int128; constexpr int L = 1E5; constexpr int R = 1E4; void solve() { int n, m; std::cin >> n >> m; std::vector<int> p(n); for (int i = 0; i < n; i++) { std::cin >> p[i]; } std::vector<std::vector<std::array<int, 2>>> adj(n), adj1(n); for (int i = 0; i < m; i++) { int a, b, w; std::cin >> a >> b >> w; a--; b--; adj[a].push_back({b, w}); adj1[b].push_back({a, w}); } int ans = -1; if (n == 1) { ans = std::max(ans, 1); } std::vector left(n, std::vector<bool>(L + 1)); std::queue<std::array<int, 2>> q; left[0][1] = true; q.push({0, 1}); while (!q.empty()) { auto [x, v] = q.front(); q.pop(); for (auto [y, w] : adj[x]) { if (1LL * v * w <= std::min(p[y], L) && !left[y][v * w]) { left[y][v * w] = true; q.push({y, v * w}); } } } std::vector right(n, std::vector(R + 1, -1)); std::priority_queue<std::array<int, 3>> pq; pq.push({p[n - 1], n - 1, 1}); while (!pq.empty()) { auto [d, x, v] = pq.top(); pq.pop(); if (right[x][v] != -1) { continue; } right[x][v] = d; for (auto [y, w] : adj1[x]) { if (1LL * v * w <= R) { pq.push({std::min(d / w, p[y]), y, v * w}); } } } for (int u = 0; u < n; u++) { for (auto [v, w] : adj[u]) { for (int l = 1, r = R; l <= L; l++) { if (!left[u][l]) { continue; } while (r > 0 && right[v][r] < 1LL * l * w) { r--; } if (r > 0) { ans = std::max(ans, l * w * r); } } } } std::cout << ans << "\n"; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int t; std::cin >> t; for (int i = 1; i <= t; i++) { solve(); } return 0; }
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | #include <bits/stdc++.h> using i64 = long long; using u64 = unsigned long long; using u32 = unsigned; using u128 = unsigned __int128; constexpr int L = 1E5; constexpr int R = 1E4; void solve() { int n, m; std::cin >> n >> m; std::vector<int> p(n); for (int i = 0; i < n; i++) { std::cin >> p[i]; } std::vector<std::vector<std::array<int, 2>>> adj(n), adj1(n); for (int i = 0; i < m; i++) { int a, b, w; std::cin >> a >> b >> w; a--; b--; adj[a].push_back({b, w}); adj1[b].push_back({a, w}); } int ans = -1; if (n == 1) { ans = std::max(ans, 1); } std::vector left(n, std::vector<bool>(L + 1)); std::queue<std::array<int, 2>> q; left[0][1] = true; q.push({0, 1}); while (!q.empty()) { auto [x, v] = q.front(); q.pop(); for (auto [y, w] : adj[x]) { if (1LL * v * w <= std::min(p[y], L) && !left[y][v * w]) { left[y][v * w] = true; q.push({y, v * w}); } } } std::vector right(n, std::vector(R + 1, -1)); std::priority_queue<std::array<int, 3>> pq; pq.push({p[n - 1], n - 1, 1}); while (!pq.empty()) { auto [d, x, v] = pq.top(); pq.pop(); if (right[x][v] != -1) { continue; } right[x][v] = d; for (auto [y, w] : adj1[x]) { if (1LL * v * w <= R) { pq.push({std::min(d / w, p[y]), y, v * w}); } } } for (int u = 0; u < n; u++) { for (auto [v, w] : adj[u]) { for (int l = 1, r = R; l <= L; l++) { if (!left[u][l]) { continue; } while (r > 0 && right[v][r] < 1LL * l * w) { r--; } if (r > 0) { ans = std::max(ans, l * w * r); } } } } std::cout << ans << "\n"; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int t; std::cin >> t; for (int i = 1; i <= t; i++) { solve(); } return 0; } |