#include<bits/stdc++.h> using namespace std; #define ll long long inline int read(){ int s=0,w=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();} while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar(); return s*w; } vector<pair<int,int>> e[103]; const int N=1e7; int a[103],b[N+3]; int f[1000003][103]; signed main() { for(int T=read();T--;) { int n=read(),m=read(),L=0,ans=-1; for(int i=1; i<=n; ++i) a[i]=read(),e[i].clear(); for(int u,v,w; m--;) u=read(),v=read(),w=read(), e[u].push_back({v,w}); for(int i=1; i<=n; ++i) { for(int j=1; j<=a[i]; j=a[i]/(a[i]/j)+1) b[++L]=j; b[++L]=a[i]+1; } sort(b+1,b+L+1),L=unique(b+1,b+L+1)-b-1; for(int i=1; i<=L; ++i) for(int j=1; j<=n; ++j) f[i][j]=-1; f[1][1]=1; for(int i=1; i<=L; ++i) { priority_queue<pair<int,int>> q; for(int j=1; j<=n; ++j) if(f[i][j]!=-1) // printf("%d %d\n",j,f[i][j]), q.push({f[i][j],j}); while(!q.empty()) { auto [v,x]=q.top();q.pop(); if(f[i][x]!=v) continue; for(auto [y,z]:e[x]) if(1ll*f[i][x]*z<=a[y]) { int c=f[i][x]*z; int p=upper_bound(b+1,b+L+1,c)-b-1; if(p!=i) f[p][y]=max(f[p][y],c); else if(c>f[i][y]) f[i][y]=c,q.push({f[i][y],y}); } } ans=max(ans,f[i][n]); } printf("%d\n",ans); } 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 | #include<bits/stdc++.h> using namespace std; #define ll long long inline int read(){ int s=0,w=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();} while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar(); return s*w; } vector<pair<int,int>> e[103]; const int N=1e7; int a[103],b[N+3]; int f[1000003][103]; signed main() { for(int T=read();T--;) { int n=read(),m=read(),L=0,ans=-1; for(int i=1; i<=n; ++i) a[i]=read(),e[i].clear(); for(int u,v,w; m--;) u=read(),v=read(),w=read(), e[u].push_back({v,w}); for(int i=1; i<=n; ++i) { for(int j=1; j<=a[i]; j=a[i]/(a[i]/j)+1) b[++L]=j; b[++L]=a[i]+1; } sort(b+1,b+L+1),L=unique(b+1,b+L+1)-b-1; for(int i=1; i<=L; ++i) for(int j=1; j<=n; ++j) f[i][j]=-1; f[1][1]=1; for(int i=1; i<=L; ++i) { priority_queue<pair<int,int>> q; for(int j=1; j<=n; ++j) if(f[i][j]!=-1) // printf("%d %d\n",j,f[i][j]), q.push({f[i][j],j}); while(!q.empty()) { auto [v,x]=q.top();q.pop(); if(f[i][x]!=v) continue; for(auto [y,z]:e[x]) if(1ll*f[i][x]*z<=a[y]) { int c=f[i][x]*z; int p=upper_bound(b+1,b+L+1,c)-b-1; if(p!=i) f[p][y]=max(f[p][y],c); else if(c>f[i][y]) f[i][y]=c,q.push({f[i][y],y}); } } ans=max(ans,f[i][n]); } printf("%d\n",ans); } return 0; } |