#include <bits/stdc++.h> using namespace std; typedef long long ll; vector<pair<int, ll>> nbhs[111]; int p[111]; unordered_set<ll> visited[111]; ll res; void dfs(int v, ll cp, int n) { if(cp > p[v]) { return; } if(visited[v].find(cp) != visited[v].end()) { return; } visited[v].insert(cp); if(v == n) { res = max(res, cp); } for(auto u: nbhs[v]) { dfs(u.first, cp*u.second, n); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T; cin >> T; while(T--) { int n, m; cin >> n >> m; for(int i = 1; i <= n; i++) { cin >> p[i]; } for(int i = 0; i < m; i++) { int v, u, w; cin >> v >> u >> w; nbhs[v].push_back({u, w}); } res = -1; dfs(1, 1, n); cout << res << "\n"; for(int i = 1; i <= n; i++) { nbhs[i].clear(); visited[i].clear(); } } 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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; vector<pair<int, ll>> nbhs[111]; int p[111]; unordered_set<ll> visited[111]; ll res; void dfs(int v, ll cp, int n) { if(cp > p[v]) { return; } if(visited[v].find(cp) != visited[v].end()) { return; } visited[v].insert(cp); if(v == n) { res = max(res, cp); } for(auto u: nbhs[v]) { dfs(u.first, cp*u.second, n); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T; cin >> T; while(T--) { int n, m; cin >> n >> m; for(int i = 1; i <= n; i++) { cin >> p[i]; } for(int i = 0; i < m; i++) { int v, u, w; cin >> v >> u >> w; nbhs[v].push_back({u, w}); } res = -1; dfs(1, 1, n); cout << res << "\n"; for(int i = 1; i <= n; i++) { nbhs[i].clear(); visited[i].clear(); } } return 0; } |