[BOJ] 11779 최소비용 구하기2

Time Lapse :27min 38sec

11779.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
#include <iostream>
#include <queue>
#include <list>
#include <stack>
using namespace std;
#define INF 987654321
int n, m, f, e, c;
bool g[1001][1001];
struct edge{
int f, e, c;
};
bool operator<(edge a, edge b) {
return a.c > b.c;
}
priority_queue<edge> pq;
list<edge> l[1001];
stack<int> ans;
int cost[1001];
void func() {
for(auto edges : l[f])
pq.push({.f = f, .e = edges.e, .c = edges.c});
while(!pq.empty()) {
edge edg = pq.top();
pq.pop();
if(cost[edg.e] <= edg.c) continue;
cost[edg.e] = edg.c;
g[edg.f][edg.e] = true;
if(edg.e == e) return;
for(auto edges : l[edg.e])
pq.push({.f = edg.e, .e = edges.e, .c = edges.c + edg.c});
}
}
bool getans(int cur) {
if(cur == e) {
ans.push(e);
return true;
}
for(int i = 1; i<= n; i++) {
if(g[cur][i] && getans(i)) {
ans.push(cur);
return true;
}
}
return false;
}
int main() {
scanf("%d %d",&n, &m);
for(int i = 0; i < m; i++) {
scanf("%d %d %d",&f,&e,&c);
l[f].push_back({f,e,c});
}
scanf("%d %d",&f, &e);
for(int i = 1; i <= n; i++)
cost[i] = INF;
cost[f] = 0;
func();
printf("%d\n",cost[e]);
getans(f);
printf("%d\n",ans.size());
while(!ans.empty()) {
printf("%d ",ans.top());
ans.pop();
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/09/11/PS/BOJ/11779/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.