-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinal_lab.cpp
More file actions
42 lines (40 loc) · 1.04 KB
/
Copy pathFinal_lab.cpp
File metadata and controls
42 lines (40 loc) · 1.04 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
#include <bits/stdc++.h>
using namespace std;
vector<pair<int,int>> adj_list[105];
int dis[105];
void Dijkstra(int source){
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
pq.push({0,source});
dis[source] = 0;
while(!pq.empty()){
pair<int,int> par = pq.top();
pq.pop();
int par_node = par.second;
int par_dis = par.first;
for(auto child : adj_list[par_node]){
int child_node = child.first;
int child_dis = child.second;
if(par_dis + child_dis < dis[child_node]){
dis[child_node] = par_dis + child_dis;
pq.push({dis[child_node], child_node});
}
}
}
}
int main(){
int n,e;
cin >> n >> e;
while(e--){
int a,b,c;
cin >> a >> b >> c;
adj_list[a].push_back({b,c});
}
for(int i = 0; i<n; i++){
dis[i] = INT_MAX;
}
Dijkstra(0);
for(int i = 0; i<n; i++){
cout << i << " -> " << dis[i] << endl;
}
return 0;
}