#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(size(X)) #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 #define S 200 #define C 32000 int P[S]; vector<pii> V[S]; vector<pii> VR[S]; vector<pii> V2[S]; vector<pii> VR2[S]; bool dp[S][C]; int dp2[S][C]; void clear(int n) { for(int i = 1; i <= n; i++) { V[i].clear(); V2[i].clear(); VR[i].clear(); VR2[i].clear(); } for(int i = 1; i <= n; i++) { for(int j = 1; j < C; j++) { dp[i][j] = 0; dp2[i][j] = 0; } } } vector<int> qs[C]; void BFS() { qs[1].pb(1); for(int j = 1; j < C; j++) { while(!qs[j].empty()) { int x = qs[j].back(); qs[j].pop_back(); if(dp[x][j]) continue; dp[x][j] = 1; for(auto [v,d]: V[x]) { if(d * j < C && d*j <= P[v]) qs[d*j].pb(v); } } } } void dijkstra(int n) { priority_queue<pair<int,pii>> pq; pq.push({P[n], {n, 1}}); while(!pq.empty()) { int d = pq.top().st; auto [x,y] = pq.top().nd; pq.pop(); if(dp2[x][y]) continue; dp2[x][y] = d; for(auto [v,c]: VR[x]) { if(c * y < C && d/c > 0) { pq.push({min(d/c,P[v]),{v, c * y}}); } } } } int inf = 1e9; void solve() { int n,m; cin >> n >> m; clear(n); for(int i = 1; i <= n; i++) { cin >> P[i]; } for(int i = 1; i <= m; i++) { int a,b,w; cin >> a >> b >> w; if(w < C) { V[a].pb({b,w}); VR[b].pb({a,w}); } V2[a].pb({b,w}); VR2[b].pb({a,w}); } BFS(); dijkstra(n); int ans = -1; if(n == 1) ans = 1; for(int x = 1; x <= n; x++) { vector<pii> tmp; for(int j = 1; j < C-1; j++) { if(!dp2[x][j]) continue; deb(x,j,dp2[x][j]); while(!tmp.empty() && tmp.back().st <= dp2[x][j]) tmp.pop_back(); tmp.pb({dp2[x][j], j}); } if(tmp.empty()) continue; for(auto [v,d]: VR2[x]) { for(int j = 1; j <= min(C-1, inf/d + 1); j++) { if(!dp[v][j]) continue; deb(x,v,j,d); if(tmp[0].st < j*d) continue; int l = 0; int r = tmp.size()-1; while(l < r) { int sr = (l+r+1)/2; if(tmp[sr].st >= j*d) l = sr; else r = sr-1; } ans = max(ans, j*d*tmp[l].nd); } } } cout << ans << "\n"; } int32_t main() { cin.tie(0)->sync_with_stdio(0); cout << fixed << setprecision(10); int t; cin >> t; while(t--) { solve(); } }
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 | #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(size(X)) #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 #define S 200 #define C 32000 int P[S]; vector<pii> V[S]; vector<pii> VR[S]; vector<pii> V2[S]; vector<pii> VR2[S]; bool dp[S][C]; int dp2[S][C]; void clear(int n) { for(int i = 1; i <= n; i++) { V[i].clear(); V2[i].clear(); VR[i].clear(); VR2[i].clear(); } for(int i = 1; i <= n; i++) { for(int j = 1; j < C; j++) { dp[i][j] = 0; dp2[i][j] = 0; } } } vector<int> qs[C]; void BFS() { qs[1].pb(1); for(int j = 1; j < C; j++) { while(!qs[j].empty()) { int x = qs[j].back(); qs[j].pop_back(); if(dp[x][j]) continue; dp[x][j] = 1; for(auto [v,d]: V[x]) { if(d * j < C && d*j <= P[v]) qs[d*j].pb(v); } } } } void dijkstra(int n) { priority_queue<pair<int,pii>> pq; pq.push({P[n], {n, 1}}); while(!pq.empty()) { int d = pq.top().st; auto [x,y] = pq.top().nd; pq.pop(); if(dp2[x][y]) continue; dp2[x][y] = d; for(auto [v,c]: VR[x]) { if(c * y < C && d/c > 0) { pq.push({min(d/c,P[v]),{v, c * y}}); } } } } int inf = 1e9; void solve() { int n,m; cin >> n >> m; clear(n); for(int i = 1; i <= n; i++) { cin >> P[i]; } for(int i = 1; i <= m; i++) { int a,b,w; cin >> a >> b >> w; if(w < C) { V[a].pb({b,w}); VR[b].pb({a,w}); } V2[a].pb({b,w}); VR2[b].pb({a,w}); } BFS(); dijkstra(n); int ans = -1; if(n == 1) ans = 1; for(int x = 1; x <= n; x++) { vector<pii> tmp; for(int j = 1; j < C-1; j++) { if(!dp2[x][j]) continue; deb(x,j,dp2[x][j]); while(!tmp.empty() && tmp.back().st <= dp2[x][j]) tmp.pop_back(); tmp.pb({dp2[x][j], j}); } if(tmp.empty()) continue; for(auto [v,d]: VR2[x]) { for(int j = 1; j <= min(C-1, inf/d + 1); j++) { if(!dp[v][j]) continue; deb(x,v,j,d); if(tmp[0].st < j*d) continue; int l = 0; int r = tmp.size()-1; while(l < r) { int sr = (l+r+1)/2; if(tmp[sr].st >= j*d) l = sr; else r = sr-1; } ans = max(ans, j*d*tmp[l].nd); } } } cout << ans << "\n"; } int32_t main() { cin.tie(0)->sync_with_stdio(0); cout << fixed << setprecision(10); int t; cin >> t; while(t--) { solve(); } } |