-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdjikstra.cpp
More file actions
65 lines (57 loc) · 1.5 KB
/
Copy pathdjikstra.cpp
File metadata and controls
65 lines (57 loc) · 1.5 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
60
61
62
63
64
65
//Directed Graph Djikstra
#include <bits/stdc++.h>
#define INF INT_MAX
using namespace std;
typedef pair<int, int> node;
typedef vector<node> graph;
vector<int> Djikstra(graph Graph[], int V, int src)
{
vector<int> weight(V, INF);
vector<bool> visited(V, false);
priority_queue<node, graph, greater<node>> min_heap;
weight[src] = 0;
min_heap.push(make_pair(weight[src], src));
while (!min_heap.empty())
{
int u = min_heap.top().second;
min_heap.pop();
visited[u] = true;
int n = Graph[u].size();
for (int i = 0; i < n; ++i)
{
int v = Graph[u][i].first;
int w = Graph[u][i].second;
if (weight[v] > weight[u] + w)
{
weight[v] = weight[u] + w;
if (!visited[v])
min_heap.push(make_pair(weight[v], v));
}
}
}
return weight;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int V, E;
cin >> V >> E;
graph Graph[V];
for (int i = 0; i < E; ++i)
{
int s, d, w;
cin >> s >> d >> w;
Graph[s].push_back(make_pair(d, w));
// Graph[d].push_back(make_pair(s, w)); // uncomment to make graph undirected
}
int src;
cin >> src;
vector<int> weights = Djikstra(Graph, V, src);
int n = weights.size();
for (int i = 0; i < n; ++i)
cout << i << " " << weights[i] << endl;
return 0;
}