-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph_Dijstra.cpp
122 lines (117 loc) · 3.47 KB
/
Graph_Dijstra.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
using ll = long long;
const int INF = 1e9;
const int MAX = 1001;
void Input(vector<pair<int, int>> adj[], int m)
{
int u, v, w;
for (int i = 0; i < m; i++)
{
cin >> u >> v >> w;
adj[u].push_back(make_pair(v, w));
adj[v].push_back(make_pair(u, w)); // Do thi vo huong
}
}
void Dijstra(vector<pair<int, int>> adj[], int n, int s)
{
vector<ll> d(n + 1, INF);
d[s] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> Q; // Min heap {Khoang cach, dinh}
Q.push(make_pair(0, s)); // Khoang cach tu s den s la 0
// Duyet cac dinh
while (Q.empty() == false)
{
// Chon khoang cach tu s den cac dinh la nho nhat
pair<int, int> top = Q.top();
Q.pop();
int u = top.second; // Dinh
int du = top.first; // Khoang cach tu s den u
if (du > d[u])
continue;
// Relaxation: Cap nhat khoang cach tu s den cac dinh ke cua u
for (int i = 0; i < adj[u].size(); i++)
{
pair<int, int> neighbor = adj[u][i];
int v = neighbor.first; // Dinh ke cua u
int w = neighbor.second; // Khoang cach tu u den v
if (d[v] > d[u] + w)
{
d[v] = d[u] + w;
Q.push(make_pair(d[v], v));
}
}
}
// In ra khoang cach tu s den cac dinh
for (int i = 1; i <= n; i++)
cout << d[i] << " ";
}
void DijstraPath(vector<pair<int, int>> adj[], int n, int s, int d, int parent[])
{
vector<ll> d(n + 1, INF);
d[s] = 0;
parent[s] = s;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> Q; // Min heap {Khoang cach, dinh}
Q.push(make_pair(0, s)); // Khoang cach tu s den s la 0
// Duyet cac dinh
while (Q.empty() == false)
{
// Chon khoang cach tu s den cac dinh la nho nhat
pair<int, int> top = Q.top();
Q.pop();
int u = top.second; // Dinh
int du = top.first; // Khoang cach tu s den u
if (du > d[u])
continue;
// Relaxation: Cap nhat khoang cach tu s den cac dinh ke cua u
for (int i = 0; i < adj[u].size(); i++)
{
pair<int, int> neighbor = adj[u][i];
int v = neighbor.first; // Dinh ke cua u
int w = neighbor.second; // Khoang cach tu u den v
if (d[v] > d[u] + w)
{
d[v] = d[u] + w;
Q.push(make_pair(d[v], v));
parent[v] = u;
}
}
}
}
void FindPath(vector < pair<int, int> adj[], int n, int s, int d, int parent[])
{
DijstraPath(adj, n, s, d, parent);
vector<int> path;
int u = d;
if (parent[u] == -1)
{
cout << "No path";
return;
}
else
{
while (u != s)
{
path.push_back(u);
u = parent[u];
}
path.push_back(s);
reverse(path.begin(), path.end());
for (int i = 0; i < path.size(); i++)
cout << path[i] << " ";
}
}
int main()
{
vector<pair<int, int>> adj[MAX]; // Adjacency list
int parent[MAX];
memset(parent, -1, sizeof(parent));
int n, m, s, d;
cin >> n >> m >> s >> d;
Input(adj, m);
Dijstra(adj, n, s);
FindPath(adj, n, s, d, parent);
return 0;
}