#include "bits/stdc++.h"
using namespace std;
#define rep(i, b, e) for (int i = (b); i <= (e); i++)
#define per(i, b, e) for (int i = (e); i >= (b); i--)
#define FOR(i, b, e) rep(i, b, (e)-1)
#define SZ(x) int(x.size())
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define st first
#define nd second
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
auto& operator<<(auto& o, pair<auto, auto> p) {
return o << "(" << p.st << ", " << p.nd << ")";
}
auto operator<<(auto& o, auto x) -> decltype(end(x), o) {
o << "{";
int i = 0;
for (auto e : x)
o << ", " + 2 * !i++ << e;
return o << "}";
}
#ifdef LOCAL
#define deb(x...) \
cerr << "[" #x "]: ", [](auto... $) { \
((cerr << $ << "; "), ...) << endl; \
}(x)
#else
#define deb(...)
#endif
vector<pair<pair<int, int>, int>> G[103], R[103];
unordered_set<int> vis[103];
map<int, int> best[103]; //najlepszy wynik dla konkretnej wartosci
int one_path[103][103];
const int PRUNING = 200000;
int from_s[103][PRUNING + 1];
int p[103];
int n;
int go(int v, int val) {
// if (dist >= 16)
// return -1;
// chce zrobic maks ile moge ale <= val, to i tak nie moge dac wiecej niz p[v]
// cerr << "Wszedlem " << v << " " << val << "\n";
val = min(val, p[v]);
if (val <= PRUNING) {
// cerr << "Zwracam od razu\n";
return from_s[v][val];
}
// moze juz mialem odpowiedz dla jakiegos wiekszego val ktora jest mniejsza od
// aktualnego, wtedy ja zwroc
auto it = best[v].lower_bound(val);
if (it != best[v].end() && it->second <= val) {
return it->second;
}
if (vis[v].count(val)) {
return -1;
}
vis[v].insert(val);
int ans = -1;
for (auto [e, max_p] : R[v]) {
auto [u, w] = e;
int x = val / w;
if (!x)
break;
if (x == 1 && one_path[1][u]) {
ans = max(ans, w);
continue;
}
int a = go(u, min(x, max_p));
ans = max(ans, a * w);
}
if (v == 1)
ans = max(ans, 1);
return (best[v][val] = ans);
}
void napraw(vector<pair<pair<int, int>, int>>& g) {
sort(all(g));
for (int j = 0; j < g.size();) {
int idx = j;
while (idx < SZ(g) && g[j].first == g[idx].first) {
g[j].second = max(g[idx].second, g[j].second);
idx++;
}
idx = j;
while (idx < SZ(g) && g[j].first == g[idx].first) {
g[idx].second = g[j].second;
idx++;
}
j = idx;
}
g.erase(unique(all(g)), g.end());
sort(all(g), [](const pair<pii, int>& p1, const pair<pii, int>& p2) {
if (p1.first.second == p2.first.second) {
return p1.second > p2.second;
}
return p1.first.second < p2.first.second;
});
}
void oznacz(int v, int val) {
// cerr << "Wchodze do " << v << " " << val << "\n";
if (val > PRUNING || from_s[v][val] == val) {
// cerr << "Wychodzem\n";
return;
}
from_s[v][val] = val;
for (auto [e, min_p] : G[v]) {
auto [u, w] = e;
// cerr << "Moze wejde do " << u << " " << val * w << "?\n";
if ((ll)val * w <= p[u] && val <= min_p) {
oznacz(u, val * w);
}
}
}
double go_time;
double oznacz_time;
void solve() {
int m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> p[i];
G[i].clear();
R[i].clear();
vis[i].clear();
best[i].clear();
for (int j = 1; j <= n; j++) {
one_path[i][j] = 0;
}
for (int j = 1; j <= PRUNING; j++)
from_s[i][j] = -1;
}
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
if (u == v && w == 1)
continue;
if ((v < n && w != 1) || v == n) {
G[u].push_back({{v, w}, p[v]});
R[v].push_back({{u, w}, p[u]});
}
if (w == 1) {
one_path[u][v] = p[v];
}
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (min(one_path[i][k], one_path[k][j]) > one_path[i][j]) {
one_path[i][j] = min(one_path[i][k], one_path[k][j]);
}
}
}
}
vector<pair<pii, int>> nowe_g[103];
vector<pair<pii, int>> nowe_r[103];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == j)
continue;
if (one_path[i][j]) {
// cerr << "Moge z " << i << " do " << j << " jesli leci mniej niz "
// << one_path[i][j] << "\n";
int s = SZ(G[j]);
for (int k = 0; k < s; k++) {
auto [e, min_pp] = G[j][k];
auto [u, w] = e;
if (w == 1) {
continue;
}
nowe_g[i].push_back({{u, w}, min(one_path[i][j], min_pp)});
nowe_r[u].push_back({{i, w}, min(one_path[i][j], min_pp)});
int min_p =
min({one_path[i][j], one_path[u][i] / w, p[u] / w, min_pp});
// cerr << "Jest krawedz do " << u << ", " << w << ", min_p =" << min_p
// << "\n";
if (min_p) {
nowe_g[i].push_back({{i, w}, min_p});
nowe_r[i].push_back({{i, w}, min_p});
}
}
}
}
}
for (int i = 1; i < n; i++) {
if (one_path[i][n]) {
G[i].push_back({{n, 1}, one_path[i][n]});
R[n].push_back({{i, 1}, one_path[i][n]});
}
}
for (int i = 1; i <= n; i++) {
G[i].insert(G[i].end(), all(nowe_g[i]));
R[i].insert(R[i].end(), all(nowe_r[i]));
napraw(G[i]);
napraw(R[i]);
for (auto [e, max_p] : R[i]) {
auto [u, w] = e;
// cerr << "Moge sie dostac do " << i << " z " << u << " i pomnozyc o " << w
// << " o ile sygnal w " << u << " nie przekraczal " << max_p << "\n";
}
}
// auto start = chrono::high_resolution_clock::now();
oznacz(1, 1);
for (int i = 1; i <= n; i++) {
// cout << i << ": ";
for (int j = 2; j <= PRUNING; j++) {
from_s[i][j] = max(from_s[i][j - 1], from_s[i][j]);
// cout << from_s[i][j] << " ";
}
// cout << "\n";
}
// auto end = chrono::high_resolution_clock::now();
// chrono::duration<double, std::milli> duration = end - start;
// oznacz_time = duration.count();
// start = chrono::high_resolution_clock::now();
go(n, p[n]);
// // cerr << "# stanow: " << vis.size() << "\n";
if (best[n].empty()) {
cout << from_s[n][PRUNING] << "\n";
} else {
cout << best[n].rbegin()->second << "\n";
}
// end = chrono::high_resolution_clock::now();
// duration = end - start;
// go_time = duration.count();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie();
double worst_case = 0;
double go_worst_case = 0;
double oznacz_worst_case = 0;
int t;
cin >> t;
while (t--) {
// auto start = chrono::high_resolution_clock::now();
solve();
// auto end = chrono::high_resolution_clock::now();
// chrono::duration<double, std::milli> duration = end - start;
// worst_case = max(worst_case, duration.count());
// go_worst_case = max(go_time, go_worst_case);
// oznacz_worst_case = max(oznacz_time, oznacz_worst_case);
// cerr << "Execution time: " << duration.count() << " ms" << std::endl;
// cerr << "Worst case time: " << worst_case << " ms" << std::endl;
// cerr << "Go worst case time: " << go_worst_case << " ms" << std::endl;
// cerr << "Oznacz worst case time: " << oznacz_worst_case << " ms"
// << std::endl;
}
}
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | #include "bits/stdc++.h" using namespace std; #define rep(i, b, e) for (int i = (b); i <= (e); i++) #define per(i, b, e) for (int i = (e); i >= (b); i--) #define FOR(i, b, e) rep(i, b, (e)-1) #define SZ(x) int(x.size()) #define all(x) x.begin(), x.end() #define pb push_back #define mp make_pair #define st first #define nd second using ll = long long; using vi = vector<int>; using pii = pair<int, int>; auto& operator<<(auto& o, pair<auto, auto> p) { return o << "(" << p.st << ", " << p.nd << ")"; } auto operator<<(auto& o, auto x) -> decltype(end(x), o) { o << "{"; int i = 0; for (auto e : x) o << ", " + 2 * !i++ << e; return o << "}"; } #ifdef LOCAL #define deb(x...) \ cerr << "[" #x "]: ", [](auto... $) { \ ((cerr << $ << "; "), ...) << endl; \ }(x) #else #define deb(...) #endif vector<pair<pair<int, int>, int>> G[103], R[103]; unordered_set<int> vis[103]; map<int, int> best[103]; //najlepszy wynik dla konkretnej wartosci int one_path[103][103]; const int PRUNING = 200000; int from_s[103][PRUNING + 1]; int p[103]; int n; int go(int v, int val) { // if (dist >= 16) // return -1; // chce zrobic maks ile moge ale <= val, to i tak nie moge dac wiecej niz p[v] // cerr << "Wszedlem " << v << " " << val << "\n"; val = min(val, p[v]); if (val <= PRUNING) { // cerr << "Zwracam od razu\n"; return from_s[v][val]; } // moze juz mialem odpowiedz dla jakiegos wiekszego val ktora jest mniejsza od // aktualnego, wtedy ja zwroc auto it = best[v].lower_bound(val); if (it != best[v].end() && it->second <= val) { return it->second; } if (vis[v].count(val)) { return -1; } vis[v].insert(val); int ans = -1; for (auto [e, max_p] : R[v]) { auto [u, w] = e; int x = val / w; if (!x) break; if (x == 1 && one_path[1][u]) { ans = max(ans, w); continue; } int a = go(u, min(x, max_p)); ans = max(ans, a * w); } if (v == 1) ans = max(ans, 1); return (best[v][val] = ans); } void napraw(vector<pair<pair<int, int>, int>>& g) { sort(all(g)); for (int j = 0; j < g.size();) { int idx = j; while (idx < SZ(g) && g[j].first == g[idx].first) { g[j].second = max(g[idx].second, g[j].second); idx++; } idx = j; while (idx < SZ(g) && g[j].first == g[idx].first) { g[idx].second = g[j].second; idx++; } j = idx; } g.erase(unique(all(g)), g.end()); sort(all(g), [](const pair<pii, int>& p1, const pair<pii, int>& p2) { if (p1.first.second == p2.first.second) { return p1.second > p2.second; } return p1.first.second < p2.first.second; }); } void oznacz(int v, int val) { // cerr << "Wchodze do " << v << " " << val << "\n"; if (val > PRUNING || from_s[v][val] == val) { // cerr << "Wychodzem\n"; return; } from_s[v][val] = val; for (auto [e, min_p] : G[v]) { auto [u, w] = e; // cerr << "Moze wejde do " << u << " " << val * w << "?\n"; if ((ll)val * w <= p[u] && val <= min_p) { oznacz(u, val * w); } } } double go_time; double oznacz_time; void solve() { int m; cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> p[i]; G[i].clear(); R[i].clear(); vis[i].clear(); best[i].clear(); for (int j = 1; j <= n; j++) { one_path[i][j] = 0; } for (int j = 1; j <= PRUNING; j++) from_s[i][j] = -1; } for (int i = 0; i < m; i++) { int u, v, w; cin >> u >> v >> w; if (u == v && w == 1) continue; if ((v < n && w != 1) || v == n) { G[u].push_back({{v, w}, p[v]}); R[v].push_back({{u, w}, p[u]}); } if (w == 1) { one_path[u][v] = p[v]; } } for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (min(one_path[i][k], one_path[k][j]) > one_path[i][j]) { one_path[i][j] = min(one_path[i][k], one_path[k][j]); } } } } vector<pair<pii, int>> nowe_g[103]; vector<pair<pii, int>> nowe_r[103]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (i == j) continue; if (one_path[i][j]) { // cerr << "Moge z " << i << " do " << j << " jesli leci mniej niz " // << one_path[i][j] << "\n"; int s = SZ(G[j]); for (int k = 0; k < s; k++) { auto [e, min_pp] = G[j][k]; auto [u, w] = e; if (w == 1) { continue; } nowe_g[i].push_back({{u, w}, min(one_path[i][j], min_pp)}); nowe_r[u].push_back({{i, w}, min(one_path[i][j], min_pp)}); int min_p = min({one_path[i][j], one_path[u][i] / w, p[u] / w, min_pp}); // cerr << "Jest krawedz do " << u << ", " << w << ", min_p =" << min_p // << "\n"; if (min_p) { nowe_g[i].push_back({{i, w}, min_p}); nowe_r[i].push_back({{i, w}, min_p}); } } } } } for (int i = 1; i < n; i++) { if (one_path[i][n]) { G[i].push_back({{n, 1}, one_path[i][n]}); R[n].push_back({{i, 1}, one_path[i][n]}); } } for (int i = 1; i <= n; i++) { G[i].insert(G[i].end(), all(nowe_g[i])); R[i].insert(R[i].end(), all(nowe_r[i])); napraw(G[i]); napraw(R[i]); for (auto [e, max_p] : R[i]) { auto [u, w] = e; // cerr << "Moge sie dostac do " << i << " z " << u << " i pomnozyc o " << w // << " o ile sygnal w " << u << " nie przekraczal " << max_p << "\n"; } } // auto start = chrono::high_resolution_clock::now(); oznacz(1, 1); for (int i = 1; i <= n; i++) { // cout << i << ": "; for (int j = 2; j <= PRUNING; j++) { from_s[i][j] = max(from_s[i][j - 1], from_s[i][j]); // cout << from_s[i][j] << " "; } // cout << "\n"; } // auto end = chrono::high_resolution_clock::now(); // chrono::duration<double, std::milli> duration = end - start; // oznacz_time = duration.count(); // start = chrono::high_resolution_clock::now(); go(n, p[n]); // // cerr << "# stanow: " << vis.size() << "\n"; if (best[n].empty()) { cout << from_s[n][PRUNING] << "\n"; } else { cout << best[n].rbegin()->second << "\n"; } // end = chrono::high_resolution_clock::now(); // duration = end - start; // go_time = duration.count(); } int main() { ios_base::sync_with_stdio(false); cin.tie(); double worst_case = 0; double go_worst_case = 0; double oznacz_worst_case = 0; int t; cin >> t; while (t--) { // auto start = chrono::high_resolution_clock::now(); solve(); // auto end = chrono::high_resolution_clock::now(); // chrono::duration<double, std::milli> duration = end - start; // worst_case = max(worst_case, duration.count()); // go_worst_case = max(go_time, go_worst_case); // oznacz_worst_case = max(oznacz_time, oznacz_worst_case); // cerr << "Execution time: " << duration.count() << " ms" << std::endl; // cerr << "Worst case time: " << worst_case << " ms" << std::endl; // cerr << "Go worst case time: " << go_worst_case << " ms" << std::endl; // cerr << "Oznacz worst case time: " << oznacz_worst_case << " ms" // << std::endl; } } |
English