-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdijkstra_print_paths.cpp
More file actions
91 lines (81 loc) · 1.87 KB
/
dijkstra_print_paths.cpp
File metadata and controls
91 lines (81 loc) · 1.87 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
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
#include <bits/stdc++.h>
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
using namespace std;
typedef long long ll;
typedef long double ld;
#define endl "\n"
#define debug(args...) cout << (#args) << " = " << (args) << endl
#define MOD 1000000007
#define vi vector<int>
#define fl forward_list
#define pb push_back
#define pf push_front
#define read(st) getline(cin, st)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define INF 10000001
#define MAXN 1010
int dist[MAXN];
bool processed[MAXN];
vector<pair<int, int>> adj[MAXN];
priority_queue<pair<int, int>> q;
vector<int> predecessors;
int n, m;
vector<int> restore_path(int s, int t)
{
vector<int> path;
for (int v = t; v != s; v = predecessors[v])
path.push_back(v);
path.push_back(s);
reverse(path.begin(), path.end());
return path;
}
void dijkstra(int x)
{
for (int i = 0; i <= n + 1; i++)
dist[i] = INF;
predecessors.assign(n + 1, -1);
dist[x] = 0;
q.push({0, x});
while (!q.empty())
{
int a = q.top().second;
q.pop();
if (processed[a])
continue;
processed[a] = true;
for (auto u : adj[a])
{
int b = u.first;
int w = u.second;
if (dist[a] + w < dist[b])
{
dist[b] = dist[a] + w;
q.push({-dist[b], b});
predecessors[b] = a;
}
}
}
}
int main()
{
fastio;
int st = 0;
while (cin >> n >> m)
{
int s, t, b;
while (m--)
{
cin >> s >> t >> b;
adj[s].pb({t, b});
adj[t].pb({s, b});
}
dijkstra(st);
vector<int> path = restore_path(st, n + 1);
for (auto u : path)
cout << u << "->";
// cout << dist[n + 1] << endl;
}
return 0;
}