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

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

const int N = 205;
vector<vector<pii>> g(N);
int capacity[N];
set<int> dist[N];

void bfs(int v, int n){
    dist[v].insert(1);
    queue<pii> q;
    int strength = 1;
    q.push({strength, v});

    while(!q.empty()){
        strength = q.front().first;
        v = q.front().second;
        q.pop();
        for(auto &[u,w] : g[v]){
            if(ll(w)*strength<=capacity[u] && dist[u].find(w*strength)==dist[u].end()){
                dist[u].insert(w*strength);
                q.push({w*strength, u});
            }
        }
    }
}

void solve(){
    int n,m;
    cin >> n >> m;
    for(int i=1; i<=n; ++i)
        cin >> capacity[i];
    for(int i=0,a,b,w; i<m; ++i){
        cin >> a >> b >> w;
        g[a].push_back({b,w});
    }
    bfs(1,n);
    if(dist[n].empty()){
        cout << "-1\n";
    }else{
        cout << (*dist[n].rbegin()) << "\n";
    }
    for(int i=1; i<=n; ++i){
        g[i].clear();
        dist[i].clear();
    }

}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    int t;
    cin >> t;
    while(t--) solve();
}