#include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i++) #define rep(i, n) fwd(i, 0, n) #define all(X) X.begin(), X.end() #define sz(X) int(X.size()) #define pb push_back #define eb emplace_back #define st first #define nd second using pii = pair<int, int>; using vi = vector<int>; using ll = long long; using ld = long double; #ifdef LOC auto SS = signal(6, [](int) { *(int *)0 = 0; }); # define DTP(x, y) \ auto operator<<(auto &o, auto a)->decltype(y, o) { \ o << "("; \ x; \ return o << ")"; \ } DTP(o << a.st << ", " << a.nd, a.nd); DTP(for (auto i : a) o << i << ", ", all(a)); void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } # define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x) #else # define deb(...) 0 #endif constexpr int SQ = 32000; constexpr int max_n = 100; int dist_to_n[max_n][SQ]; void run_dp(const vi &lim, const vector<vector<pii> > &g, int s) { vector<vi> unit_edges(sz(g)); rep(i, sz(g)) { for (auto [v, w] : g[i]) { if (w == 1) unit_edges[i].pb(v); } } rep(i, sz(g)) fill(dist_to_n[i], dist_to_n[i] + SQ, 0); dist_to_n[s][1] = lim[s]; vi component_dp(sz(g)); vi component_vis(sz(g)); priority_queue<pii> pq; fwd(d, 1, SQ) { fill(all(component_dp), 0); fill(all(component_vis), 0); rep(i, sz(g)) pq.push({min(dist_to_n[i][d], lim[i]), i}); while (sz(pq)) { auto [dist, v] = pq.top(); pq.pop(); if (component_vis[v]) continue; component_vis[v] = 1; component_dp[v] = dist; for (auto u : unit_edges[v]) { int new_dist = min(dist, lim[u]); pq.push({new_dist, u}); } } rep(i, sz(g)) { dist_to_n[i][d] = component_dp[i]; for (auto [v, w] : g[i]) { if (w > 1 && d * w < SQ) dist_to_n[v][d * w] = max(dist_to_n[v][d * w], dist_to_n[i][d] / w); } } } } bitset<SQ> vis_from_1[max_n]; void run_bfs(const vi &lim, const vector<vector<pii> > &g, int s) { vi min_lim(lim); for (auto &x : min_lim) x = min(x, SQ - 1); vector<vi> unit_edges(sz(g)); rep(i, sz(g)) { for (auto [v, w] : g[i]) { if (w == 1) unit_edges[i].pb(v); } } vi component_vis(sz(g)); vi dfs_stack; rep(i, sz(g)) vis_from_1[i].reset(); vis_from_1[s][1] = 1; fwd(d, 1, SQ) { fill(all(component_vis), 0); rep(i, sz(g)) if (vis_from_1[i][d]) dfs_stack.pb(i); while (sz(dfs_stack)) { int v = dfs_stack.back(); dfs_stack.pop_back(); if (component_vis[v]) continue; component_vis[v] = 1; for (auto u : unit_edges[v]) { if (min_lim[u] >= d) dfs_stack.pb(u); } } rep(i, sz(g)) { vis_from_1[i][d] = component_vis[i]; if (vis_from_1[i][d]) { for (auto [v, w] : g[i]) { if (w > 1 && d * w <= min_lim[v]) vis_from_1[v][d * w] = 1; } } } } } void sol() { int n, m; cin >> n >> m; vi lim(n); rep(i, n) cin >> lim[i]; vector<vector<pii> > g(n), rg(n); vector<array<int, 3> > edges = {{0, 0, 1}}; rep(i, m) { int v, u, w; cin >> v >> u >> w; v -= 1, u -= 1; if (w < SQ) { g[v].pb({u, w}); rg[u].pb({v, w}); } edges.pb({v, u, w}); } run_bfs(lim, g, 0); run_dp(lim, rg, n - 1); int ans = 0; for (auto [s, t, w] : edges) { int best_ptr = 1; int best = 0; int best_dist = 0; for (int r = SQ - 1; r >= 1; r--) { best_dist = max(best_dist, dist_to_n[t][r]); while (best_ptr < SQ && 1ll * best_ptr * w <= best_dist) { if (vis_from_1[s][best_ptr]) best = best_ptr; best_ptr += 1; } ans = max(ans, best * w * r); } } if (ans == 0) ans = -1; cout << ans << '\n'; } int32_t main() { cin.tie(0)->sync_with_stdio(0); cout << fixed << setprecision(10); constexpr int max_val = 1e9 + 7; assert((SQ - 1) * (SQ - 1) > max_val); int t; cin >> t; rep(i, t) sol(); #ifdef LOCF cout.flush(); cerr << "- - - - - - - - -\n"; (void)!system( "grep VmPeak /proc/$PPID/status | sed s/....kB/\' MB\'/1 >&2"); // 4x.kB // ....kB #endif }
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | #include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i++) #define rep(i, n) fwd(i, 0, n) #define all(X) X.begin(), X.end() #define sz(X) int(X.size()) #define pb push_back #define eb emplace_back #define st first #define nd second using pii = pair<int, int>; using vi = vector<int>; using ll = long long; using ld = long double; #ifdef LOC auto SS = signal(6, [](int) { *(int *)0 = 0; }); # define DTP(x, y) \ auto operator<<(auto &o, auto a)->decltype(y, o) { \ o << "("; \ x; \ return o << ")"; \ } DTP(o << a.st << ", " << a.nd, a.nd); DTP(for (auto i : a) o << i << ", ", all(a)); void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } # define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x) #else # define deb(...) 0 #endif constexpr int SQ = 32000; constexpr int max_n = 100; int dist_to_n[max_n][SQ]; void run_dp(const vi &lim, const vector<vector<pii> > &g, int s) { vector<vi> unit_edges(sz(g)); rep(i, sz(g)) { for (auto [v, w] : g[i]) { if (w == 1) unit_edges[i].pb(v); } } rep(i, sz(g)) fill(dist_to_n[i], dist_to_n[i] + SQ, 0); dist_to_n[s][1] = lim[s]; vi component_dp(sz(g)); vi component_vis(sz(g)); priority_queue<pii> pq; fwd(d, 1, SQ) { fill(all(component_dp), 0); fill(all(component_vis), 0); rep(i, sz(g)) pq.push({min(dist_to_n[i][d], lim[i]), i}); while (sz(pq)) { auto [dist, v] = pq.top(); pq.pop(); if (component_vis[v]) continue; component_vis[v] = 1; component_dp[v] = dist; for (auto u : unit_edges[v]) { int new_dist = min(dist, lim[u]); pq.push({new_dist, u}); } } rep(i, sz(g)) { dist_to_n[i][d] = component_dp[i]; for (auto [v, w] : g[i]) { if (w > 1 && d * w < SQ) dist_to_n[v][d * w] = max(dist_to_n[v][d * w], dist_to_n[i][d] / w); } } } } bitset<SQ> vis_from_1[max_n]; void run_bfs(const vi &lim, const vector<vector<pii> > &g, int s) { vi min_lim(lim); for (auto &x : min_lim) x = min(x, SQ - 1); vector<vi> unit_edges(sz(g)); rep(i, sz(g)) { for (auto [v, w] : g[i]) { if (w == 1) unit_edges[i].pb(v); } } vi component_vis(sz(g)); vi dfs_stack; rep(i, sz(g)) vis_from_1[i].reset(); vis_from_1[s][1] = 1; fwd(d, 1, SQ) { fill(all(component_vis), 0); rep(i, sz(g)) if (vis_from_1[i][d]) dfs_stack.pb(i); while (sz(dfs_stack)) { int v = dfs_stack.back(); dfs_stack.pop_back(); if (component_vis[v]) continue; component_vis[v] = 1; for (auto u : unit_edges[v]) { if (min_lim[u] >= d) dfs_stack.pb(u); } } rep(i, sz(g)) { vis_from_1[i][d] = component_vis[i]; if (vis_from_1[i][d]) { for (auto [v, w] : g[i]) { if (w > 1 && d * w <= min_lim[v]) vis_from_1[v][d * w] = 1; } } } } } void sol() { int n, m; cin >> n >> m; vi lim(n); rep(i, n) cin >> lim[i]; vector<vector<pii> > g(n), rg(n); vector<array<int, 3> > edges = {{0, 0, 1}}; rep(i, m) { int v, u, w; cin >> v >> u >> w; v -= 1, u -= 1; if (w < SQ) { g[v].pb({u, w}); rg[u].pb({v, w}); } edges.pb({v, u, w}); } run_bfs(lim, g, 0); run_dp(lim, rg, n - 1); int ans = 0; for (auto [s, t, w] : edges) { int best_ptr = 1; int best = 0; int best_dist = 0; for (int r = SQ - 1; r >= 1; r--) { best_dist = max(best_dist, dist_to_n[t][r]); while (best_ptr < SQ && 1ll * best_ptr * w <= best_dist) { if (vis_from_1[s][best_ptr]) best = best_ptr; best_ptr += 1; } ans = max(ans, best * w * r); } } if (ans == 0) ans = -1; cout << ans << '\n'; } int32_t main() { cin.tie(0)->sync_with_stdio(0); cout << fixed << setprecision(10); constexpr int max_val = 1e9 + 7; assert((SQ - 1) * (SQ - 1) > max_val); int t; cin >> t; rep(i, t) sol(); #ifdef LOCF cout.flush(); cerr << "- - - - - - - - -\n"; (void)!system( "grep VmPeak /proc/$PPID/status | sed s/....kB/\' MB\'/1 >&2"); // 4x.kB // ....kB #endif } |