#include #include using namespace std; using namespace atcoder; using ll=long long; using ldub=long double; using lldub=__float128; using str=string; using mint=modint; template using tup2=tuple; template using tup3=tuple; template using tup4=tuple; template using tup5=tuple; template using tup6=tuple; template using tup7=tuple; template using vec=vector; template using vec2=vector>; template using vec3=vector>; template using vec4=vector>; template using vec5=vector>; template using vec6=vector>; template using que=queue; template using Pque=priority_queue; template using pque=priority_queue, greater>; struct Edge{ ll from,to,w=1,num=-1; }; using gvec=vector; using gvec2=vector; template bool chmax(T &a,T b){if(a bool chmin(T &a,T b){if(b> N >> M; gvec2 G(N); gvec E(M); for(ll i=0;i> u >> v >> w; u--,v--; E[i]={u,v,w,i}; G[u].pb({u,v,w,i}); G[v].pb({v,u,w,i}); } vec A(N),B(N),C(N); for(ll i=0;i> A[i]; for(ll i=0;i> B[i]; for(ll i=0;i> C[i]; pque> BFS; vec D(N,INF); BFS.push(tup(1,0)); D[0]=1; while(!BFS.empty()){ auto [d,p]=BFS.top(); BFS.pop(); ll nd=(d+A[p]-1)/A[p]*A[p]; if((nd/A[p])%B[p]==0) nd+=min(A[p],C[p]); for(auto e:G[p]) if(chmin(D[e.to],nd+e.w)) BFS.push(tup(nd+e.w,e.to)); } cout << D[N-1] << endl; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); mint::set_mod(998244353); // mint::set_mod(1000000007); ll T=1; // cin >> T; while(T--) solve(); return 0; }