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
#pragma GCC optimize("O2")
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define pii pair<int, int>

const int inf = 1000000007;

struct Edge{
    int to, mul, max_pass = inf;
};

vector<int> routers;
vector<vector<pii>> redges;
vector<vector<Edge>> edges, edges2;
vector<vector<int>> values_at;
vector<unordered_set<int>> inserted;
int n;

void do_step(){
    vector<vector<int>> tmp_vals(n);
    for(int i = 0; i<n; i++){
        for(auto edge : edges[i]){
            for(int j = 0; j<values_at[i].size(); j++){
                int tmp = values_at[i][j] * edge.mul;
                if(tmp > edge.max_pass) continue;
                if(tmp > routers[edge.to]) continue;
                if(inserted[edge.to].find(tmp) != inserted[edge.to].end()) continue;
                inserted[edge.to].insert(tmp);
                tmp_vals[edge.to].push_back(tmp);
            }
        }
    }

    values_at = vector<vector<int>>(tmp_vals);
}

int simulate(){
    values_at.assign(n, {});
    values_at[0].push_back(1);
    inserted.assign(n, {});
    int out = -1;
    // the edges with value one are a problem (to fix)
    for(int i = 0; i<67; i++){
        do_step();
        for(int val : values_at[n-1]){
            out = max(out, val);
        }
    }

    return out;
}

vector<bool> done;

void dfs(int sn, int cn, int min_rou = inf){
    done[cn] = 1;

    for(auto nn : edges[cn]){
        if(nn.mul != 1) continue;
        if(done[nn.to]) continue;
        int tp = min_rou;
        min_rou = min(min_rou, routers[nn.to]);
        edges[sn].push_back({nn.to, 1, min_rou});
        dfs(sn, nn.to, min_rou);
        min_rou = tp;
    }
}

void combine_ones(){
    for(int i = 0; i<n; i++){
        done.assign(n, 0);
        dfs(i, i, routers[i]);
    }
}

bool do_shortening(int cn){
    bool shortened = false;
    for(int i = 0; i<edges[cn].size(); i++){
        auto nn = edges[cn][i];
        if(nn.to == n-1) continue;
        if(edges[nn.to].size() != 1) continue;
        shortened = true;
        int tmp = edges[nn.to][0].mul * edges[cn][i].mul;
        tmp = min(tmp, inf);
        int tmin = min(routers[cn], routers[nn.to]);
        tmin = min(tmin, nn.max_pass);
        edges[cn][i] = {edges[nn.to][0].to, tmp, tmin};
    }

    return shortened;
}

void shorten_paths(){
    bool shortened = false;
    for(int it = 0; it<8; it++){
        shortened = false;
        for(int i = 0; i<n; i++){
            shortened |= do_shortening(i);
        }
    };
}

vector<bool> rdone;
void update_max(){
    priority_queue<pii, vector<pii>, greater<pii>> q;
    q.push({1, n-1});
    while(q.size()){
        auto cn = q.top();
        q.pop();
        if(rdone[cn.second]) continue;
        rdone[cn.second] = true;
        routers[cn.second] = min(routers[cn.second], routers[n-1] / cn.first);
        
        for(auto nn : redges[cn.second]){
            if(rdone[nn.first]) continue;
            int tmp = nn.second * cn.first;
            q.push({tmp, nn.first});
        }
    }
}

void solve(){
    int k;
    cin>>n>>k;

    routers.assign(n, 0);
    edges.assign(n, {});
    edges2.assign(n, {});
    redges.assign(n, {});
    for(int i = 0; i<n; i++){
        cin>>routers[i];
    }

    for(int i = 0; i<k; i++){
        int a, b, p;
        cin>>a>>b>>p;
        a--; b--;

        edges[a].push_back({b, p, inf});
        redges[b].push_back({a, p});
    }

    rdone.assign(n, 0);
    update_max();
    shorten_paths();
    combine_ones();

    cout << simulate() << endl;
}

signed main(){
    cin.tie(0)->sync_with_stdio(0);

    int t; cin>>t;

    while(t--) solve();
    return 0;
}