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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#pragma GCC optimize("O2")
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;

using ll = long long;
using ld = long double;

#define int ll
#define sz(x) ((int)(x).size())

using pii = pair<int,int>;
using tii = tuple<int,int,int>;

const int inf = 1e9 + 5;
const int nmax = 105;
const int bulan = 2048;

vector<pii> g[nmax], tr[nmax];
vector<pii> jumps[nmax][nmax];
int cap[nmax];
int mdist[nmax][bulan];
int connected[nmax][nmax];

int n, m;

void initmdist() {   
   priority_queue<pii, vector<pii>, greater<pii>> heap;
   for(int i = 0; i < n; i++) for(int j = 0; j < bulan; j++) mdist[i][j] = inf;
   heap.emplace(1, n - 1);
   mdist[n - 1][0] = 1;
   while(!heap.empty()) {
      auto [c, node] = heap.top();
      heap.pop();
      if([&]() {
         for(int i = 0; i < bulan; i++) 
            if(mdist[node][i] == c) return 0;
         return 1;
      }()) continue;
      
      for(auto [x, e] : tr[node]) {
         if(c * e < mdist[x][bulan - 1] && !(
         [&]() {
            for(int i = 0; i < bulan; i++)
      
               if(mdist[x][i] == c * e) return 1;
            return 0;
         }())) {
            int p = bulan - 1;
            while(p > 0 && mdist[x][p] > c * e) mdist[x][p] = mdist[x][p - 1], p--;
            if(p == 0) {
               if(mdist[x][0] > c * e) mdist[x][0] = c * e;
               else mdist[x][1] = c * e;
            }
            else 
               mdist[x][p + 1] = c * e;
            heap.emplace(c * e, x);
         }
      }
   }
   return;
}
vector<int> cst;
int occ[nmax], flag;

void drawjumps(int node, int limit, int store) {
   if(cap[node] < cst[limit]) return;
   if(occ[node] == flag) return;
   occ[node] = flag;
   for(auto [x, e] : g[node]) if(e != 1) jumps[store][limit].emplace_back(x, e);
   for(auto [x, e] : g[node]) {
      if(e == 1)
         drawjumps(x, limit, store);
   }
}

void connect(int node, int limit) {
   if(cap[node] < cst[limit]) return;
   if(connected[node][limit]) return;
   connected[node][limit] = 1;
   for(auto [x, e] : tr[node]) {
      if(e == 1)
         connect(x, limit);
   }
}

int bestrez = -1;

unordered_set<int> states[nmax];

void bkt(int node, int C) {
   if(C > cap[node]) return;
   if(states[node].count(C)) return;
   
   int availability = lower_bound(all(cst), C) - begin(cst);
   if(availability >= sz(cst)) return;
   
   if(connected[node][availability]) {
      bestrez = max(bestrez, C);
   }
   
   states[node].emplace(C);
   
   if([&]() {
      for(int i = 0; i < bulan; i++) {
         if(mdist[node][i] * C > cap[n - 1]) return 1;
         if(mdist[node][i] * C > bestrez) return 0;
      }
      return 0;
   }()) return;
   
   if(mdist[node][(bulan + 1) / 2] * C > cap[n - 1])
      for(auto [x, e] : jumps[node][availability] | views::reverse)
         bkt(x, e * C);
   else
      for(auto [x, e] : jumps[node][availability])
         bkt(x, e * C);
   return;
}

//void bkt(int node, int C) {
   //if(C > cap[node]) return;
   //if(states[node].count(C)) return;
   
   //int availability = lower_bound(all(cst), C) - begin(cst);
   //if(availability >= sz(cst)) return;
   
   //if(node == n - 1)
      //bestrez = max(bestrez, C);
   
   //states[node].emplace(C);
   
   //for(auto [x, e] : g[node])
      //bkt(x, e * C);
   //return;
//}

void testcase() {
   cin >> n >> m;
   
   {
      bestrez = -1;
      fill(occ, occ + n + 1, 0);
      flag = 2;
      cst.clear();
      for(int i = 0; i < n; i++) {
         g[i].clear();
         tr[i].clear();
         states[i].clear();
         for(int j = 0; j < n; j++) connected[i][j] = 0, jumps[i][j].clear();
      }
   }
   
   for(int i = 0; i < n; i++) cin >> cap[i], cst.emplace_back(cap[i]);
   sort(all(cst));
   
   for(int i = 0, x, y, e; i < m; i++) {
      cin >> x >> y >> e;
      --x;
      --y;
      g[x].emplace_back(y, e);
      tr[y].emplace_back(x, e);
   }
   
   initmdist();
   //for(int i = 0; i < n; i++) {
      //for(int j = 0; j < bulan; j++) cerr << mdist[i][j] << ' ';
      //cerr << '\n';
   //}
   
   for(int i = 0; i < n; i++) {
      for(int node = 0; node < n; node++) {
         flag++;
         drawjumps(node, i, node);
         sort(all(jumps[node][i]), [&](auto a, auto b) { return a.second > b.second; });
         jumps[node][i].erase(unique(all(jumps[node][i])), end(jumps[node][i]));
      }
      connect(n - 1, i);
   }
   
   //cerr << "muie\n";
   if(mdist[0][0] >= inf) {
      cout << "-1\n";
      return;
   }
   
   bkt(0, 1);
   cout << bestrez << '\n';
}

signed main() {
   cin.tie(0) -> sync_with_stdio(0);
   int T;
   cin >> T;
   while(T--) testcase();
   
}

/**
      Binecuvanteaza Doamne Ukraina.
--
*/