#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define deb(...)
#define DBP(...)
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define rep(i, b, e) for (int i = (b); i < (e); i++)
#define each(a, x) for (auto& a : (x))
#define all(x) (x).begin(), (x).end()
#define sz(x) int((x).size())
void run();
int main() {
cin.sync_with_stdio(0); cin.tie(0);
cout << fixed << setprecision(12);
run();
cout << flush; _Exit(0);
}
constexpr int MAX_N = 101;
constexpr int MAX_LAYERS = 1.5e6;
constexpr int SMALL = 100'000;
struct Edge {
int x, y, b;
};
int n, m;
vi cap, layers;
vector<vector<Edge>> adj;
vector<bool> reachable;
bitset<SMALL> que;
int dist[MAX_LAYERS][MAX_N], touched[MAX_LAYERS], counter;
void dfs(int v) {
if (reachable[v]) return;
reachable[v] = 1;
each(e, adj[v]) dfs(e.x);
}
void access(int b) {
if (touched[b] != counter) {
touched[b] = counter;
fill(dist[b], dist[b]+n, -1);
if (b < SMALL) que.set(b);
}
}
void propagate(int b, int v, int d) {
each(e, adj[v]) {
if (e.y == 1 && d <= cap[e.x] && dist[b][e.x] < d) {
dist[b][e.x] = d;
propagate(b, e.x, d);
}
}
}
void update(int b) {
static vector<pii> tmp;
bool changed = true;
while (changed) {
if (layers[b+1]-layers[b] > 1) {
tmp.clear();
rep(v, 0, n) if (dist[b][v] != -1) tmp.pb({dist[b][v], v});
sort(all(tmp), [](pii l, pii r) { return l.x > r.x; });
each(e, tmp) propagate(b, e.y, e.x);
} else {
rep(v, 0, n) if (dist[b][v] != -1) propagate(b, v, dist[b][v]);
}
changed = false;
rep(v, 0, n) if (dist[b][v] != -1) {
each(e, adj[v]) {
ll alt = ll(dist[b][v]) * e.y;
if (alt <= cap[e.x]) {
while (layers[e.b+1] <= alt) e.b++;
access(e.b);
if (dist[e.b][e.x] < alt) {
dist[e.b][e.x] = int(alt);
changed = changed || (e.b == b);
}
}
}
}
}
}
void solve() {
cin >> n >> m;
cap.resize(n);
adj.assign(n, {});
each(c, cap) cin >> c;
rep(i, 0, m) {
int a, b, w;
cin >> a >> b >> w;
a--; b--;
adj[a].pb({b, w, 0});
}
layers.resize(SMALL);
reachable.assign(n, 0);
dfs(0);
rep(v, 0, n) if (reachable[v]) {
for (int d = 1; cap[v]/d >= SMALL; d++) {
layers.pb(cap[v]/d+1);
}
}
sort(layers.begin()+SMALL, layers.end());
layers.erase(unique(layers.begin()+SMALL, layers.end()), layers.end());
layers.pb(INT_MAX);
assert(sz(layers) <= MAX_LAYERS);
int ans = -1;
counter++;
access(0);
dist[0][0] = 1;
for (int b = 0; b < SMALL; b = int(que._Find_next(b))) {
que.reset(b);
update(b);
ans = max(ans, dist[b][n-1]);
}
rep(b, SMALL, sz(layers)) {
if (touched[b] == counter) {
update(b);
ans = max(ans, dist[b][n-1]);
}
}
cout << ans << '\n';
}
void run() {
layers.resize(SMALL);
iota(all(layers), 1);
int t; cin >> t;
while (t--) {
solve();
}
}
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 | #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define deb(...) #define DBP(...) using namespace std; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) void run(); int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(12); run(); cout << flush; _Exit(0); } constexpr int MAX_N = 101; constexpr int MAX_LAYERS = 1.5e6; constexpr int SMALL = 100'000; struct Edge { int x, y, b; }; int n, m; vi cap, layers; vector<vector<Edge>> adj; vector<bool> reachable; bitset<SMALL> que; int dist[MAX_LAYERS][MAX_N], touched[MAX_LAYERS], counter; void dfs(int v) { if (reachable[v]) return; reachable[v] = 1; each(e, adj[v]) dfs(e.x); } void access(int b) { if (touched[b] != counter) { touched[b] = counter; fill(dist[b], dist[b]+n, -1); if (b < SMALL) que.set(b); } } void propagate(int b, int v, int d) { each(e, adj[v]) { if (e.y == 1 && d <= cap[e.x] && dist[b][e.x] < d) { dist[b][e.x] = d; propagate(b, e.x, d); } } } void update(int b) { static vector<pii> tmp; bool changed = true; while (changed) { if (layers[b+1]-layers[b] > 1) { tmp.clear(); rep(v, 0, n) if (dist[b][v] != -1) tmp.pb({dist[b][v], v}); sort(all(tmp), [](pii l, pii r) { return l.x > r.x; }); each(e, tmp) propagate(b, e.y, e.x); } else { rep(v, 0, n) if (dist[b][v] != -1) propagate(b, v, dist[b][v]); } changed = false; rep(v, 0, n) if (dist[b][v] != -1) { each(e, adj[v]) { ll alt = ll(dist[b][v]) * e.y; if (alt <= cap[e.x]) { while (layers[e.b+1] <= alt) e.b++; access(e.b); if (dist[e.b][e.x] < alt) { dist[e.b][e.x] = int(alt); changed = changed || (e.b == b); } } } } } } void solve() { cin >> n >> m; cap.resize(n); adj.assign(n, {}); each(c, cap) cin >> c; rep(i, 0, m) { int a, b, w; cin >> a >> b >> w; a--; b--; adj[a].pb({b, w, 0}); } layers.resize(SMALL); reachable.assign(n, 0); dfs(0); rep(v, 0, n) if (reachable[v]) { for (int d = 1; cap[v]/d >= SMALL; d++) { layers.pb(cap[v]/d+1); } } sort(layers.begin()+SMALL, layers.end()); layers.erase(unique(layers.begin()+SMALL, layers.end()), layers.end()); layers.pb(INT_MAX); assert(sz(layers) <= MAX_LAYERS); int ans = -1; counter++; access(0); dist[0][0] = 1; for (int b = 0; b < SMALL; b = int(que._Find_next(b))) { que.reset(b); update(b); ans = max(ans, dist[b][n-1]); } rep(b, SMALL, sz(layers)) { if (touched[b] == counter) { update(b); ans = max(ans, dist[b][n-1]); } } cout << ans << '\n'; } void run() { layers.resize(SMALL); iota(all(layers), 1); int t; cin >> t; while (t--) { solve(); } } |
English