-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11779_Dijkstra_BackTracking.cpp
More file actions
80 lines (69 loc) · 1.46 KB
/
11779_Dijkstra_BackTracking.cpp
File metadata and controls
80 lines (69 loc) · 1.46 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
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <climits>
using namespace std;
const int INF = INT_MAX;
const int MAX = 1000;
int n, m, A, B;
int dist[MAX + 1];
int idx[MAX + 1];
vector<pair<int, int>> graph[MAX + 1];
void input()
{
cin >> n >> m;
while (m--)
{
int a, b, c;
cin >> a >> b >> c;
graph[a].push_back(make_pair(b, c));
}
cin >> A >> B;
for (int i = 1; i <= n; i++) dist[i] = INF;
}
void solution()
{
priority_queue<pair<int, int>> pq;
pq.push(make_pair(0, A));
while (!pq.empty())
{
int cost = -pq.top().first;
int cur = pq.top().second;
pq.pop();
if (dist[cur] < cost) continue;
for (int i = 0; i < graph[cur].size(); i++)
{
int next = graph[cur][i].first;
int ncost = graph[cur][i].second;
if (dist[next] > cost + ncost)
{
dist[next] = cost + ncost;
idx[next] = cur;
pq.push(make_pair(-dist[next], next));
}
}
}
stack<int> ans;
ans.push(B);
while (ans.top() != A)
{
ans.push(idx[ans.top()]);
}
cout << dist[B] << '\n'
<< ans.size() << '\n';
while (!ans.empty())
{
cout << ans.top() << ' ';
ans.pop();
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
input();
solution();
return 0;
}