#include<iostream> #include<cstring> #include<cstdio> using namespace std; const int maxn = 100; int map[maxn][maxn]; int dis[maxn]; int path[maxn]; int vis[maxn]; int n; void DIJ(int s) { memset(path, -1, sizeof(path)); memset(dis, 0x3f, sizeof(dis)); memset(vis, 0, sizeof(vis)); dis[s] = 0; while (1) { int k = 0; for (int j = 0; j <= n; ++j) { if (!vis[j] && dis[j] < dis[k])//找到未被收录且dis值最小的点 { k = j;//变为起点 } } if (!k)return; vis[k] = 1; for (int j = 1; j <= n; ++j) { if (dis[j] > dis[k] + map[k][j]) { dis[j] = dis[k] + map[k][j]; path[j] = k; } } } } void print(int x)//x为终点 { if (x == -1) return; //递归 print(path[x]); printf("%d->", x); }
原文地址:https://www.cnblogs.com/yangmenda/p/11725559.html
时间: 2024-10-10 07:51:30