HDU 2544

#include #include #include #include #include #include #include #include #define MAXN 105 #define MAXM 305 #define MOD 10000 #define inf 100000000 #define eps 1e-9 #define pi acos(-1.0) typedef long long LL; using namespace std; int n, m, c, u, v, w, mat[MAXN][MAXN], dis[MAXN]; bool vis[MAXN]; void Dijkstra() { memset(vis, false, sizeof(vis)); memset(dis, 0x3f, sizeof(dis)); dis[1] = 0; for (int i = 1; i < n; i++) { int k = -1; for (int j = 1; j <= n; j++) { if (!vis[j] && (k == -1 || dis[j] < dis[k])) k = j; } vis[k] = true; for (int j = 1; j <= n; j++) { if (!vis[j] && mat[k][j] && dis[k] + mat[k][j] < dis[j]) dis[j] = dis[k] + mat[k][j]; } } } int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); while (scanf("%d %d", &n, &m) && (n || m)) { memset(mat, 0, sizeof(mat)); for (int i = 1; i <= m; i++) { scanf("%d %d %d", &u, &v, &w); mat[u][v] = mat[v][u] = w; } Dijkstra(); printf("%d\n", dis[n]); } return 0; }

posted on 2012-11-28 13:55  Sure_Yi  阅读(91)  评论(0编辑  收藏  举报

导航