[BOJ] 11657 타임머신

Time Lapse :11min 21sec

11657.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
#include <iostream>
#include <algorithm>
#include <memory.h>
using namespace std;
#define INF 987654321
int edge[501][501], n, m, a, b, c;
int ans[501];
void dijkstra(int cur, int cost, int depth) {
if(depth > 500) {
printf("-1");
exit(0);
}
if(ans[cur] > cost) {
ans[cur] = cost;
for(int i = 1; i <= n; ++i)
if(edge[cur][i] != INF)
dijkstra(i,cost + edge[cur][i], depth + 1);
}
}
int main() {
scanf("%d %d",&n,&m);
for(int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j)
edge[i][j] = INF;
ans[i] = INF;
}
for(int i = 0; i < m; ++i) {
scanf("%d %d %d",&a,&b,&c);
edge[a][b] = min(edge[a][b],c);
}
dijkstra(1,0, 0);
for(int i = 2; i <= n; ++i)
printf("%d\n",ans[i] == INF ? -1 : ans[i]);
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/08/31/PS/BOJ/11657/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.