#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace __gnu_pbds;
using namespace std;
typedef gp_hash_table<int, int, hash<int>, equal_to<int>, direct_mod_range_hashing<int>, linear_probe_fn<>, hash_standard_resize_policy<hash_prime_size_policy, hash_load_check_resize_trigger<true>, true>>gp;
#define pb push_back
#define st first
#define nd second
typedef long long ll;
typedef long double ld;
const ll I = 1000'000'000'000'000'000LL;
const int II = 2'000'000'000;
const ll M = 1000'000'007LL;
const int N = 107;
struct chash { // large odd number for C
const uint64_t C = ll(4e18 * acos(0)) | 71;
ll operator()(ll x) const { return __builtin_bswap64(x*C); }
};
int dis[N];
bool vis[N];
int lim[N];
int tab[N], bst[N];
//cc_hash_table<int, int, chash> pos[N];
gp pos[N];
vector<pair<int, int>> ed[N], rev[N];
bool Check(int n, int a, int x)
{
priority_queue<pair<int, int>> q;
for(int i = 1; i <= n; ++i)
{
vis[i] = 0;
dis[i] = (II / 2) + 1;
}
dis[a] = x;
q.push(make_pair(-x, a));
while((int)q.size() > 0)
{
int v = q.top().nd;
q.pop();
if(vis[v]) continue;
vis[v] = 1;
for(int i = 0; i < (int)ed[v].size(); ++i)
{
if(dis[ed[v][i].st] / dis[v] >= ed[v][i].nd)
if(dis[v] * ed[v][i].nd < min(tab[ed[v][i].st] + 1, dis[ed[v][i].st]))
{
dis[ed[v][i].st] = dis[v] * ed[v][i].nd;
q.push(make_pair(-dis[ed[v][i].st], ed[v][i].st));
}
}
}
if(dis[n] <= tab[n]) return 1;
return 0;
}
int BS(int n, int a)
{
int p = 0, k = tab[a];
while(p < k)
{
int s = (p + k + 1) / 2;
if(Check(n, a, s))
p = s;
else
k = s - 1;
}
return p;
}
void DoLim(int n)
{
for(int i = 1; i <= n; ++i)
lim[i] = BS(n, i);
}
void Do(int n)
{
int cnt = 0;
queue<pair<int, int>> q;
pos[1][1] = 1;
bst[1] = 1;
q.push(make_pair(1, 1));
while(q.size() > 0)
{
int v = q.front().st, x = q.front().nd;
q.pop();
++cnt;
for(int i = 0; i < (int)ed[v].size(); ++i)
{
int ne = ed[v][i].st;
if(tab[ne] / x < ed[v][i].nd) continue;
int o = x * ed[v][i].nd;
if(lim[ne] < o) continue;
if(pos[ne].find(o) == pos[ne].end())
{
pos[ne][o] = 1;
bst[ne] = max(bst[ne], o);
q.push(make_pair(ne, o));
}
}
}
cerr << "R: " << cnt << "\n";
}
void Solve()
{
int n, m, a, b, c;
cin >> n >> m;
for(int i = 1; i <= n; ++i)
cin >> tab[i];
for(int i = 1; i <= m; ++i)
{
cin >> a >> b >> c;
ed[a].pb(make_pair(b, c));
rev[b].pb(make_pair(a, c));
}
DoLim(n);
Do(n);
if(bst[n] > 0)
cout << bst[n] << "\n";
else
cout << -1 << "\n";
for(int i = 1; i <= n; ++i)
{
ed[i].clear(); bst[i] = 0;
pos[i].clear();
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t; cin >> t;
while(t--)
Solve();
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> using namespace __gnu_pbds; using namespace std; typedef gp_hash_table<int, int, hash<int>, equal_to<int>, direct_mod_range_hashing<int>, linear_probe_fn<>, hash_standard_resize_policy<hash_prime_size_policy, hash_load_check_resize_trigger<true>, true>>gp; #define pb push_back #define st first #define nd second typedef long long ll; typedef long double ld; const ll I = 1000'000'000'000'000'000LL; const int II = 2'000'000'000; const ll M = 1000'000'007LL; const int N = 107; struct chash { // large odd number for C const uint64_t C = ll(4e18 * acos(0)) | 71; ll operator()(ll x) const { return __builtin_bswap64(x*C); } }; int dis[N]; bool vis[N]; int lim[N]; int tab[N], bst[N]; //cc_hash_table<int, int, chash> pos[N]; gp pos[N]; vector<pair<int, int>> ed[N], rev[N]; bool Check(int n, int a, int x) { priority_queue<pair<int, int>> q; for(int i = 1; i <= n; ++i) { vis[i] = 0; dis[i] = (II / 2) + 1; } dis[a] = x; q.push(make_pair(-x, a)); while((int)q.size() > 0) { int v = q.top().nd; q.pop(); if(vis[v]) continue; vis[v] = 1; for(int i = 0; i < (int)ed[v].size(); ++i) { if(dis[ed[v][i].st] / dis[v] >= ed[v][i].nd) if(dis[v] * ed[v][i].nd < min(tab[ed[v][i].st] + 1, dis[ed[v][i].st])) { dis[ed[v][i].st] = dis[v] * ed[v][i].nd; q.push(make_pair(-dis[ed[v][i].st], ed[v][i].st)); } } } if(dis[n] <= tab[n]) return 1; return 0; } int BS(int n, int a) { int p = 0, k = tab[a]; while(p < k) { int s = (p + k + 1) / 2; if(Check(n, a, s)) p = s; else k = s - 1; } return p; } void DoLim(int n) { for(int i = 1; i <= n; ++i) lim[i] = BS(n, i); } void Do(int n) { int cnt = 0; queue<pair<int, int>> q; pos[1][1] = 1; bst[1] = 1; q.push(make_pair(1, 1)); while(q.size() > 0) { int v = q.front().st, x = q.front().nd; q.pop(); ++cnt; for(int i = 0; i < (int)ed[v].size(); ++i) { int ne = ed[v][i].st; if(tab[ne] / x < ed[v][i].nd) continue; int o = x * ed[v][i].nd; if(lim[ne] < o) continue; if(pos[ne].find(o) == pos[ne].end()) { pos[ne][o] = 1; bst[ne] = max(bst[ne], o); q.push(make_pair(ne, o)); } } } cerr << "R: " << cnt << "\n"; } void Solve() { int n, m, a, b, c; cin >> n >> m; for(int i = 1; i <= n; ++i) cin >> tab[i]; for(int i = 1; i <= m; ++i) { cin >> a >> b >> c; ed[a].pb(make_pair(b, c)); rev[b].pb(make_pair(a, c)); } DoLim(n); Do(n); if(bst[n] > 0) cout << bst[n] << "\n"; else cout << -1 << "\n"; for(int i = 1; i <= n; ++i) { ed[i].clear(); bst[i] = 0; pos[i].clear(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while(t--) Solve(); return 0; } |
English