-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
52 lines (47 loc) · 1.11 KB
/
main.cpp
File metadata and controls
52 lines (47 loc) · 1.11 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
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
struct graph {
int n, m;
vector<vector<pii>> g;
vector<int> d;
};
void get(graph& x) {
cin >> x.n >> x.m;
x.g.resize(x.n);
for(int i = 0; i < x.m; i ++) {
int u, v, w;
cin >> u >> v >> w;
x.g[u].push_back({w, v});
x.g[v].push_back({w, u});
}
}
void dijkstra(graph& x, int src) {
vector<bool> v(x.n, false);
x.d.assign(x.n, INT_MAX);
x.d[src] = 0;
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.push({0, src});
while(!pq.empty()) {
src = pq.top().second;
for(auto p: x.g[src]) {
x.d[p.second] = min(x.d[p.second], x.d[src] + p.first);
if(!v[p.second]) {
pq.push(p);
v[p.second] = true;
}
}
pq.pop();
}
}
int main() {
graph x;
get(x);
for(int i = 0; i < x.n; i ++) {
dijkstra(x, i);
for(auto d: x.d) {
cout << d << " ";
}
cout << endl;
}
}