#include <bits/stdc++.h> #define debug(x) cout<<#x<<" = "<<x<<"\n" // #define debug(x) x #define pb push_back #define ins insert #define fi first #define se second using namespace std; using ull = unsigned long long; using ll = long long; using ld = long double; template <typename H, typename T> ostream& operator<<(ostream& os, pair<H, T> m){ return os <<"("<< m.fi<<", "<<m.se<<")"; } template <typename H> ostream& operator<<(ostream& os, vector<H> V){ os<<"{"; for(int i=0; i<V.size(); i++){ if(i)os<<" "; os<<V[i]; } os<<"}"; return os; } void solve(); int T = 0; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; cin>>t; while(t--) { T++; //s debug(T); solve(); } return 0; } const int MAX_N = 100+5; const int SQRT = 31622; ll p[MAX_N]; vector<pair<int,int> > graf[MAX_N]; vector<pair<int,int> > grafT[MAX_N]; //transponowany set<ll , greater<ll> > cze1[MAX_N]; map<ll,ll> cze2[MAX_N]; void dfs1(int v, ll sila){ if(sila <= p[v]){ if(cze1[v].find(sila) != cze1[v].end()){ return; // ten wierzcholek juz byl odwiedzony z ta sila po co sie powtarzac }else{ cze1[v].insert(sila); } }else{ return; } if(sila > SQRT) return; //zostawiamy zabawe dla kogos innego for(auto x : graf[v]){ dfs1(x.fi,sila*x.se); } } void solve(){ int n,m; cin>>n>>m; //if(T == 15) {debug(n); debug(m);} for(int i = 1; i <= n; i++) cin>>p[i]; for(int i = 1; i <= n; i++){ graf[i].clear(); grafT[i].clear(); cze1[i].clear(); cze2[i].clear(); } for(int i = 1; i <= m; i++){ ll a,b,w; cin>>a>>b>>w; graf[a].pb({b,w}); grafT[b].pb({a,w}); } //faza 1 dfs1(1,1); //debug((*(cze1[n].begin()))); //https://www.youtube.com/watch?v=60X19_jeKF0 // priority_queue<pair<ll,pair<ll,ll> > > Q; //ciagle czekamy na dzien...//choc potrzebny nam sen tej nocy pewnie nikt nie pojdzie spac... cze2[n][1] = p[n]; // limit na n-ce (ence pence) //let fold_tree f a t = match t with | Leaf -> a | Node(l,v,r) -> f (fold_tree f a l) v (fold_tree f a r);; //KOCHAM WIELBLONDY!... ...Pozdrowienia dla czytajacego (skoro to czytasz to istniejesz powiedzial kartezjusz) for(auto x : grafT[n]){ Q.push({-1 * x.se, {x.fi , p[n] / x.se}}); } while(!Q.empty()){ //if(T == 15) debug(Q.size()); bool ok = true; pair<ll,pair<ll,ll> > pa = Q.top(); Q.pop(); //if(T ==15) debug(pa); auto it = cze2[pa.se.fi].find(-pa.fi); ll u = min(pa.se.se,p[pa.se.fi]); if(it == cze2[pa.se.fi].end()){ cze2[pa.se.fi][-pa.fi] = u; }else{ if(cze2[pa.se.fi][-pa.fi] >= u){ ok = false; }else{ cze2[pa.se.fi][-pa.fi] = u; } } if(ok){ for(auto x : grafT[pa.se.fi]){ if(u / x.se > 0 && -pa.fi * x.se <= SQRT) Q.push({pa.fi * x.se,{x.fi,u/x.se}}); } } } // debug(*(prev(cze2[1].end()))); ll odp = -1; for(int i = 1; i <= n; i++){ for(auto x : cze2[i]){ auto it = cze1[i].lower_bound(x.se); if(it!=cze1[i].end()){ odp = max(odp,(*it) * x.fi); } } } if(odp == 0) odp = -1; cout<<odp<<"\n"; }
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 debug(x) cout<<#x<<" = "<<x<<"\n" // #define debug(x) x #define pb push_back #define ins insert #define fi first #define se second using namespace std; using ull = unsigned long long; using ll = long long; using ld = long double; template <typename H, typename T> ostream& operator<<(ostream& os, pair<H, T> m){ return os <<"("<< m.fi<<", "<<m.se<<")"; } template <typename H> ostream& operator<<(ostream& os, vector<H> V){ os<<"{"; for(int i=0; i<V.size(); i++){ if(i)os<<" "; os<<V[i]; } os<<"}"; return os; } void solve(); int T = 0; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; cin>>t; while(t--) { T++; //s debug(T); solve(); } return 0; } const int MAX_N = 100+5; const int SQRT = 31622; ll p[MAX_N]; vector<pair<int,int> > graf[MAX_N]; vector<pair<int,int> > grafT[MAX_N]; //transponowany set<ll , greater<ll> > cze1[MAX_N]; map<ll,ll> cze2[MAX_N]; void dfs1(int v, ll sila){ if(sila <= p[v]){ if(cze1[v].find(sila) != cze1[v].end()){ return; // ten wierzcholek juz byl odwiedzony z ta sila po co sie powtarzac }else{ cze1[v].insert(sila); } }else{ return; } if(sila > SQRT) return; //zostawiamy zabawe dla kogos innego for(auto x : graf[v]){ dfs1(x.fi,sila*x.se); } } void solve(){ int n,m; cin>>n>>m; //if(T == 15) {debug(n); debug(m);} for(int i = 1; i <= n; i++) cin>>p[i]; for(int i = 1; i <= n; i++){ graf[i].clear(); grafT[i].clear(); cze1[i].clear(); cze2[i].clear(); } for(int i = 1; i <= m; i++){ ll a,b,w; cin>>a>>b>>w; graf[a].pb({b,w}); grafT[b].pb({a,w}); } //faza 1 dfs1(1,1); //debug((*(cze1[n].begin()))); //https://www.youtube.com/watch?v=60X19_jeKF0 // priority_queue<pair<ll,pair<ll,ll> > > Q; //ciagle czekamy na dzien...//choc potrzebny nam sen tej nocy pewnie nikt nie pojdzie spac... cze2[n][1] = p[n]; // limit na n-ce (ence pence) //let fold_tree f a t = match t with | Leaf -> a | Node(l,v,r) -> f (fold_tree f a l) v (fold_tree f a r);; //KOCHAM WIELBLONDY!... ...Pozdrowienia dla czytajacego (skoro to czytasz to istniejesz powiedzial kartezjusz) for(auto x : grafT[n]){ Q.push({-1 * x.se, {x.fi , p[n] / x.se}}); } while(!Q.empty()){ //if(T == 15) debug(Q.size()); bool ok = true; pair<ll,pair<ll,ll> > pa = Q.top(); Q.pop(); //if(T ==15) debug(pa); auto it = cze2[pa.se.fi].find(-pa.fi); ll u = min(pa.se.se,p[pa.se.fi]); if(it == cze2[pa.se.fi].end()){ cze2[pa.se.fi][-pa.fi] = u; }else{ if(cze2[pa.se.fi][-pa.fi] >= u){ ok = false; }else{ cze2[pa.se.fi][-pa.fi] = u; } } if(ok){ for(auto x : grafT[pa.se.fi]){ if(u / x.se > 0 && -pa.fi * x.se <= SQRT) Q.push({pa.fi * x.se,{x.fi,u/x.se}}); } } } // debug(*(prev(cze2[1].end()))); ll odp = -1; for(int i = 1; i <= n; i++){ for(auto x : cze2[i]){ auto it = cze1[i].lower_bound(x.se); if(it!=cze1[i].end()){ odp = max(odp,(*it) * x.fi); } } } if(odp == 0) odp = -1; cout<<odp<<"\n"; } |