-
Notifications
You must be signed in to change notification settings - Fork 0
/
21.cpp
85 lines (43 loc) · 1.04 KB
/
21.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
#include <stdio.h>
#include <vector>
#include <queue>
using namespace std;
const int oo = 1000111000;
typedef pair<int, int> ii;
vector<ii> a[2309];
int n, m;
int d[2309];
void dijkstra() {
priority_queue<ii, vector<ii>, greater<ii>> pq;
for (int i = 1; i <= n; i++)
d[i] = oo;
d[1] = 0;
pq.push(ii(0, 1));
while (pq.size()) {
int u = pq.top().second;
int du = pq.top().first;
pq.pop();
if (du != d[u])
continue;
for (int i = 0; i < a[u].size(); i++) {
int v = a[u][i].second;
int uv = a[u][i].first;
if (d[v] > du + uv) {
d[v] = du + uv;
pq.push(ii(d[v], v));
}
}
}
}
int main() {
int p, q, m, w;
scanf("%d%d", &n, &m);
while (m--) {
scanf("%d%d%d", &p, &q, &w);
a[p].push_back(ii(w, q));
a[q].push_back(ii(w, p));
}
dijkstra();
for (int i = 1; i <= n; i++)
printf("d( 1 -> %d ) = %d\n", i, d[i]);
}