-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1753.cpp
More file actions
64 lines (52 loc) · 1.25 KB
/
1753.cpp
File metadata and controls
64 lines (52 loc) · 1.25 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
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
bool visited[1001];
int dist[1001];
#define max_n 1001
#define max_m 100001
#define INF 1000000
vector<pair<int,int>>graph[max_m];
int n, m;
void dijkstra(int start)
{
int i;
int u, v;
int distu, distv;
vector<int> dist(n + 1, INF);
vector<int> prev(n + 1, -1);
dist[start] = 0;
prev[start] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>pq;
pq.push({ 0, start });
while (!pq.empty()) {
u = pq.top().second;
distu = pq.top().first;
pq.pop();
for (i = 0; i < graph[u].size(); i++) {
v = graph[u][i].first;
distv = distu + graph[u][i].second;
if (dist[v] > distv) {
dist[v] = distv;
prev[v] = u;
pq.push({ distv, v });
}
}
}
for (int i = 1; i <= n; i++)
printf("%d\n", dist[i]);
printf("\n");
}
int main(void)
{
scanf("%d %d", &n, &m);
int start;
scanf("%d", &start);
int u, v, w;
for (int i = 0; i < m; i++) {
scanf("%d %d %d", &u, &v, &w);
graph[u].push_back(make_pair( v,w ));
}
dijkstra(start);
}