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
#include "bits/stdc++.h"

#define int long long
#define pi pair<long long, long long>
#define a3 array<long long, 3>

#define meow main

using namespace std;

void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> P(n);
  for(auto &p : P) cin >> p;
  vector<vector<pi>> G(n);
  while(m--) {
    int a, b, w; cin >> a >> b >> w;
    G[a-1].push_back({b-1, w});
  }

  set<pi> vis;
  vis.insert({0, 1});
  queue<pi> Q;
  Q.push({0, 1});
  while(!Q.empty()) {
    auto [v, val] = Q.front(); Q.pop();
    for(auto [w, mul] : G[v]) {
      if(P[w] >= mul*val && P[n-1] >= mul*val) {
        if(vis.count({w, mul*val}) == 0) {
          Q.push({w, mul*val});
          vis.insert({w, mul*val});
        }
      }
    }
  }

  auto [v, val] = *(--(vis.end()));

  if(v == n-1) {
    cout << val << '\n';
  } else cout << "-1\n";
}

signed meow() {
  cin.tie((ostream *)!ios::sync_with_stdio(false));
  int satori_moment;
  cin >> satori_moment;
  while (satori_moment--)
    solve();
  return 0;
}