// PA 2024 5B - Heavy metal #include <iostream> #include <string> #include <vector> #include <queue> #include <unordered_set> #include <algorithm> using namespace std; struct pair_hash { inline std::size_t operator()(const std::pair<int,int> & v) const { return v.first*31+v.second; } }; vector<vector<long long>> edgeMult; vector<vector<long long>> edgeDest; vector<long long> vCap; long long t, n, m, p, a, b, w, res, newVal; unordered_set<pair<long long, long long>, pair_hash> visited; void dfs(long long curr, long long val) { if (val > vCap[curr]) { return; } if (visited.find(make_pair(curr, val)) != visited.end()) { return; } visited.insert(make_pair(curr, val)); if (curr == n - 1 && res < val) { res = val; } for (unsigned long long i = 0; i < edgeDest[curr].size(); ++i) { newVal = val * edgeMult[curr][i]; dfs(edgeDest[curr][i], newVal); } } int main() { ios_base::sync_with_stdio(0); cin >> t; for (long long i = 0; i < t; ++i) { edgeMult.clear(); edgeDest.clear(); vCap.clear(); visited.clear(); cin >> n; cin >> m; for (long long j = 0; j < n; ++j) { cin >> p; vCap.push_back(p); edgeMult.push_back(vector<long long>()); edgeDest.push_back(vector<long long>()); } for (long long j = 0; j < m; ++j) { cin >> a; cin >> b; cin >> w; --a; --b; edgeMult[a].push_back(w); edgeDest[a].push_back(b); } res = -1; dfs(0, 1); cout << res << endl; } 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 | // PA 2024 5B - Heavy metal #include <iostream> #include <string> #include <vector> #include <queue> #include <unordered_set> #include <algorithm> using namespace std; struct pair_hash { inline std::size_t operator()(const std::pair<int,int> & v) const { return v.first*31+v.second; } }; vector<vector<long long>> edgeMult; vector<vector<long long>> edgeDest; vector<long long> vCap; long long t, n, m, p, a, b, w, res, newVal; unordered_set<pair<long long, long long>, pair_hash> visited; void dfs(long long curr, long long val) { if (val > vCap[curr]) { return; } if (visited.find(make_pair(curr, val)) != visited.end()) { return; } visited.insert(make_pair(curr, val)); if (curr == n - 1 && res < val) { res = val; } for (unsigned long long i = 0; i < edgeDest[curr].size(); ++i) { newVal = val * edgeMult[curr][i]; dfs(edgeDest[curr][i], newVal); } } int main() { ios_base::sync_with_stdio(0); cin >> t; for (long long i = 0; i < t; ++i) { edgeMult.clear(); edgeDest.clear(); vCap.clear(); visited.clear(); cin >> n; cin >> m; for (long long j = 0; j < n; ++j) { cin >> p; vCap.push_back(p); edgeMult.push_back(vector<long long>()); edgeDest.push_back(vector<long long>()); } for (long long j = 0; j < m; ++j) { cin >> a; cin >> b; cin >> w; --a; --b; edgeMult[a].push_back(w); edgeDest[a].push_back(b); } res = -1; dfs(0, 1); cout << res << endl; } return 0; } |