1 #include <cstdio> 2 const int M=150010,N=30010; 3 struct edge{int v,w,next;}e[M];int head[N],cnt; 4 void add(int u,int v,int w){e[++cnt].v=v,e[cnt].w=w,e[cnt].next=head[u],head[u]=cnt;} 5 int d[N],vis[N],stack[M],top; 6 int spfa(int n){ 7 for(int i=1;i<=n;i++)vis[i]=0,d[i]=2147483647; 8 top=0,stack[++top]=1,vis[1]=1,d[1]=0; 9 for(int u;top>=0;){ 10 u=stack[top--],vis[u]=0; 11 for(int i=head[u],v,w;v=e[i].v,w=e[i].w,i;i=e[i].next) 12 if(d[v]>d[u]+w){ 13 d[v]=d[u]+w; 14 if(!vis[v])stack[++top]=v,vis[v]=1; 15 } 16 } 17 return d[n]; 18 } 19 int main(){ 20 for(int n,m,u,v,w;scanf("%d%d",&n,&m)!=EOF;){ 21 for(int i=1;i<=n;i++)head[i]=0; 22 while(m--)scanf("%d%d%d",&u,&v,&w),add(u,v,w); 23 printf("%d ",spfa(n)); 24 } 25 return 0; 26 }