1 #include <cmath> 2 #include <queue> 3 #include <cstdio> 4 #include <cstdlib> 5 #include <cstring> 6 #include <iostream> 7 #include <algorithm> 8 using namespace std; 9 /* 10 struct point{ 11 int a,b; 12 point(int a=0,int b=0):a(a),b(b){} 13 bool operator<(const point NEXT)const{return (a==NEXT.a)?(b<NEXT.b):(a<NEXT.a);} 14 void out(){printf("(%d,%d)",a,b);} 15 }; 16 17 bool comp(point A,point B){return (A.a==B.a)?(A.b>B.b):(A.a<B.a);} 18 19 void sortForTwoSet(){ 20 point A[5]={point(1,2),point(1,4),point(2,5),point(3,5),point(1,3)}; 21 sort(A,A+5,comp); 22 for(int i=0;i<5;i++){A[i].out();cout<<" ";} 23 } 24 */ 25 struct edge{ 26 int next,v,w; 27 edge(int next=0,int v=0,int w=0):next(next),v(v),w(w){} 28 }; 29 30 const int MAXN=105,MAXM=10005; 31 32 int head[MAXN],cnt; 33 edge e[MAXM*2]; 34 35 void init_G(){ 36 cnt=0,memset(head,0,sizeof(head)); 37 memset(e,0,sizeof(e)); 38 } 39 40 void add(int u,int v,int w){ 41 e[++cnt]=edge(head[u],v,w),head[u]=cnt; 42 e[++cnt]=edge(head[v],u,w),head[v]=cnt; 43 } 44 45 struct node{ 46 int dis,u; 47 bool operator<(const node NEXT)const{return dis<NEXT.dis;} 48 node(int dis=0,int u=0):dis(dis),u(u){} 49 //优先队列默认大根堆,要反过来才是小根堆 50 }; 51 52 int d[MAXN];void init_d(int n){for(int i=1;i<=n;i++)d[i]=1e9;}//typedef pair<int,int> P; 53 54 int dijkstra(int s,int t){ 55 init_d(t); 56 priority_queue<node>q;//priority_queue<P,vector<P>,greater<P> >q;//第一维度为距离,第二维度为点 57 d[s]=0,q.push(node(0,s));//d[s]=0,q.push(make_pair(0,s)); 58 for(int u,v;!q.empty();){ 59 u=q.top().u,q.pop();//u=q.top().second,q.pop(); 60 for(int i=head[u];i;i=e[i].next){ 61 v=e[i].v; 62 if(d[v]>d[u]+e[i].w) { 63 d[v]=d[u]+e[i].w; 64 q.push(node(d[v],v));//q.push(make_pair(d[v],v)); 65 } 66 } 67 68 } 69 return d[t]; 70 } 71 72 int main(){ 73 for(int n,m;scanf("%d%d",&n,&m),n+m;){ 74 init_G(); 75 for(int i=1,A,B,C;i<=m;i++)cin>>A>>B>>C,add(A,B,C); 76 cout<<dijkstra(1,n)<<endl; 77 } 78 return 0; 79 }