/*ACMer:MDK 2011-04-20 16:56:49 Accepted 2544 140 MS 4296 KB GNU C++*/ #include<iostream> #include<string.h> #include<limits.h> #include<stdio.h> #define MAXN 1005 #define INTMAX 1000000 using namespace std; int map[MAXN][MAXN],minl[MAXN],pre[MAXN]; int big = INT_MIN; int dijkstra(int N,int M,int s) { int n = ++big,v[MAXN]={0},k,i,j; for(minl[s]=0,i = 0;i<n;i++) { for(k=-1,j = 0;j<n;j++) if(!v[j]&&(k==-1||minl[j]<minl[k])) k=j; for(v[k]=1,j = 0;j<n;j++) if(!v[j]&&map[k][j]+minl[k]<minl[j]) minl[j]=map[k][j]+minl[k]; //一开始K打成J了,%>_<%,检查好久,不过一次AC了 } return minl[N]; } int main() { //freopen("d:\\1.txt","r",stdin); int N,M; while(cin>>N>>M&&(N||M)) { int a,b; memset(map,INTMAX,sizeof(map)); memset(minl,INTMAX,sizeof(minl)); memset(pre,0,sizeof(pre)); for(int i = 0,v;i<M;i++){ cin>>a>>b>>v;big = max(big,max(a,b)); if( v < map[a][b] ) map[a][b] = map[b][a] = v; }//,cout<<map[b][a]<<endl; cout<<dijkstra(N,M,1)<<endl; } }