#include <bits/stdc++.h> #define st first #define nd second #define pb push_back #define ub upper_bound #define lb lower_bound #define fr(i,n) for(int i=0; i<(n); i++) #define frx(i,a,b) for(int i=(a); i<=(b); i++) #define ok(cond) cout << ((cond) ? "Yes" : "No") << "\n"; #define watch(x) cerr << (#x) << " == " << (x) << endl; using namespace std; typedef long long ll; typedef long double ld; typedef const int ci; typedef const ll cll; typedef const ld cld; typedef pair<int,int> pii; typedef set<int> si; typedef vector<int> vi; typedef vector<pair<int,int>> vpii; typedef vector<pair<int,ll>> vpill; template<typename T1, typename T2> istream& operator>>(istream& is, pair<T1,T2>& p) { return is >> p.st >> p.nd; } template<typename T1, typename T2> ostream& operator<<(ostream& os, pair<T1,T2>& p) { return os << p.st << " " << p.nd; } template<typename T> ostream& operator<<(ostream& os, vector<T>& v) { for (T x : v) { os << x << " "; } return os; } template<typename T> ostream& operator<<(ostream& os, set<T>& s) { for (T x : s) { os << x << " "; } return os; } const int N = 100, M = 200, P = 1e9; int n,m; int p[N+5]; bool vis[N+5]; int max_cap[N+5]; vector<pair<int,int>> g[N+5], compressed_ones[N+5]; vector<int> ones[N+5]; int max_vol = -1; void get_ones_paths(int start) { priority_queue<pii, vector<pii>, greater<pii>> pq; fill(vis, vis+n+1, 0); fill(max_cap, max_cap+n+1, 0); max_cap[start] = p[start]; pq.push({0, start}); while (!pq.empty()) { auto [cap, v] = pq.top(); pq.pop(); if (vis[v]) { continue; } vis[v] = 1; for (int u : ones[v]) { if (!vis[u] && max_cap[u] < min(p[u], max_cap[v])) { max_cap[u] = min(p[u], max_cap[v]); pq.push({max_cap[u], u}); } } } frx(i,1,n) { if (i != start && max_cap[i]) { compressed_ones[start].pb({i, max_cap[i]}); } } } void compress_ones() { frx(i,1,n) { get_ones_paths(i); } } void get_max_volume(int vol, int v, bool can_be_one) { if (v == n) { max_vol = max(max_vol, vol); } for (auto [u,w] : g[v]) { if (min(p[u], p[n])/w >= vol) { get_max_volume(vol*w, u, 1); } } if (can_be_one) { for (auto [u,cap] : compressed_ones[v]) { if (cap >= vol) { get_max_volume(vol, u, 0); } } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T; cin>>T; fr(C, T) { cin>>n>>m; fr(i,n) { cin>>p[i+1]; } fr(i,m) { int a,b,w; cin>>a>>b>>w; if (w == 1) { ones[a].pb(b); } else { g[a].pb({b,w}); } } compress_ones(); get_max_volume(1, 1, 1); cout << max_vol << "\n"; frx(i,1,n) { max_vol = -1; g[i].clear(); ones[i].clear(); compressed_ones[i].clear(); } } 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 152 153 154 155 156 | #include <bits/stdc++.h> #define st first #define nd second #define pb push_back #define ub upper_bound #define lb lower_bound #define fr(i,n) for(int i=0; i<(n); i++) #define frx(i,a,b) for(int i=(a); i<=(b); i++) #define ok(cond) cout << ((cond) ? "Yes" : "No") << "\n"; #define watch(x) cerr << (#x) << " == " << (x) << endl; using namespace std; typedef long long ll; typedef long double ld; typedef const int ci; typedef const ll cll; typedef const ld cld; typedef pair<int,int> pii; typedef set<int> si; typedef vector<int> vi; typedef vector<pair<int,int>> vpii; typedef vector<pair<int,ll>> vpill; template<typename T1, typename T2> istream& operator>>(istream& is, pair<T1,T2>& p) { return is >> p.st >> p.nd; } template<typename T1, typename T2> ostream& operator<<(ostream& os, pair<T1,T2>& p) { return os << p.st << " " << p.nd; } template<typename T> ostream& operator<<(ostream& os, vector<T>& v) { for (T x : v) { os << x << " "; } return os; } template<typename T> ostream& operator<<(ostream& os, set<T>& s) { for (T x : s) { os << x << " "; } return os; } const int N = 100, M = 200, P = 1e9; int n,m; int p[N+5]; bool vis[N+5]; int max_cap[N+5]; vector<pair<int,int>> g[N+5], compressed_ones[N+5]; vector<int> ones[N+5]; int max_vol = -1; void get_ones_paths(int start) { priority_queue<pii, vector<pii>, greater<pii>> pq; fill(vis, vis+n+1, 0); fill(max_cap, max_cap+n+1, 0); max_cap[start] = p[start]; pq.push({0, start}); while (!pq.empty()) { auto [cap, v] = pq.top(); pq.pop(); if (vis[v]) { continue; } vis[v] = 1; for (int u : ones[v]) { if (!vis[u] && max_cap[u] < min(p[u], max_cap[v])) { max_cap[u] = min(p[u], max_cap[v]); pq.push({max_cap[u], u}); } } } frx(i,1,n) { if (i != start && max_cap[i]) { compressed_ones[start].pb({i, max_cap[i]}); } } } void compress_ones() { frx(i,1,n) { get_ones_paths(i); } } void get_max_volume(int vol, int v, bool can_be_one) { if (v == n) { max_vol = max(max_vol, vol); } for (auto [u,w] : g[v]) { if (min(p[u], p[n])/w >= vol) { get_max_volume(vol*w, u, 1); } } if (can_be_one) { for (auto [u,cap] : compressed_ones[v]) { if (cap >= vol) { get_max_volume(vol, u, 0); } } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T; cin>>T; fr(C, T) { cin>>n>>m; fr(i,n) { cin>>p[i+1]; } fr(i,m) { int a,b,w; cin>>a>>b>>w; if (w == 1) { ones[a].pb(b); } else { g[a].pb({b,w}); } } compress_ones(); get_max_volume(1, 1, 1); cout << max_vol << "\n"; frx(i,1,n) { max_vol = -1; g[i].clear(); ones[i].clear(); compressed_ones[i].clear(); } } return 0; } |