-
Notifications
You must be signed in to change notification settings - Fork 2
/
11779_josueyeon.cpp
88 lines (70 loc) · 1.68 KB
/
11779_josueyeon.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
vector<pair<int, int>> graph[1001];
// 부모 노드 저장 용도
int route[1001];
int dist[1001];
void dijkstra(int start)
{
priority_queue<pair<int, int>> pq;
// {가중치, 노드}
pq.push({0, start});
dist[start] = 0;
while (!pq.empty())
{
int w = pq.top().first * -1;
int now = pq.top().second;
pq.pop();
if (dist[now] < w)
continue;
for(int i = 0;i < graph[now].size();i++)
{
int cost = w + graph[now][i].second;
if (cost < dist[graph[now][i].first])
{
// 경로 저장을 위해 배열에 현재 노드를 넣어서 부모노드를 체크함
route[graph[now][i].first] = now;
dist[graph[now][i].first] = cost;
pq.push({cost * -1, graph[now][i].first});
}
}
}
}
int main()
{
// N(node), M(edge)
int N, M;
cin>>N>>M;
for(int i = 0;i < M;i++)
{
int a, b, c;
cin>>a>>b>>c;
// {노드, 가중치}
graph[a].push_back({b, c});
}
int start, target;
cin>>start>>target;
fill_n(dist, N + 1, 1e9);
fill_n(route, 1001, 0);
dijkstra(start);
cout<<dist[target]<<"\n";
stack<int> st;
st.push(target);
while(1)
{
target = route[target];
if (target == 0)
break;
st.push(target);
}
cout<<st.size()<<"\n";
while(!st.empty())
{
cout<<st.top()<<" ";
st.pop();
}
return 0;
}