1 #include <cstdio> 2 #include <cmath> 3 #include <cstring> 4 #include <algorithm> 5 #include <queue> 6 #include <stack> 7 #include <vector> 8 #include <iomanip> 9 using namespace std; 10 typedef pair<int,int> P; 11 const int mmax=160000; 12 struct edge{ 13 int next,to,w; 14 }e[mmax]; 15 int n,m,d[mmax],head[mmax],cnt; 16 void add(int u,int v,int d) 17 { 18 e[cnt].w=d; 19 e[cnt].to=v; 20 e[cnt].next=head[u]; 21 head[u]=cnt++; 22 } 23 int main(int argc, char *argv[]) 24 { 25 while(scanf("%d%d",&n,&m)!=EOF) 26 { 27 memset(head,-1,sizeof(head)); 28 fill(d+1,d+1+n,0x3f3f3f3f); 29 cnt=0; 30 for(int i=0;i<m;i++) 31 { 32 int x,y,z; 33 scanf("%d%d%d",&x,&y,&z); 34 add(x,y,z); 35 } 36 d[1]=0; 37 priority_queue<pair<int,int> >q; 38 q.push(make_pair(-d[1],1)); 39 while(!q.empty()) 40 { 41 P p=q.top(); 42 int now=p.second; 43 q.pop(); 44 if(-p.first>d[now])continue; 45 for(int i=head[now];i>=0;i=e[i].next) 46 { 47 int v=e[i].to; 48 if(d[e[i].to]>d[now]+e[i].w) 49 { 50 d[e[i].to]=d[now]+e[i].w; 51 q.push(make_pair(-d[e[i].to],e[i].to)); 52 } 53 } 54 } 55 printf("%d\n",d[n]); 56 } 57 return 0; 58 }