-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcost2.cpp
68 lines (61 loc) · 1.39 KB
/
cost2.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
// https://www.acmicpc.net/problem/11779
#include <bits/stdc++.h>
using namespace std;
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(NULL);
int v, e;
cin>>v>>e;
//u, {cost, v};
vector<vector<pair<int, int>>> adj_list(v+1);
while (e--) {
int u, v, w;
cin>>u>>v>>w;
adj_list[u].push_back({w,v});
}
int st, en;
cin>>st>>en;
// cost, v
vector<int> d(v+1, 1e9);
d[st]=0;
vector<int> p(v+1);
priority_queue<pair<int, int>,
vector<pair<int, int>>,
greater<pair<int, int>>> pq;
pq.push({d[st],st});
while (!pq.empty()) {
auto cur = pq.top();
pq.pop();
int dist = cur.first;
int vtx = cur.second;
// 예전 정보는 무시
if (d[vtx]!=dist) {
continue;
}
for (auto nxt : adj_list[vtx]) {
int cost = nxt.first;
int nxt_vtx = nxt.second;
if (d[nxt_vtx]>dist+cost) {
d[nxt_vtx]=dist+cost;
pq.push({d[nxt_vtx],nxt_vtx});
p[nxt_vtx]=vtx;
}
}
}
cout << d[en] << '\n';
stack<int> s;
int r = en;
s.push(r);
while (st!=p[r]) {
r=p[r];
s.push(r);
}
s.push(st);
cout << s.size() << '\n';
while (!s.empty()) {
cout << s.top() << ' ';
s.pop();
}
return 0;
}