Skip to content

Commit 77d5ca6

Browse files
Create 11779.cpp
1 parent 9b022c1 commit 77d5ca6

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

0x1D/solutions/11779.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Authored by : BaaaaaaaaaaarkingDog
2+
// Co-authored by : -
3+
// http://boj.kr/4d78c57278294f0f9ab04ce7653c871a
4+
#include <bits/stdc++.h>
5+
using namespace std;
6+
7+
#define X first
8+
#define Y second
9+
10+
int v,e,st,en;
11+
12+
// {비용, 정점 번호}
13+
vector<pair<int,int>> adj[1005];
14+
const int INF = 0x3f3f3f3f;
15+
int d[1005]; // 최단 거리 테이블
16+
int pre[1005];
17+
int main(void) {
18+
ios::sync_with_stdio(0);
19+
cin.tie(0);
20+
cin >> v >> e;
21+
fill(d,d+v+1,INF);
22+
while(e--){
23+
int u,v,w;
24+
cin >> u >> v >> w;
25+
adj[u].push_back({w,v});
26+
}
27+
cin >> st >> en;
28+
29+
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>> > pq;
30+
d[st] = 0;
31+
// 우선순위 큐에 (0, 시작점) 추가
32+
pq.push({d[st],st});
33+
while(!pq.empty()){
34+
auto cur = pq.top(); pq.pop(); // {비용, 정점 번호}
35+
// 거리가 d에 있는 값과 다를 경우 넘어감
36+
if(d[cur.Y] != cur.X) continue;
37+
for(auto nxt : adj[cur.Y]){
38+
if(d[nxt.Y] <= d[cur.Y]+nxt.X) continue;
39+
// cur를 거쳐가는 것이 더 작은 값을 가질 경우
40+
// d[nxt.Y]을 갱신하고 우선순위 큐에 (거리, nxt.Y)를 추가
41+
d[nxt.Y] = d[cur.Y]+nxt.X;
42+
pq.push({d[nxt.Y],nxt.Y});
43+
pre[nxt.Y] = cur.Y;
44+
}
45+
}
46+
47+
cout << d[en] << '\n';
48+
vector<int> path;
49+
int cur = en;
50+
while(cur != st){
51+
path.push_back(cur);
52+
cur = pre[cur];
53+
}
54+
path.push_back(cur);
55+
reverse(path.begin(), path.end());
56+
cout << path.size() << '\n';
57+
for(auto x : path) cout << x << ' ';
58+
}

0 commit comments

Comments
 (0)