1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| void Dijkstra(vector<vector<int>>& A, int v, int n) { vector<int> dist(n); vector<bool> S(n); for (int i = 0; i < n; i++) { dist[i] = A[v][i]; S[i] = false; } S[v] = true; for (int i = 0; i < n-1; i++) { int mindis = INT_MAX; int u = -1;
for (int j = 0; j < n; j++) { if (!S[j] && dist[j] < mindis) { mindis = dist[j]; u = j; } } if (u == -1) break; S[u] = true; for (int j = 0; j < n; j++) { if (!S[j] && A[u][j] != INT_MAX && dist[u] != INT_MAX && dist[u] + A[u][j] < dist[j]) { dist[j] = dist[u] + A[u][j]; } } } for (int i = 0; i < n; i++) { cout << dist[i] << " "; } }
|