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
- 백준 2447
- 해시 구현
- 백준 1406
- C/C++ 구현
- ㅣ풀이
- 백준
- AVL 시간 복잡도
- 조세퍼스 순열
- 버킷 정렬
- 풀이
- Stack 이란
- 시간 복잡도
- 5397
- qorwns
- 백준 1158
- 백준 5397
- 자료구조
- 게리멘더링2
- c#
- heap
- 스택의 특징
- 구현
- 백준 17822
- 백준 17779
- 해시구현
- 별 찍기 10
- 원판 돌리기
- dfs
- 1764
- 백준 17471
Archives
- Today
- Total
홍시홍의 프로그래밍
[백준 11657] 타임 머신 본문
https://www.acmicpc.net/problem/11657
11657번: 타임머신
첫째 줄에 도시의 개수 N (1 ≤ N ≤ 500), 버스 노선의 개수 M (1 ≤ M ≤ 6,000)이 주어진다. 둘째 줄부터 M개의 줄에는 버스 노선의 정보 A, B, C (1 ≤ A, B ≤ N, -10,000 ≤ C ≤ 10,000)가 주어진다.
www.acmicpc.net
요구사항
음의 경로가 있는지 확인(벨만 포드)
풀이
모든 노드에 대해서 간선만큼 반복하여 최저 경로를 갱신한다
모든 노드에 대해서 탐색하고 한번 더 탐색하였을 때, 감소되는 경로가 있으면 음의 경로가 존재하는 것이다.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct go {
int x;
int y;
};
int n, m;
int dist[501];
vector<go> v[501];
const int INF = 0x7fffffff;
int main()
{
cin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
v[x].push_back({ y,z });
}
for (int i = 1; i <= n; i++) {
dist[i] = INF;
}
dist[1] = 0;
int flag = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
for (auto p : v[j]) {
int next = p.x;
int D = p.y;
// cout << next << " " << D << endl;
if (dist[j] != INF && dist[next] > dist[j] + D)
{
dist[next] = dist[j] + D;
if (i == n) flag = 1;
}
}
}
}
if (flag == 1)
cout << "-1" << endl;
else {
for (int i = 2; i <= n; i++) {
printf("%d\n", dist[i] != INF ? dist[i] : -1);
}
}
}
'알고리즘 문제풀이 > 백준' 카테고리의 다른 글
[백준 11652] 카드 (0) | 2020.01.14 |
---|---|
[백준 3047]ABC (0) | 2020.01.14 |
[백준 1504] 특정한 최단 경로 (0) | 2020.01.14 |
[백준 4485] 젤다 (0) | 2020.01.14 |
[백준 1261] 알고스팟 (0) | 2020.01.14 |
Comments