Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🍪 문제 번호
Resolve: #102
🍊 문제 정의
input
output
🍑 알고리즘 설계
시간이 줄어든다는 것은
음수 사이클이 존재하는지
를 묻는 것과 같습니다.출발 지점과 관계 없이, 전체 그래프 안에서 음수 사이클 존재 여부를 검사하면 됩니다.
원래 밸만 포드 알고리즘은 시작 정점을 정해야 하지만,
이 문제에선 방문하지 않은 정점도 최단 거리를 갱신 한다면, 시작 정점을 아무데나 정해도 되고 심지어 없어도 됩니다.
해당 정점까지 최단 거리를 구하는 것이 아니라, 음수 사이클 여부만 판별 하면 되기 때문입니다.
이외에도 가상 정점을 만들어 모든 시작 정점을 한 번에 탐색하는 방법이 있습니다.
참고하면 좋을 자료 : https://www.acmicpc.net/board/view/72995
🥝 최악 수행 시간 복잡도
O(N * (M + W))
🍰 특이 사항 (Optional)