#include <bits/stdc++.h> using namespace std; #define _DEBUG #ifdef _DEBUG template<typename T1, typename T2> auto& operator<<(ostream& o, pair<T1, T2> a) { return o << "(" << a.first << ", " << a.second << ")"; } template<typename T, typename OS> auto& operator<<(OS& o, T a) { o << "{"; for(auto b : a) o << b << ", "; return o << "}"; } #define dbg(x...) cerr << "[" #x "]: ", [](auto... args) { ((cerr << args << ", "),...) << "\n"; }(x) #else #define dbg(...) #endif #define sz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define F first #define S second using ll = long long; using ld = long double; using pll = pair<ll,ll>; using vi = vector<int>; using Graph = vector<vector<pair<int, int>>>; const vector<int>& rec(int i, int movesLeft, const Graph& graph, const vector<int>& maxv, vector<vector<vector<int>>>& dp) { if(dp[i][movesLeft] == vector<int>({-1})) { dp[i][movesLeft].pop_back(); for(auto [nei, w] : graph[i]) { const auto& vs = rec(nei, movesLeft - 1, graph, maxv, dp); for(auto v : vs) { if(static_cast<ll>(v) * w > static_cast<ll>(maxv[i])) { break; } dp[i][movesLeft].push_back(v * w); } } if(i == 0) { dp[i][movesLeft].push_back(1); } sort(dp[i][movesLeft].begin(), dp[i][movesLeft].end()); dp[i][movesLeft].erase(unique(dp[i][movesLeft].begin(), dp[i][movesLeft].end()), dp[i][movesLeft].end()); } return dp[i][movesLeft]; } bool canGo(int i, const Graph& graph, vector<bool>& visited) { if(i == 0) { return true; } visited[i] = true; for(auto [nei, w] : graph[i]) { if(!visited[nei] && canGo(nei, graph, visited)) { return true; } } return false; } void solve() { int n, m; cin >> n >> m; Graph graph(n); vector<int> maxv(n); for(auto& v : maxv) { cin >> v; } for(auto& v : maxv) { v = min(v, maxv.back()); } int minw = 2000000000; int noOne = 1; for(int i = 0; i < m; i++) { int a, b, w; cin >> a >> b >> w; a--; b--; graph[b].push_back({a, w}); if(w != 1) { minw = min(minw, w); } else { noOne++; } } vector<bool> visited(n); if(!canGo(0, graph, visited)) { cout << -1; return; } if(minw == 2000000000) { cout << 1; return; } int moves = 1; ll tmp = 1; while(tmp < maxv.back()) { tmp *= minw; moves++; } moves *= noOne; moves += 3; // lol vector<vector<vector<int>>> dp(n, vector<vector<int>>(moves + 1, vector<int>({-1}))); for(int i = 0; i < n; i++) { dp[i][0] = {}; } dp[0][0] = {1}; rec(n - 1, moves, graph, maxv, dp); int ans = -1; for(auto vect : dp[n - 1]) { if(!vect.empty()) { ans = max(ans, vect.back()); } } cout << ans; } int main() { cin.tie(0)->sync_with_stdio(0); int t = 1; cin >> t; while(t--) { solve(); cout << "\n"; } 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 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 | #include <bits/stdc++.h> using namespace std; #define _DEBUG #ifdef _DEBUG template<typename T1, typename T2> auto& operator<<(ostream& o, pair<T1, T2> a) { return o << "(" << a.first << ", " << a.second << ")"; } template<typename T, typename OS> auto& operator<<(OS& o, T a) { o << "{"; for(auto b : a) o << b << ", "; return o << "}"; } #define dbg(x...) cerr << "[" #x "]: ", [](auto... args) { ((cerr << args << ", "),...) << "\n"; }(x) #else #define dbg(...) #endif #define sz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define F first #define S second using ll = long long; using ld = long double; using pll = pair<ll,ll>; using vi = vector<int>; using Graph = vector<vector<pair<int, int>>>; const vector<int>& rec(int i, int movesLeft, const Graph& graph, const vector<int>& maxv, vector<vector<vector<int>>>& dp) { if(dp[i][movesLeft] == vector<int>({-1})) { dp[i][movesLeft].pop_back(); for(auto [nei, w] : graph[i]) { const auto& vs = rec(nei, movesLeft - 1, graph, maxv, dp); for(auto v : vs) { if(static_cast<ll>(v) * w > static_cast<ll>(maxv[i])) { break; } dp[i][movesLeft].push_back(v * w); } } if(i == 0) { dp[i][movesLeft].push_back(1); } sort(dp[i][movesLeft].begin(), dp[i][movesLeft].end()); dp[i][movesLeft].erase(unique(dp[i][movesLeft].begin(), dp[i][movesLeft].end()), dp[i][movesLeft].end()); } return dp[i][movesLeft]; } bool canGo(int i, const Graph& graph, vector<bool>& visited) { if(i == 0) { return true; } visited[i] = true; for(auto [nei, w] : graph[i]) { if(!visited[nei] && canGo(nei, graph, visited)) { return true; } } return false; } void solve() { int n, m; cin >> n >> m; Graph graph(n); vector<int> maxv(n); for(auto& v : maxv) { cin >> v; } for(auto& v : maxv) { v = min(v, maxv.back()); } int minw = 2000000000; int noOne = 1; for(int i = 0; i < m; i++) { int a, b, w; cin >> a >> b >> w; a--; b--; graph[b].push_back({a, w}); if(w != 1) { minw = min(minw, w); } else { noOne++; } } vector<bool> visited(n); if(!canGo(0, graph, visited)) { cout << -1; return; } if(minw == 2000000000) { cout << 1; return; } int moves = 1; ll tmp = 1; while(tmp < maxv.back()) { tmp *= minw; moves++; } moves *= noOne; moves += 3; // lol vector<vector<vector<int>>> dp(n, vector<vector<int>>(moves + 1, vector<int>({-1}))); for(int i = 0; i < n; i++) { dp[i][0] = {}; } dp[0][0] = {1}; rec(n - 1, moves, graph, maxv, dp); int ans = -1; for(auto vect : dp[n - 1]) { if(!vect.empty()) { ans = max(ans, vect.back()); } } cout << ans; } int main() { cin.tie(0)->sync_with_stdio(0); int t = 1; cin >> t; while(t--) { solve(); cout << "\n"; } return 0; } |