-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra.cpp
More file actions
59 lines (47 loc) · 1.26 KB
/
dijkstra.cpp
File metadata and controls
59 lines (47 loc) · 1.26 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <vector>
#include <queue>
#include <limits>
#include <fstream>
using namespace std;
const int INF = numeric_limits<int>::max();
vector<vector<pair<int, int>>> adj;
vector<int> dist;
void dijkstra(int source, int n) {
dist.assign(n, INF);
dist[source] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
pq.push({0, source});
while (!pq.empty()) {
int d = pq.top().first;
int u = pq.top().second;
pq.pop();
if (d > dist[u]) continue;
for (auto &edge : adj[u]) {
int v = edge.first;
int weight = edge.second;
if (dist[u] + weight < dist[v]) {
dist[v] = dist[u] + weight;
pq.push({dist[v], v});
}
}
}
}
int main() {
int n, m, source;
ifstream infile("data/graph.txt");
infile >> n >> m >> source;
adj.resize(n);
for (int i = 0; i < m; ++i) {
int u, v, w;
infile >> u >> v >> w;
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w); // Assuming undirected graph
}
dijkstra(source, n);
ofstream outfile("data/output.txt");
for (int i = 0; i < n; ++i) {
outfile << dist[i] << " ";
}
return 0;
}