-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdijkstra.cpp
More file actions
36 lines (33 loc) · 1001 Bytes
/
dijkstra.cpp
File metadata and controls
36 lines (33 loc) · 1001 Bytes
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
#include <iostream>
#include <vector>
#include <map>
using namespace std;
class Dijkstra {
public:
vector<int> solve(vector<vector<int>> &graph, int src) {
int len = graph.size();
vector<int> rst = graph[src];
vector<bool> visited(len, false);
visited[src] = true;
int count = len - 1;
int idx;
int tmp_min;
while (count-- > 0) {
tmp_min = -1;
for (int i = 0; i < len; ++i) {
if (!visited[i] && rst[i] != -1 && (tmp_min == -1 || rst[i] < tmp_min)) {
idx = i;
tmp_min = rst[i];
}
}
visited[idx] = true;
for (int i = 0; i < len; ++i) {
if (!visited[i] && graph[idx][i] != -1 &&
(rst[i] == -1 || graph[idx][i] + rst[idx] < rst[i])) {
rst[i] = graph[idx][i] + rst[idx];
}
}
}
return rst;
}
};