/* 2025
* Maciej Szeptuch
*/
#include <algorithm>
#include <cstdio>
#include <queue>
#include <unordered_set>
#include <vector>
int tests;
int verts;
int edges;
const int MAX_VERTS = 128;
std::vector<std::pair<int, int>> graph[MAX_VERTS];
std::vector<std::pair<int, int>> revgraph[MAX_VERTS];
int limit[MAX_VERTS];
void cleanup_limits(void);
int solve(void);
int main(void)
{
scanf("%d", &tests);
for(int t = 0; t < tests; ++t)
{
scanf("%d %d", &verts, &edges);
for(int v = 0; v < verts; ++v)
{
scanf("%d", &limit[v]);
graph[v].clear();
revgraph[v].clear();
}
for(int e = 0; e < edges; ++e)
{
int start;
int end;
int amp;
scanf("%d %d %d\n", &start, &end, &);
--start;
--end;
graph[start].push_back({end, amp});
revgraph[end].push_back({start, amp});
}
cleanup_limits();
printf("%d\n", solve());
}
return 0;
}
void cleanup_limits(void)
{
bool visited[128] = {};
std::queue<int> que;
que.push(verts - 1);
visited[verts - 1] = true;
while(!que.empty())
{
auto v = que.front();
que.pop();
for(const auto &[n, amp]: revgraph[v])
{
if(visited[n])
continue;
visited[n] = true;
que.push(n);
}
}
for(int v = 0; v < verts; ++v)
if(!visited[v])
limit[v] = 0;
bool updated = false;
do
{
updated = false;
for(int v = 0; v < verts - 1; ++v)
{
if(!limit[v])
continue;
int max_limit = 0;
for(const auto &[n, amp]: graph[v])
max_limit = std::max(max_limit, limit[n] / amp);
if(limit[v] > max_limit)
{
updated = true;
limit[v] = max_limit;
}
}
for(int v = 0; v < verts; ++v)
{
if(!limit[v])
continue;
long long int max_limit = 1;
for(const auto &[n, amp]: revgraph[v])
max_limit = std::max(max_limit, 1ll * limit[n] * amp);
if(limit[v] > max_limit)
{
updated = true;
limit[v] = max_limit;
}
}
} while(updated);
}
template <>
struct std::hash<std::pair<int, int>>
{
long long int operator()(const std::pair<int, int> &p) const
{
return ((1LL * p.first) << 32) + p.second;
}
};
int solve(void)
{
std::unordered_set<std::pair<int, int>> visited;
std::priority_queue<std::pair<int, int>> que;
int max_reached[MAX_VERTS] = {};
visited.emplace(0, 1);
que.push({1, 0});
while(!que.empty() && max_reached[verts - 1] - 1 < limit[verts - 1])
{
const auto [signal, v] = que.top();
que.pop();
for(const auto &[n, amp]: graph[v])
{
long long int new_signal = 1ll * signal * amp;
if(new_signal > limit[n])
continue;
if(visited.contains({n, new_signal}))
continue;
visited.emplace(n, new_signal);
que.push({new_signal, n});
max_reached[n] = std::max(1ll * max_reached[n], new_signal + 1);
}
}
return max_reached[verts - 1] - 1;
}
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 | /* 2025 * Maciej Szeptuch */ #include <algorithm> #include <cstdio> #include <queue> #include <unordered_set> #include <vector> int tests; int verts; int edges; const int MAX_VERTS = 128; std::vector<std::pair<int, int>> graph[MAX_VERTS]; std::vector<std::pair<int, int>> revgraph[MAX_VERTS]; int limit[MAX_VERTS]; void cleanup_limits(void); int solve(void); int main(void) { scanf("%d", &tests); for(int t = 0; t < tests; ++t) { scanf("%d %d", &verts, &edges); for(int v = 0; v < verts; ++v) { scanf("%d", &limit[v]); graph[v].clear(); revgraph[v].clear(); } for(int e = 0; e < edges; ++e) { int start; int end; int amp; scanf("%d %d %d\n", &start, &end, &); --start; --end; graph[start].push_back({end, amp}); revgraph[end].push_back({start, amp}); } cleanup_limits(); printf("%d\n", solve()); } return 0; } void cleanup_limits(void) { bool visited[128] = {}; std::queue<int> que; que.push(verts - 1); visited[verts - 1] = true; while(!que.empty()) { auto v = que.front(); que.pop(); for(const auto &[n, amp]: revgraph[v]) { if(visited[n]) continue; visited[n] = true; que.push(n); } } for(int v = 0; v < verts; ++v) if(!visited[v]) limit[v] = 0; bool updated = false; do { updated = false; for(int v = 0; v < verts - 1; ++v) { if(!limit[v]) continue; int max_limit = 0; for(const auto &[n, amp]: graph[v]) max_limit = std::max(max_limit, limit[n] / amp); if(limit[v] > max_limit) { updated = true; limit[v] = max_limit; } } for(int v = 0; v < verts; ++v) { if(!limit[v]) continue; long long int max_limit = 1; for(const auto &[n, amp]: revgraph[v]) max_limit = std::max(max_limit, 1ll * limit[n] * amp); if(limit[v] > max_limit) { updated = true; limit[v] = max_limit; } } } while(updated); } template <> struct std::hash<std::pair<int, int>> { long long int operator()(const std::pair<int, int> &p) const { return ((1LL * p.first) << 32) + p.second; } }; int solve(void) { std::unordered_set<std::pair<int, int>> visited; std::priority_queue<std::pair<int, int>> que; int max_reached[MAX_VERTS] = {}; visited.emplace(0, 1); que.push({1, 0}); while(!que.empty() && max_reached[verts - 1] - 1 < limit[verts - 1]) { const auto [signal, v] = que.top(); que.pop(); for(const auto &[n, amp]: graph[v]) { long long int new_signal = 1ll * signal * amp; if(new_signal > limit[n]) continue; if(visited.contains({n, new_signal})) continue; visited.emplace(n, new_signal); que.push({new_signal, n}); max_reached[n] = std::max(1ll * max_reached[n], new_signal + 1); } } return max_reached[verts - 1] - 1; } |
English