#include "bits/stdc++.h"
using namespace std;
#define all(x) x.begin(),x.end()
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << " " << p.second; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#ifdef LOCAL
// #include "debug.h"
#else
#define debug(...) 42
#define ASSERT(...) 42
#endif
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int oo = 1e9;
const int H = 1e5;
const int N = 100;
int dst[N][H];
void solve(bool haha) {
int n,m; cin >> n >> m;
vi p(n);
for(auto& i : p) cin >> i;
vector<vector<pi>> adj(n);
for(int i=0;i<m;++i) {
int u,v,w; cin >> u >> v >> w;
--u,--v;
adj[v].push_back({u,w});
}
if(!haha) return;
auto good = [&](int mid) {
struct el {
int at,lo;
int hi;
bool operator<(const el& o) const {
return hi<o.hi;
}
};
vi c;
const int C= sqrt(mid+0.5)+1;
for(int i=0;i<n;++i) {
fill(dst[i], dst[i]+C*2+1,-oo);
}
// for(int i=1;i<=C;++i) {
// c.push_back((mid+i-1)/i);
// }
// for(int i=1;i<=C;++i) c.push_back(i);
// sort(all(c));
// c.erase(unique(all(c)),c.end());
const int k = 2*C;
auto real = [&](int y) {
if(y<=C) {
return y-1;
}
return (mid+y-1)/y + C;
};
priority_queue<el> pq;
auto push = [&](int at , int lo, int hi) {
hi = min(hi,p[at]);
if(lo>hi) return;
int id= real(lo);
if(dst[at][id]<hi) {
dst[at][id]=hi;
pq.push({at,lo,hi});
}
};
push(n-1,mid,p[n-1]);
while(!pq.empty()) {
auto e = pq.top(); pq.pop();
int id = real(e.lo);
if(dst[e.at][id]!=e.hi) continue;
if(e.at==0 and e.lo==1) {
return true;
}
for(auto [to,w] : adj[e.at]) {
push(to,(e.lo+w-1)/w,(e.hi)/w);
}
}
return false;
};
if(!good(1)) {
cout << "-1\n";
return;
}
int lo=1, hi = p[n-1];
while(lo<hi) {
int mid = (lo+hi+1)/2;
if(good(mid)) lo = mid;
else hi = mid-1;
}
cout << lo << '\n';
}
int main() {
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
// freopen("input_file.txt","r",stdin);
int t; cin >> t;
for(int i=1;i<=t;++i) {
if(i%10==0)
cerr << i << '\n';
solve(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 | #include "bits/stdc++.h" using namespace std; #define all(x) x.begin(),x.end() template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << " " << p.second; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; } #ifdef LOCAL // #include "debug.h" #else #define debug(...) 42 #define ASSERT(...) 42 #endif typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pi; const int oo = 1e9; const int H = 1e5; const int N = 100; int dst[N][H]; void solve(bool haha) { int n,m; cin >> n >> m; vi p(n); for(auto& i : p) cin >> i; vector<vector<pi>> adj(n); for(int i=0;i<m;++i) { int u,v,w; cin >> u >> v >> w; --u,--v; adj[v].push_back({u,w}); } if(!haha) return; auto good = [&](int mid) { struct el { int at,lo; int hi; bool operator<(const el& o) const { return hi<o.hi; } }; vi c; const int C= sqrt(mid+0.5)+1; for(int i=0;i<n;++i) { fill(dst[i], dst[i]+C*2+1,-oo); } // for(int i=1;i<=C;++i) { // c.push_back((mid+i-1)/i); // } // for(int i=1;i<=C;++i) c.push_back(i); // sort(all(c)); // c.erase(unique(all(c)),c.end()); const int k = 2*C; auto real = [&](int y) { if(y<=C) { return y-1; } return (mid+y-1)/y + C; }; priority_queue<el> pq; auto push = [&](int at , int lo, int hi) { hi = min(hi,p[at]); if(lo>hi) return; int id= real(lo); if(dst[at][id]<hi) { dst[at][id]=hi; pq.push({at,lo,hi}); } }; push(n-1,mid,p[n-1]); while(!pq.empty()) { auto e = pq.top(); pq.pop(); int id = real(e.lo); if(dst[e.at][id]!=e.hi) continue; if(e.at==0 and e.lo==1) { return true; } for(auto [to,w] : adj[e.at]) { push(to,(e.lo+w-1)/w,(e.hi)/w); } } return false; }; if(!good(1)) { cout << "-1\n"; return; } int lo=1, hi = p[n-1]; while(lo<hi) { int mid = (lo+hi+1)/2; if(good(mid)) lo = mid; else hi = mid-1; } cout << lo << '\n'; } int main() { // ios_base::sync_with_stdio(false); // cin.tie(NULL); // freopen("input_file.txt","r",stdin); int t; cin >> t; for(int i=1;i<=t;++i) { if(i%10==0) cerr << i << '\n'; solve(1); } } |
English