#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int N, M, map[1010][1010], dis[1010], hash[1010], cnt, dp[1010];
const int inf = 0x7fffffff;
void Dijsktra () {
dis[2] = 0;
for ( int i = 1; i <= N; i++ ) {
int pos, t = inf;
for ( int j = 1; j <= N; j++ ) {
if ( hash[j] == 0 ) {
if ( dis[j] < t ) {
t = dis[j]; pos = j;
}
}
}
hash[pos] = 1;
for ( int j = 1; j <= N; j++ ) {
if ( hash[j] == 0 ) {
if ( map[pos][j] != inf && dis[j] > dis[pos] + map[pos][j] ) {
dis[j] = dis[pos] + map[pos][j];
}
}
}
}
}
#include<string.h>
#include<stdlib.h>
int N, M, map[1010][1010], dis[1010], hash[1010], cnt, dp[1010];
const int inf = 0x7fffffff;
void Dijsktra () {
dis[2] = 0;
for ( int i = 1; i <= N; i++ ) {
int pos, t = inf;
for ( int j = 1; j <= N; j++ ) {
if ( hash[j] == 0 ) {
if ( dis[j] < t ) {
t = dis[j]; pos = j;
}
}
}
hash[pos] = 1;
for ( int j = 1; j <= N; j++ ) {
if ( hash[j] == 0 ) {
if ( map[pos][j] != inf && dis[j] > dis[pos] + map[pos][j] ) {
dis[j] = dis[pos] + map[pos][j];
}
}
}
}
}
int DFS ( int x ) {
if ( x == 2 ) {
cnt++; return 1;
}
if ( dp[x] ) {
cnt++; return dp[x];
}
for ( int i = 1; i <= N; i++ ) {
if ( hash[i] == 0 ) {
if ( map[x][i] != inf &&dis[i] < dis[x] ) {
hash[i] = 1;
dp[x] += DFS ( i );
hash[i] = 0;
}
}
}
return dp[x];
}
if ( x == 2 ) {
cnt++; return 1;
}
if ( dp[x] ) {
cnt++; return dp[x];
}
for ( int i = 1; i <= N; i++ ) {
if ( hash[i] == 0 ) {
if ( map[x][i] != inf &&dis[i] < dis[x] ) {
hash[i] = 1;
dp[x] += DFS ( i );
hash[i] = 0;
}
}
}
return dp[x];
}
int main () {
while ( scanf ( "%d", &N ), N ) {
scanf ( "%d", &M );
int x, y, val;
for ( int i = 1; i <= N; i++ ) {
dis[i] = inf ; hash[i] = 0;
for ( int j = 1; j <= N; j++ )
map[i][j] = inf;
}
for ( int i = 1; i <= M; i++ ) {
scanf ( "%d%d%d", &x, &y, &val );
if ( map[x][y] > val ) {
map[x][y] = map[y][x] = val;
}
}
Dijsktra ();
cnt = 0;
memset ( hash, 0, sizeof (hash) );
memset ( dp, 0, sizeof (dp) );
DFS (1)
printf ( "%d\n", cnt );
}
return 0;
}
while ( scanf ( "%d", &N ), N ) {
scanf ( "%d", &M );
int x, y, val;
for ( int i = 1; i <= N; i++ ) {
dis[i] = inf ; hash[i] = 0;
for ( int j = 1; j <= N; j++ )
map[i][j] = inf;
}
for ( int i = 1; i <= M; i++ ) {
scanf ( "%d%d%d", &x, &y, &val );
if ( map[x][y] > val ) {
map[x][y] = map[y][x] = val;
}
}
Dijsktra ();
cnt = 0;
memset ( hash, 0, sizeof (hash) );
memset ( dp, 0, sizeof (dp) );
DFS (1)
printf ( "%d\n", cnt );
}
return 0;
}
////////////////////////
#include<stdio.h>
#include<string.h>
int map[110][110],dis[110], hash[110], N, M, x, y, v;
const int inf = 0x7fffffff;
int iii = 0x7f7f7f7f;
int Dijkstra () {
for ( int i = 1; i <= N; ++ i ) {
dis[i] = inf;
hash[i] = 0;
}
dis[1] = 0;
for ( int i = 1; i <= N; ++ i ) {
int t = inf, pos = 0;
for ( int j = 1; j <= N; ++ j ) {
if ( !hash[j] ) {
if ( dis[j] < t ) {
t = dis[j];
pos = j;
}
}
}
hash[pos] = 1;
//if ( pos == N ) break;
for ( int j = 1; j <= N; ++ j ) {
if ( !hash[j] && map[pos][j] != inf ) {
if ( dis[j] > dis[pos] + map[pos][j] ) {
dis[j] = dis[pos] + map[pos][j];
}
}
}
}
return dis[N];
}
int main( )
{
while ( scanf ( "%d%d", &N, &M ) , N || M ) {
for ( int i= 1; i <= N; ++ i ) {
for ( int j = 1; j <= N; ++ j ) map[i][j] = inf;
}
// memset ( map, 0x7f, sizeof ( map ) );
for ( int i = 1; i <= M ; ++ i ) {
scanf ( "%d%d%d", &x, &y, &v );
map[x][y] = map[y][x] = v;
}
printf ( "%d\n", Dijkstra () );
}
return 0;
}
#include<string.h>
int map[110][110],dis[110], hash[110], N, M, x, y, v;
const int inf = 0x7fffffff;
int iii = 0x7f7f7f7f;
int Dijkstra () {
for ( int i = 1; i <= N; ++ i ) {
dis[i] = inf;
hash[i] = 0;
}
dis[1] = 0;
for ( int i = 1; i <= N; ++ i ) {
int t = inf, pos = 0;
for ( int j = 1; j <= N; ++ j ) {
if ( !hash[j] ) {
if ( dis[j] < t ) {
t = dis[j];
pos = j;
}
}
}
hash[pos] = 1;
//if ( pos == N ) break;
for ( int j = 1; j <= N; ++ j ) {
if ( !hash[j] && map[pos][j] != inf ) {
if ( dis[j] > dis[pos] + map[pos][j] ) {
dis[j] = dis[pos] + map[pos][j];
}
}
}
}
return dis[N];
}
int main( )
{
while ( scanf ( "%d%d", &N, &M ) , N || M ) {
for ( int i= 1; i <= N; ++ i ) {
for ( int j = 1; j <= N; ++ j ) map[i][j] = inf;
}
// memset ( map, 0x7f, sizeof ( map ) );
for ( int i = 1; i <= M ; ++ i ) {
scanf ( "%d%d%d", &x, &y, &v );
map[x][y] = map[y][x] = v;
}
printf ( "%d\n", Dijkstra () );
}
return 0;
}