1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 5 using namespace std; 6 7 const int Max = 100000001; 8 9 struct N 10 { 11 int v,w; 12 N *next; 13 }*head[110]; 14 15 void init(int n) 16 { 17 for(int i = 1;i <= n; i++) 18 { 19 head[i] = (struct N *)malloc(sizeof(struct N)); 20 head[i]->v = head[i]->w = -1; 21 head[i]->next = NULL; 22 } 23 } 24 25 void link(int u,int v,int w) 26 { 27 N *p1,*p2,*p; 28 for(p1 = head[u],p2 = head[u]->next; p2 != NULL; p1 = p1->next,p2 = p2->next) 29 { 30 if(p2->v == v) 31 { 32 if(p2->w > w) 33 p2->w = w; 34 return; 35 } 36 if(p1->v < v && v < p2->v) 37 { 38 p = (struct N *)malloc(sizeof(struct N)); 39 p->v = v; 40 p->w = w; 41 p->next = p1->next; 42 p1->next = p; 43 return; 44 } 45 } 46 p = (struct N *)malloc(sizeof(struct N)); 47 p->v = v; 48 p->w = w; 49 p->next = NULL; 50 p1->next = p; 51 return; 52 } 53 54 int mark[1000]; 55 56 void find(int n) 57 { 58 int q[10001],t; 59 int s,e,i; 60 for(i = 1;i <= n; i++) 61 mark[i] = Max; 62 N *p; 63 for(s = 0,e = 0,p = head[1]->next; p != NULL; p = p->next) 64 { 65 q[e++] = p->v; 66 mark[p->v] = p->w; 67 68 } 69 while(s != e) 70 { 71 t = q[s]; 72 s++; 73 for(p = head[t]->next; p != NULL; p = p->next) 74 { 75 if(mark[t] + p->w < mark[p->v]) 76 { 77 mark[p->v] = mark[t] + p->w; 78 q[e++] = p->v; 79 80 } 81 } 82 } 83 } 84 85 int main() 86 { 87 int n,m,i,u,v,w,t; 88 while(scanf("%d %d",&n,&m) != EOF && (m||n) ) 89 { 90 init(n); 91 for(i = 0;i < m;i++) 92 { 93 scanf("%d %d %d",&u,&v,&w); 94 link(v,u,w); 95 link(u,v,w); 96 } 97 find(n); 98 printf("%d\n",mark[n]); 99 } 100 return 0; 101 }