1 #include <cstdio> 2 3 struct road{ 4 int head; 5 int tail; 6 int value; 7 }; 8 9 #define Infinity 233333333 10 11 int main(){ 12 int n, m; 13 while (scanf("%d %d", &n, &m), n != 0){ 14 road roads[m]; 15 int dis[n]; 16 for (int i = 0; i < m; i++) 17 scanf("%d %d %d", &roads[i].head, &roads[i].tail, &roads[i].value); 18 dis[0] = 0; 19 for (int i = 1; i < n; i++) 20 dis[i] = Infinity; 21 for (int i = 0; i < n - 1; i++) 22 for (int j = 0; j < m; j++){ 23 int head, tail; 24 if (dis[roads[j].tail - 1] > dis[roads[j].head - 1]){ 25 head = roads[j].head - 1; 26 tail = roads[j].tail - 1; 27 } 28 else{ 29 head = roads[j].tail - 1; 30 tail = roads[j].head - 1; 31 } 32 if (dis[tail] > dis[head] + roads[j].value) 33 dis[tail] = dis[head] + roads[j].value; 34 } 35 printf("%d ", dis[n - 1]); 36 } 37 return 0; 38 }