#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using pii=pair<int,int>;
const int M1=100002,M2=10002;
int n,m;
ll p[105];
vector<pii> G[105],rG[105];
bool vis[105][M1+5];
int lst[105][M1+5];
ll dist[105][M2+5];
void dfs(int u,ll v)
{
vis[u][v]=1;
for(auto pr:G[u])
{
ll nv=v*pr.second;
int nu=pr.first;
if(nv<=p[nu]&&nv<=M1&&!vis[nu][nv])
dfs(nu,nv);
}
}
void solve()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
G[i].clear();
rG[i].clear();
memset(vis[i],0,sizeof(vis[i]));
memset(dist[i],0,sizeof(dist[i]));
}
for(int i=1;i<=n;i++)
cin>>p[i];
for(int i=1;i<=m;i++)
{
int a,b,c;
cin>>a>>b>>c;
G[a].emplace_back(b,c);
rG[b].emplace_back(a,c);
}
dfs(1,1);
priority_queue<pair<ll,pii>> pq;
dist[n][1]=p[n];
pq.emplace(p[n],pii(n,1));
while(!pq.empty())
{
ll d=pq.top().first;
int u=pq.top().second.first;
int v=pq.top().second.second;
pq.pop();
if(d!=dist[u][v]) continue;
for(auto pr:rG[u]) if(1ll*pr.second*v<=M2)
{
int nu=pr.first;
int nv=pr.second*v;
ll nd=min(p[nu],d/pr.second);
if(nd>dist[nu][nv])
{
dist[nu][nv]=nd;
pq.emplace(nd,pii(nu,nv));
}
}
}
for(int i=1;i<=n;i++)
{
lst[i][0]=0;
for(int j=1;j<=M1;j++)
lst[i][j]=(vis[i][j])?j:lst[i][j-1];
}
ll ans=0;
if(n==1) ans=1;
for(int i=1;i<=n;i++)
for(int j=1;j<=M2;j++)
for(auto pr:rG[i])
{
int u=pr.first;
ll lim=dist[i][j]/pr.second;
lim=min(lim,(ll)M1);
ans=max(ans,1ll*j*lst[u][lim]*pr.second);
}
if(!ans)
cout<<"-1\n";
else
cout<<ans<<'\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--)
solve();
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 | #include<bits/stdc++.h> using namespace std; using ll=long long; using pii=pair<int,int>; const int M1=100002,M2=10002; int n,m; ll p[105]; vector<pii> G[105],rG[105]; bool vis[105][M1+5]; int lst[105][M1+5]; ll dist[105][M2+5]; void dfs(int u,ll v) { vis[u][v]=1; for(auto pr:G[u]) { ll nv=v*pr.second; int nu=pr.first; if(nv<=p[nu]&&nv<=M1&&!vis[nu][nv]) dfs(nu,nv); } } void solve() { cin>>n>>m; for(int i=1;i<=n;i++) { G[i].clear(); rG[i].clear(); memset(vis[i],0,sizeof(vis[i])); memset(dist[i],0,sizeof(dist[i])); } for(int i=1;i<=n;i++) cin>>p[i]; for(int i=1;i<=m;i++) { int a,b,c; cin>>a>>b>>c; G[a].emplace_back(b,c); rG[b].emplace_back(a,c); } dfs(1,1); priority_queue<pair<ll,pii>> pq; dist[n][1]=p[n]; pq.emplace(p[n],pii(n,1)); while(!pq.empty()) { ll d=pq.top().first; int u=pq.top().second.first; int v=pq.top().second.second; pq.pop(); if(d!=dist[u][v]) continue; for(auto pr:rG[u]) if(1ll*pr.second*v<=M2) { int nu=pr.first; int nv=pr.second*v; ll nd=min(p[nu],d/pr.second); if(nd>dist[nu][nv]) { dist[nu][nv]=nd; pq.emplace(nd,pii(nu,nv)); } } } for(int i=1;i<=n;i++) { lst[i][0]=0; for(int j=1;j<=M1;j++) lst[i][j]=(vis[i][j])?j:lst[i][j-1]; } ll ans=0; if(n==1) ans=1; for(int i=1;i<=n;i++) for(int j=1;j<=M2;j++) for(auto pr:rG[i]) { int u=pr.first; ll lim=dist[i][j]/pr.second; lim=min(lim,(ll)M1); ans=max(ans,1ll*j*lst[u][lim]*pr.second); } if(!ans) cout<<"-1\n"; else cout<<ans<<'\n'; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin>>t; while(t--) solve(); return 0; } |
English