using System; using System.Collections.Generic; using System.Collections; using System.Collections.Specialized; using System.Linq; using System.Text; using System.IO; using System.Reflection; using static System.Math; using System.Numerics; using System.Threading; using System.Runtime.CompilerServices; static class Program{ const int mod=(int)1e9+7; const long inf=long.MaxValue>>1; static List>[] li; static void Main(){ Sc sc=new Sc(); var t=sc.I; var s=sc.Ia; int n=s[0],m=s[1]; li=new List>[n+1]; var b=new int[n+1]; var h=new int[m][]; var c=new long[n+1]; for(int i=1;i<=n;i++){ li[i]=new List>(); b[i]=-1; } for(int i=0;i(64); pq.Push(new Rheap.Dt(0,h[k][1])); c[h[k][1]]=0; while(pq.cnt>0){ var e=pq.Dequeue; if(e.d==h[k][0]){ ans=Min(h[k][2]+e.n,ans); break; } if(b[e.d]==k){continue;} b[e.d]=k; for(int i=0;ili[e.d][i].Item2+e.n){ pq.Push(new Rheap.Dt(li[e.d][i].Item2+e.n,li[e.d][i].Item1)); c[li[e.d][i].Item1]=li[e.d][i].Item2+e.n; } } } } Console.WriteLine("{0}",ans==inf?-1:ans); } } public class Rheap{ public class Dt{ public long n; public T d; public Dt(long n,T d){this.n=n;this.d=d;} public override string ToString()=>"d:"+d.ToString()+" n:"+n.ToString(); } private const int m=65; private long la=0; private Dt[][] bf; private int[] h; public int cnt=0; public Rheap(int max){ bf=new Dt[m][]; h=new int[m]; for(int i = 0;i0){ int p=0; for(int j = 1;jbf[i][j].n){p=j;} } la=bf[i][p].n; for(int j = 0;j(int n,Func f){var a=new T[n];for(int i=0;i(int n,Func f){var a=new T[n];for(int i=0;i(int n,Func f){var a=new T[n];for(int i=0;i(int n,Func f){var a=new T[n];for(int i=0;i