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
#include <cstdio>
#include <vector>
#include <unordered_set>

typedef long long LL;

using namespace std;

void doit() {
  int n, m; scanf(" %d %d", &n, &m);
  vector<int> p(n);
  for (int i=0; i<n; ++i) scanf(" %d", &p[i]);
  vector< vector< pair<int, int> > > e(n);
  for (int i=0; i<m; ++i) {
    int u, v, w;
    scanf("%d %d %d", &u, &v, &w); --u; --v;
    e[u].push_back(make_pair(v, w));
  }

  LL N = 2000000000;
  unordered_set<LL> visited;
  int q = 0;
  vector<pair<int, int> > queue;

  queue.push_back(make_pair(0, 1));
  visited.insert(1);

  while (q < queue.size()) {
    const auto last = queue[q++];
    for (const auto& f: e[last.first]) {
      LL newW = LL(last.second) * f.second;

//      printf("Sprawdzam %d -> %lld (%d * %d)\n", f.first, newW, last.second, f.second);

      if (p[f.first] < newW) continue;
      if (visited.contains(N*f.first + newW)) continue;

//      printf("Ok ^\n");

      queue.push_back(make_pair(f.first, (int)newW));
      visited.insert(N*f.first + newW);
    }
  }

  int best = -1;
  for (const auto& el: queue) if (el.first == n-1) best = max(best, el.second);
  printf("%d\n", best);
}

int main() {
  int t; scanf(" %d", &t);
  while (t--) doit();
}