#include <iostream>
#include <unordered_map>
#include <vector>
int64_t resolve_cycles(int64_t w, const std::vector<std::pair<int64_t, int64_t>> &cycles, int32_t i) {
// std::cerr << std::string(i, ' ') << "resolve (" << w << " " << cycles[i].first << " " << cycles[i].second << ")\n";
if (i + 1 == cycles.size()) {
while (w * cycles[i].first <= cycles[i].second) {
w *= cycles[i].first;
}
// std::cerr << std::string(i, ' ') << "= " << w << "\n";
return w;
}
int64_t best = resolve_cycles(w, cycles, i + 1);
while (w * cycles[i].first <= cycles[i].second) {
w *= cycles[i].first;
best = std::max(best, resolve_cycles(w, cycles, i + 1));
}
// std::cerr << std::string(i, ' ') << "= " << best << "\n";
return best;
}
void collect_cycles(int32_t n, int32_t m, const std::vector<int64_t> &p, const std::unordered_multimap<int32_t, std::pair<int32_t, int64_t>> &x, std::vector<int32_t> &seen_i, std::vector<int64_t> &seen_w, std::vector<int32_t> &path, std::unordered_multimap<int32_t, std::pair<int64_t, int64_t>> &all_cycles, int32_t cur_i, int64_t cur_w) {
if (cur_w > p[cur_i]) {
return;
}
seen_i[cur_i] = path.size();
seen_w[cur_i] = cur_w;
path.push_back(cur_i);
auto range = x.equal_range(cur_i);
for (auto it = range.first; it != range.second; ++it) {
auto val = it->second;
if (seen_i[val.first] == -1) {
continue;
}
// std::cerr << "cycle: " << cur_i << " -> " << val.first << "\n";
int64_t mult = val.second;
int64_t max = p[val.first];
for (int32_t i = seen_i[val.first]; i + 1 < path.size(); ++i) {
int32_t from = path[i];
int32_t to = path[i + 1];
// std::cerr << " segment: " << seen_w[to] << " / " << seen_w[from] << "\n";
int64_t m1 = seen_w[to] / seen_w[from];
mult *= m1;
max *= m1;
max = std::min(max, p[to]);
if (mult > max) {
goto skip;
}
}
// std::cerr << " is: " << mult << " " << max << "\n";
if (mult > 1) {
all_cycles.insert({val.first, {mult, max}});
}
skip:;
}
for (auto it = range.first; it != range.second; ++it) {
auto val = it->second;
if (seen_i[val.first] != -1) {
continue;
}
collect_cycles(n, m, p, x, seen_i, seen_w, path, all_cycles, val.first, cur_w * val.second);
}
seen_i[cur_i] = -1;
path.pop_back();
}
int64_t goo(int32_t n, int32_t m, const std::vector<int64_t> &p, const std::unordered_multimap<int32_t, std::pair<int32_t, int64_t>> &x, std::vector<int32_t> &seen_i, std::vector<int64_t> &seen_w, std::vector<int32_t> &path, const std::unordered_multimap<int32_t, std::pair<int64_t, int64_t>> &all_cycles, std::vector<std::pair<int64_t, int64_t>> &path_cycles, int32_t cur_i, int64_t cur_w) {
if (cur_w > p[cur_i]) {
return -1;
}
seen_i[cur_i] = path.size();
seen_w[cur_i] = cur_w;
path.push_back(cur_i);
// std::cerr << "goo at " << cur_i << "\n";
for (auto &c: path_cycles) {
c.second = std::min(c.second, p[cur_i]);
// std::cerr << " have cycle " << c.first << " " << c.second << "\n";
}
int32_t cycles_to_drop = 0;
auto range1 = all_cycles.equal_range(cur_i);
for (auto it = range1.first; it != range1.second; ++it) {
path_cycles.push_back(it->second);
// std::cerr << " have cycle " << path_cycles.back().first << " " << path_cycles.back().second << "\n";
cycles_to_drop += 1;
}
int64_t best = -1;
if (cur_i == n - 1) {
best = path_cycles.empty() ? cur_w : resolve_cycles(cur_w, path_cycles, 0);
} else {
auto range = x.equal_range(cur_i);
for (auto it = range.first; it != range.second; ++it) {
auto val = it->second;
if (seen_i[val.first] != -1) {
continue;
}
for (auto &c: path_cycles) {
c.second *= val.second;
}
best = std::max(best, goo(n, m, p, x, seen_i, seen_w, path, all_cycles, path_cycles, val.first, cur_w * val.second));
for (auto &c: path_cycles) {
c.second /= val.second;
}
}
}
for (int32_t i = 0; i < cycles_to_drop; ++i) {
path_cycles.pop_back();
}
seen_i[cur_i] = -1;
path.pop_back();
return best;
}
void go() {
int32_t n, m;
std::cin >> n >> m;
std::vector<int64_t> p(n);
for (int32_t i = 0; i < n; ++i) {
std::cin >> p[i];
}
std::unordered_multimap<int32_t, std::pair<int32_t, int64_t>> x;
for (int32_t i = 0; i < m; ++i) {
int32_t a, b;
int64_t w;
std::cin >> a >> b >> w;
x.insert({a - 1, {b - 1, w}});
}
std::vector<int32_t> seen_i(n, -1);
std::vector<int64_t> seen_w(n, -1);
std::vector<int32_t> path;
std::unordered_multimap<int32_t, std::pair<int64_t, int64_t>> all_cycles;
std::vector<std::pair<int64_t, int64_t>> path_cycles;
collect_cycles(n, m, p, x, seen_i, seen_w, path, all_cycles, 0, 1);
std::cout << goo(n, m, p, x, seen_i, seen_w, path, all_cycles, path_cycles, 0, 1) << "\n";
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int32_t t;
std::cin >> t;
for (int32_t i = 0; i < t; ++i) {
go();
}
}
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 | #include <iostream> #include <unordered_map> #include <vector> int64_t resolve_cycles(int64_t w, const std::vector<std::pair<int64_t, int64_t>> &cycles, int32_t i) { // std::cerr << std::string(i, ' ') << "resolve (" << w << " " << cycles[i].first << " " << cycles[i].second << ")\n"; if (i + 1 == cycles.size()) { while (w * cycles[i].first <= cycles[i].second) { w *= cycles[i].first; } // std::cerr << std::string(i, ' ') << "= " << w << "\n"; return w; } int64_t best = resolve_cycles(w, cycles, i + 1); while (w * cycles[i].first <= cycles[i].second) { w *= cycles[i].first; best = std::max(best, resolve_cycles(w, cycles, i + 1)); } // std::cerr << std::string(i, ' ') << "= " << best << "\n"; return best; } void collect_cycles(int32_t n, int32_t m, const std::vector<int64_t> &p, const std::unordered_multimap<int32_t, std::pair<int32_t, int64_t>> &x, std::vector<int32_t> &seen_i, std::vector<int64_t> &seen_w, std::vector<int32_t> &path, std::unordered_multimap<int32_t, std::pair<int64_t, int64_t>> &all_cycles, int32_t cur_i, int64_t cur_w) { if (cur_w > p[cur_i]) { return; } seen_i[cur_i] = path.size(); seen_w[cur_i] = cur_w; path.push_back(cur_i); auto range = x.equal_range(cur_i); for (auto it = range.first; it != range.second; ++it) { auto val = it->second; if (seen_i[val.first] == -1) { continue; } // std::cerr << "cycle: " << cur_i << " -> " << val.first << "\n"; int64_t mult = val.second; int64_t max = p[val.first]; for (int32_t i = seen_i[val.first]; i + 1 < path.size(); ++i) { int32_t from = path[i]; int32_t to = path[i + 1]; // std::cerr << " segment: " << seen_w[to] << " / " << seen_w[from] << "\n"; int64_t m1 = seen_w[to] / seen_w[from]; mult *= m1; max *= m1; max = std::min(max, p[to]); if (mult > max) { goto skip; } } // std::cerr << " is: " << mult << " " << max << "\n"; if (mult > 1) { all_cycles.insert({val.first, {mult, max}}); } skip:; } for (auto it = range.first; it != range.second; ++it) { auto val = it->second; if (seen_i[val.first] != -1) { continue; } collect_cycles(n, m, p, x, seen_i, seen_w, path, all_cycles, val.first, cur_w * val.second); } seen_i[cur_i] = -1; path.pop_back(); } int64_t goo(int32_t n, int32_t m, const std::vector<int64_t> &p, const std::unordered_multimap<int32_t, std::pair<int32_t, int64_t>> &x, std::vector<int32_t> &seen_i, std::vector<int64_t> &seen_w, std::vector<int32_t> &path, const std::unordered_multimap<int32_t, std::pair<int64_t, int64_t>> &all_cycles, std::vector<std::pair<int64_t, int64_t>> &path_cycles, int32_t cur_i, int64_t cur_w) { if (cur_w > p[cur_i]) { return -1; } seen_i[cur_i] = path.size(); seen_w[cur_i] = cur_w; path.push_back(cur_i); // std::cerr << "goo at " << cur_i << "\n"; for (auto &c: path_cycles) { c.second = std::min(c.second, p[cur_i]); // std::cerr << " have cycle " << c.first << " " << c.second << "\n"; } int32_t cycles_to_drop = 0; auto range1 = all_cycles.equal_range(cur_i); for (auto it = range1.first; it != range1.second; ++it) { path_cycles.push_back(it->second); // std::cerr << " have cycle " << path_cycles.back().first << " " << path_cycles.back().second << "\n"; cycles_to_drop += 1; } int64_t best = -1; if (cur_i == n - 1) { best = path_cycles.empty() ? cur_w : resolve_cycles(cur_w, path_cycles, 0); } else { auto range = x.equal_range(cur_i); for (auto it = range.first; it != range.second; ++it) { auto val = it->second; if (seen_i[val.first] != -1) { continue; } for (auto &c: path_cycles) { c.second *= val.second; } best = std::max(best, goo(n, m, p, x, seen_i, seen_w, path, all_cycles, path_cycles, val.first, cur_w * val.second)); for (auto &c: path_cycles) { c.second /= val.second; } } } for (int32_t i = 0; i < cycles_to_drop; ++i) { path_cycles.pop_back(); } seen_i[cur_i] = -1; path.pop_back(); return best; } void go() { int32_t n, m; std::cin >> n >> m; std::vector<int64_t> p(n); for (int32_t i = 0; i < n; ++i) { std::cin >> p[i]; } std::unordered_multimap<int32_t, std::pair<int32_t, int64_t>> x; for (int32_t i = 0; i < m; ++i) { int32_t a, b; int64_t w; std::cin >> a >> b >> w; x.insert({a - 1, {b - 1, w}}); } std::vector<int32_t> seen_i(n, -1); std::vector<int64_t> seen_w(n, -1); std::vector<int32_t> path; std::unordered_multimap<int32_t, std::pair<int64_t, int64_t>> all_cycles; std::vector<std::pair<int64_t, int64_t>> path_cycles; collect_cycles(n, m, p, x, seen_i, seen_w, path, all_cycles, 0, 1); std::cout << goo(n, m, p, x, seen_i, seen_w, path, all_cycles, path_cycles, 0, 1) << "\n"; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int32_t t; std::cin >> t; for (int32_t i = 0; i < t; ++i) { go(); } } |
English