#include <cstdio>
#include <cstdint>
#include <vector>
#include <queue>
#include <stack>
#include <set>
using namespace std;
const int64_t INF = 1000000008LL;
typedef struct {
int64_t min_mul_to_exit;
int capacity;
bool ach1, achn;
vector<pair<int, int> > adj_out;
vector<pair<int, int> > adj_in;
vector<pair<int, int> > adj_out2;
vector<pair<int, int> > adj_in2;
set<int64_t> powers;
} router_t;
typedef struct {
int from, to;
int64_t power;
} transmitter_t;
void test_case() {
int n, m;
scanf("%d %d", &n, &m);
vector<router_t> V;
V.reserve(n + 8);
for (int i = 0; i < n; ++i) {
router_t r;
r.ach1 = false;
r.achn = false;
r.min_mul_to_exit = (i == n - 1) ? 1 : INF;
scanf("%d", &r.capacity);
V.push_back(r);
}
for (int i = 0; i < m; ++i) {
int a, b, w;
scanf("%d %d %d", &a, &b, &w);
V[a - 1].adj_out.push_back(make_pair(b - 1, w));
V[b - 1].adj_in.push_back(make_pair(a - 1, w));
}
queue<int> q;
q.push(0);
while (!q.empty()) {
int item = q.front();
q.pop();
if (V[item].ach1) {
continue;
}
V[item].ach1 = true;
for (vector<pair<int, int> >::iterator it = V[item].adj_out.begin(); it != V[item].adj_out.end(); it++) {
q.push(it->first);
}
}
if (!V[n - 1].ach1) {
printf("-1\n");
return;
}
q.push(n - 1);
while (!q.empty()) {
int item = q.front();
q.pop();
if (V[item].achn) {
continue;
}
V[item].achn = true;
for (vector<pair<int, int> >::iterator it = V[item].adj_in.begin(); it != V[item].adj_in.end(); it++) {
q.push(it->first);
}
}
for (int i = 0; i < n; ++i) {
for (vector<pair<int, int> >::iterator it = V[i].adj_out.begin(); it != V[i].adj_out.end(); it++) {
if (V[it->first].ach1 && V[it->first].achn) {
V[i].adj_out2.push_back(*it);
V[it->first].adj_in2.push_back(make_pair(i, it->second));
}
}
}
vector<transmitter_t> E;
for (int i = 0; i < n; ++i) {
for (vector<pair<int, int> >::iterator it = V[i].adj_out2.begin(); it != V[i].adj_out2.end(); it++) {
transmitter_t tr;
tr.from = i;
tr.to = it->first;
tr.power = it->second;
E.push_back(tr);
}
}
bool modified = true;
while (modified) {
modified = false;
for (vector<transmitter_t>::iterator it = E.begin(); it != E.end(); it++) {
int64_t new_mul = V[it->to].min_mul_to_exit * it->power;
if (new_mul >= INF) {
new_mul = INF;
}
if (new_mul < V[it->from].min_mul_to_exit) {
V[it->from].min_mul_to_exit = new_mul;
modified = true;
}
}
}
stack<pair<int, int64_t> > st;
st.push(make_pair(0, 1));
while (!st.empty()) {
pair<int, int64_t> item = st.top();
st.pop();
int node = item.first;
int64_t power = item.second;
if (V[node].capacity < power) {
continue;
}
if (V[node].powers.contains(power)) {
continue;
}
if (power * V[node].min_mul_to_exit >= INF) {
continue;
}
V[node].powers.insert(power);
for (vector<pair<int, int> >::iterator it = V[node].adj_out2.begin(); it != V[node].adj_out2.end(); it++) {
st.push(make_pair(it->first, power * it->second));
}
}
int64_t max = -1;
for (set<int64_t>::iterator it = V[n - 1].powers.begin(); it != V[n - 1].powers.end(); it++) {
if (*it > max) {
max = *it;
}
}
printf("%lld\n", max);
}
int main () {
int T;
scanf("%d", &T);
for (int i = 0; i < T; ++i) {
test_case();
}
return 0;
}
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 | #include <cstdio> #include <cstdint> #include <vector> #include <queue> #include <stack> #include <set> using namespace std; const int64_t INF = 1000000008LL; typedef struct { int64_t min_mul_to_exit; int capacity; bool ach1, achn; vector<pair<int, int> > adj_out; vector<pair<int, int> > adj_in; vector<pair<int, int> > adj_out2; vector<pair<int, int> > adj_in2; set<int64_t> powers; } router_t; typedef struct { int from, to; int64_t power; } transmitter_t; void test_case() { int n, m; scanf("%d %d", &n, &m); vector<router_t> V; V.reserve(n + 8); for (int i = 0; i < n; ++i) { router_t r; r.ach1 = false; r.achn = false; r.min_mul_to_exit = (i == n - 1) ? 1 : INF; scanf("%d", &r.capacity); V.push_back(r); } for (int i = 0; i < m; ++i) { int a, b, w; scanf("%d %d %d", &a, &b, &w); V[a - 1].adj_out.push_back(make_pair(b - 1, w)); V[b - 1].adj_in.push_back(make_pair(a - 1, w)); } queue<int> q; q.push(0); while (!q.empty()) { int item = q.front(); q.pop(); if (V[item].ach1) { continue; } V[item].ach1 = true; for (vector<pair<int, int> >::iterator it = V[item].adj_out.begin(); it != V[item].adj_out.end(); it++) { q.push(it->first); } } if (!V[n - 1].ach1) { printf("-1\n"); return; } q.push(n - 1); while (!q.empty()) { int item = q.front(); q.pop(); if (V[item].achn) { continue; } V[item].achn = true; for (vector<pair<int, int> >::iterator it = V[item].adj_in.begin(); it != V[item].adj_in.end(); it++) { q.push(it->first); } } for (int i = 0; i < n; ++i) { for (vector<pair<int, int> >::iterator it = V[i].adj_out.begin(); it != V[i].adj_out.end(); it++) { if (V[it->first].ach1 && V[it->first].achn) { V[i].adj_out2.push_back(*it); V[it->first].adj_in2.push_back(make_pair(i, it->second)); } } } vector<transmitter_t> E; for (int i = 0; i < n; ++i) { for (vector<pair<int, int> >::iterator it = V[i].adj_out2.begin(); it != V[i].adj_out2.end(); it++) { transmitter_t tr; tr.from = i; tr.to = it->first; tr.power = it->second; E.push_back(tr); } } bool modified = true; while (modified) { modified = false; for (vector<transmitter_t>::iterator it = E.begin(); it != E.end(); it++) { int64_t new_mul = V[it->to].min_mul_to_exit * it->power; if (new_mul >= INF) { new_mul = INF; } if (new_mul < V[it->from].min_mul_to_exit) { V[it->from].min_mul_to_exit = new_mul; modified = true; } } } stack<pair<int, int64_t> > st; st.push(make_pair(0, 1)); while (!st.empty()) { pair<int, int64_t> item = st.top(); st.pop(); int node = item.first; int64_t power = item.second; if (V[node].capacity < power) { continue; } if (V[node].powers.contains(power)) { continue; } if (power * V[node].min_mul_to_exit >= INF) { continue; } V[node].powers.insert(power); for (vector<pair<int, int> >::iterator it = V[node].adj_out2.begin(); it != V[node].adj_out2.end(); it++) { st.push(make_pair(it->first, power * it->second)); } } int64_t max = -1; for (set<int64_t>::iterator it = V[n - 1].powers.begin(); it != V[n - 1].powers.end(); it++) { if (*it > max) { max = *it; } } printf("%lld\n", max); } int main () { int T; scanf("%d", &T); for (int i = 0; i < T; ++i) { test_case(); } return 0; } |
English