#ifndef LOCAL #pragma GCC optimize("O3,unroll-loops") #endif #include <bits/stdc++.h> #define fi first #define se second #define pn printf("\n") #define ssize(x) int(x.size()) #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #define bitcount(x) __builtin_popcount(x) #define clz(x) __builtin_clz(x) #define ctz(x) __builtin_ctz(x) #define mp make_pair //~ #define r(x) resize(x) //~ #define rf(x, c) resize(x, c) using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<int, ll> pil; typedef pair<ll, int> pli; typedef pair<ll, ll> pll; typedef double db; typedef long double ldb; #define V vector int inf = 2e09; ll infll = 2e18; int mod = (1<<23)*119+1; void answer(){ int n, m; scanf("%d%d", &n, &m); V<int> p(n+1, 0); for(int i = 1; i <= n; ++i) scanf("%d", &p[i]); struct e{ int x, c; e(){} e(int xx, int cc) : x(xx), c(cc) {} }; V<V<e>> g(n+1); for(int i = 1; i <= m; ++i){ int a, b, c; scanf("%d%d%d", &a, &b, &c); g[b].emplace_back(a, c); // odwracamy krawedzie } struct pq_item{ int x, prop, res; pq_item(){} pq_item(int xx, int pp, int rr) : x(xx), prop(pp), res(rr) {} bool operator <(const pq_item& a) const{ return res < a.res; } }; V<unordered_map<int, int>> dist(n+1); priority_queue<pq_item> pq; dist[n][p[n]] = 1, pq.emplace(n, p[n], 1); pq_item tmp; int x, prop, prop2, res; int result = -1; while(ssize(pq)){ tmp = pq.top(), pq.pop(); x = tmp.x, prop = tmp.prop, res = tmp.res; if(x == 1) result = max(result, res); if(res < dist[x][prop]) continue; for(e &u : g[x]) if(u.c <= prop) { prop2 = min(prop/u.c, p[u.x]); if(dist[u.x][prop2] < res*u.c) dist[u.x][prop2] = res*u.c, pq.emplace(u.x, prop2, res*u.c); } } printf("%d\n", result); } int main(){ int T = 1; scanf("%d", &T); //~ ios_base::sync_with_stdio(0); cin.tie(0); cin >> T; for(++T; --T; ) answer(); 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 | #ifndef LOCAL #pragma GCC optimize("O3,unroll-loops") #endif #include <bits/stdc++.h> #define fi first #define se second #define pn printf("\n") #define ssize(x) int(x.size()) #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #define bitcount(x) __builtin_popcount(x) #define clz(x) __builtin_clz(x) #define ctz(x) __builtin_ctz(x) #define mp make_pair //~ #define r(x) resize(x) //~ #define rf(x, c) resize(x, c) using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<int, ll> pil; typedef pair<ll, int> pli; typedef pair<ll, ll> pll; typedef double db; typedef long double ldb; #define V vector int inf = 2e09; ll infll = 2e18; int mod = (1<<23)*119+1; void answer(){ int n, m; scanf("%d%d", &n, &m); V<int> p(n+1, 0); for(int i = 1; i <= n; ++i) scanf("%d", &p[i]); struct e{ int x, c; e(){} e(int xx, int cc) : x(xx), c(cc) {} }; V<V<e>> g(n+1); for(int i = 1; i <= m; ++i){ int a, b, c; scanf("%d%d%d", &a, &b, &c); g[b].emplace_back(a, c); // odwracamy krawedzie } struct pq_item{ int x, prop, res; pq_item(){} pq_item(int xx, int pp, int rr) : x(xx), prop(pp), res(rr) {} bool operator <(const pq_item& a) const{ return res < a.res; } }; V<unordered_map<int, int>> dist(n+1); priority_queue<pq_item> pq; dist[n][p[n]] = 1, pq.emplace(n, p[n], 1); pq_item tmp; int x, prop, prop2, res; int result = -1; while(ssize(pq)){ tmp = pq.top(), pq.pop(); x = tmp.x, prop = tmp.prop, res = tmp.res; if(x == 1) result = max(result, res); if(res < dist[x][prop]) continue; for(e &u : g[x]) if(u.c <= prop) { prop2 = min(prop/u.c, p[u.x]); if(dist[u.x][prop2] < res*u.c) dist[u.x][prop2] = res*u.c, pq.emplace(u.x, prop2, res*u.c); } } printf("%d\n", result); } int main(){ int T = 1; scanf("%d", &T); //~ ios_base::sync_with_stdio(0); cin.tie(0); cin >> T; for(++T; --T; ) answer(); return 0; } |