//代码: //方法1:Dijkstra's Algorithm #include<stdio.h>
#include<math.h>
#include<string.h>
#define INF 0xfffffff
#define N 110
#define min(a, b)(a < b ? a : b) int maps[N][N];
int d[N], visit[N];
int n, m; void Init()
{
int i, j;
memset(visit, , sizeof(visit));
for(i = ; i <= n ; i++)
{
d[i] = INF;
for(j = ; j <= n ; j++)
maps[i][j] = INF;
}
} void Dij()
{
int i, j, x, min;
d[] = ;
for(i = ; i <= n ; i++)
{
min = INF;
for(j = ; j <= n ; j++)
{
if(!visit[j] && d[j] < min)
{
min = d[j];
x = j;
}
}
visit[x] = ;
for(j = ; j <= n ; j++)
{
if(!visit[j] && d[j] > d[x] + maps[x][j])
d[j] = d[x] + maps[x][j];
}
}
} int main()
{
int i, a, b, c;
while(scanf("%d%d", &n, &m), m + n != )
{
Init();
for(i = ; i <= m ; i++)
{
scanf("%d%d%d", &a, &b, &c);
maps[a][b] = min(maps[a][b], c);
maps[b][a] = maps[a][b];
}
Dij();
printf("%d\n", d[n]);
}
return ;
}//单元最短路 //方法2:弗洛伊德算法 #include<stdio.h>
#include<string.h>
#define N 110
#define INF 0xfffffff
#define min(a, b) (a < b ? a : b) int maps[N][N];
int n, m; void Init()
{
int i, j;
for(i = ; i <= n ; i++)
for(j = ; j <= n ; j++)
maps[i][j] = INF;
} void Floyd()
{
int i, k, j;
for(k = ; k <= n ; k++)
{
for(i = ; i <= n ; i++)
{
for(j = ; j <= n ; j++)
maps[i][j] = min(maps[i][j], maps[i][k] + maps[k][j]);
}
}
} int main()
{
int a, b, c, i;
while(scanf("%d%d", &n, &m), m + n != )
{
Init();
for(i = ; i <= m ; i++)
{
scanf("%d%d%d", &a, &b, &c);
maps[a][b] = min(maps[a][b], c);
maps[b][a] = maps[a][b];
}
Floyd();
printf("%d\n", maps[][n]);
}
return ;
}//多元最短路