/* THE PROGRAM IS MADE BY PYY */ /*----------------------------------------------------------------------------// Copyright (c) 2011 panyanyany All rights reserved. URL : http://acm.hdu.edu.cn/showproblem.php?pid=2544 Name : 2544 最短路 Date : Thursday, January 5, 2012 Time Stage : half an hour Result: 5231194 2012-01-05 17:06:37 Accepted 2544 15MS 228K 1573 B C++ pyy Test Data : Review : //----------------------------------------------------------------------------*/ #include <stdio.h> #include <string.h> #define min(x, y) ((x) < (y) ? (x) : (y)) #define max(x, y) ((x) > (y) ? (x) : (y)) #define INF 0x3f3f3f3f #define MAXN 102 int n, m ; int map[MAXN][MAXN], dest[MAXN] ; bool used[MAXN] ; int dijkstra (const int start, const int end) { int i, j ; int iMinPath, MinPath ; memset (used, 0, sizeof (used)) ; memset (dest, INF, sizeof (dest)) ; for (i = start ; i <= end ; ++i) dest[i] = map[start][i] ; used[start] = true ; for (i = 1 ; i <= n ; ++i) { MinPath = INF ; for (j = 1 ; j <= n ; ++j) { if (!used[j] && dest[j] < MinPath) { iMinPath = j ; MinPath = dest[j] ; } } used[iMinPath] = true ; for (j = 1 ; j <= n ; ++j) { if (!used[j] && dest[iMinPath] + map[iMinPath][j] < dest[j]) dest[j] = dest[iMinPath] + map[iMinPath][j] ; } } return dest[end] ; } int main (void) { int i, j ; int x, y, c ; while (scanf ("%d%d", &n, &m), n | m) { memset (map, INF, sizeof (map)) ; for (i = 1 ; i <= m ; ++i) { scanf ("%d%d%d", &x, &y, &c) ; map[x][y] = map[y][x] = min (map[x][y], c) ; } printf ("%d\n", dijkstra (1, n)) ; } return 0 ; }
/* THE PROGRAM IS MADE BY PYY */ /*----------------------------------------------------------------------------// Copyright (c) 2011 panyanyany All rights reserved. URL : http://acm.hdu.edu.cn/showproblem.php?pid=2544 Name : 2544 最短路 Date : Thursday, January 5, 2012 Time Stage : half an hour Result: 5231253 2012-01-05 17:46:08 Accepted 2544 31MS 224K 1127 B C++ pyy Test Data : Review : //----------------------------------------------------------------------------*/ #include <stdio.h> #include <string.h> #define min(x, y) ((x) < (y) ? (x) : (y)) #define max(x, y) ((x) > (y) ? (x) : (y)) #define INF 0x3f3f3f3f #define MAXN 102 int n, m ; int map[MAXN][MAXN] ; int floyd (const int start, const int end) { int i, j, k ; for (k = 1 ; k <= n ; ++k) for (i = 1 ; i <= n ; ++i) for (j = 1 ; j <= n ; ++j) map[i][j] = min (map[i][j], map[i][k] + map[k][j]) ; return map[start][end] ; } int main (void) { int i, j ; int x, y, c ; while (scanf ("%d%d", &n, &m), n | m) { memset (map, INF, sizeof (map)) ; for (i = 1 ; i <= m ; ++i) { scanf ("%d%d%d", &x, &y, &c) ; map[x][y] = map[y][x] = min (map[x][y], c) ; } printf ("%d\n", floyd (1, n)) ; } return 0 ; }