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
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <vector>
#include <set>

using lli = long long int;

struct connection_info {
    int to;
    lli amplification;
};

struct vertex_info {
    lli max_throughput;
    std::vector<connection_info> connections;
};

struct search_configuration {
    int at;
    lli strength;

    auto operator<=>(const search_configuration&) const = default;
};

void do_single_case() {
    int n, m;
    scanf("%d %d\n", &n, &m);

    std::vector<vertex_info> vertices;

    for (int i = 0; i < n; i++) {
        int p;
        scanf("%d", &p);

        vertices.push_back(vertex_info{p, {}});
    }

    for (int i = 0; i < m; i++) {
        int a, b;
        lli w;
        scanf("%d %d %lld", &a, &b, &w);

        a--;
        b--;
        vertices[a].connections.push_back(connection_info{b, w});
    }

    lli best_seen = -1;

    std::set<search_configuration> visited_configs;
    std::vector<search_configuration> stack;

    stack.push_back({0, 1});

    while (!stack.empty()) {
        auto state = stack.back();
        stack.pop_back();

        auto [it, inserted] = visited_configs.insert(state);
        if (!inserted) {
            // Was already visited - just continue
            continue;
        }

        if (state.at == n - 1 && best_seen < state.strength) {
            best_seen = state.strength;
        }

        for (const auto& conn : vertices[state.at].connections) {
            const lli new_strength = state.strength * conn.amplification;
            if (new_strength <= vertices[conn.to].max_throughput) {
                stack.push_back({conn.to, new_strength});
            }
        }
    }

    printf("%lld\n", best_seen);
}

int main() {
    int t;
    scanf("%d\n", &t);

    while (t --> 0) {
        do_single_case();
    }

    return 0;
}