반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 소프티어
- 연결요소
- dfs
- 처우산정
- incr
- 13908
- upper_bound
- 오퍼레터
- @P0
- 성적평가
- msSQL
- 백트래킹
- Docker
- 6987
- 백준
- compose
- 매개변수탐색
- Kafka
- 기술면접
- OFFSET
- BOJ
- 이분탐색
- 퇴사통보
- 처우협의
- 경력
- 파라메트릭
- boj #19237 #어른 상어
- 물채우기
- BFS
- softeer
Archives
- Today
- Total
기술 블로그
11657번 타임머신 본문
728x90
반응형
https://www.acmicpc.net/problem/11657
벨만 포드 알고리즘이다.
전혀 기억하지 못 하여서, 구현하고 구글링하면서 풀었다.
47번 째 줄에 dist[here] != INF 가 있는 이유는
3 1
2 3 -10000
-1
이렇게 출력돼야 하는데,
-1 밑에
INF - 10000 까지 나온다.
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 | #include <iostream> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <cstring> #include <string> #include <math.h> #include <algorithm> using namespace std; #define INF 987654321 int N = 0, M = 0; vector<pair<int, int> > v[501]; int dist[501] = { 0, }; int main(void) { int A = 0, B = 0, C = 0; scanf("%d %d", &N, &M); for (int i = 0; i < M; i++) { scanf("%d %d %d", &A, &B, &C); v[A].push_back({ B, C }); } fill(dist, dist + 501, INF); // 시작 정점 dist[1] = 0; for (int i = 0; i < N; i++) { for (int here = 1; here <= N; here++) { for (int index = 0; index < v[here].size(); index++) { int next = v[here].at(index).first; if (dist[next] > dist[here] + v[here].at(index).second && dist[here] != INF) { if (i == N - 1) { // 음수 사이클 printf("-1\n"); return 0; } dist[next] = dist[here] + v[here].at(index).second; } } } } for (int i = 2; i <= N; i++) { if (dist[i] == INF) printf("-1\n"); else printf("%d\n", dist[i]); } return 0; } | cs |
728x90
반응형
'알고리즘 문제 > BOJ' 카테고리의 다른 글
1038번 감소하는 수 (0) | 2018.12.28 |
---|---|
1107번 리모컨 (0) | 2018.12.26 |
2577번 숫자의 개수 (0) | 2018.12.25 |
5567번 결혼식 (0) | 2018.12.24 |
9372번 상근이의 여행 (0) | 2018.12.19 |