Skip to content

[BOJ] 웜홀 #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

[BOJ] 웜홀 #106

wants to merge 1 commit into from

Conversation

elbica
Copy link
Collaborator

@elbica elbica commented Jul 31, 2022

🍪 문제 번호

Resolve: #102

🍊 문제 정의

input

  • 테스트 케이스 TC
  • 지점의 수 N(1 ≤ N ≤ 500), 도로의 개수 M(1 ≤ M ≤ 2500), 웜홀의 개수 W(1 ≤ W ≤ 200)
  • 각 도로의 정보는 S, E, T (0 ≤ T ≤ 10000)

output

  • 시간이 줄어드는 지점이 있으면 YES

🍑 알고리즘 설계

시간이 줄어든다는 것은 음수 사이클이 존재하는지를 묻는 것과 같습니다.
출발 지점과 관계 없이, 전체 그래프 안에서 음수 사이클 존재 여부를 검사하면 됩니다.

  • 이에 특화된 밸만 포드 알고리즘을 활용하였습니다.

원래 밸만 포드 알고리즘은 시작 정점을 정해야 하지만,
이 문제에선 방문하지 않은 정점도 최단 거리를 갱신 한다면, 시작 정점을 아무데나 정해도 되고 심지어 없어도 됩니다.
해당 정점까지 최단 거리를 구하는 것이 아니라, 음수 사이클 여부만 판별 하면 되기 때문입니다.

   // visited[next] != INF 조건이 생략되었음
       if (visited[next] > visited[here] + distance) {
                        visited[next] = visited[here] + distance;
                        if(i == n - 1) cycle = true;
                    }

이외에도 가상 정점을 만들어 모든 시작 정점을 한 번에 탐색하는 방법이 있습니다.

참고하면 좋을 자료 : https://www.acmicpc.net/board/view/72995

🥝 최악 수행 시간 복잡도

  • O(N * (M + W))

🍰 특이 사항 (Optional)

최단 거리 (밸만 포드)
@elbica elbica requested review from SHDPR, seonpilKim and raetic July 31, 2022 14:08
@elbica elbica self-assigned this Jul 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BOJ] 웜홀 / Gold 3
1 participant