[20250710] BOJ / G2 / 궁금한 민호 / 이종환 #428
Merged
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.
🧷 문제 링크
https://www.acmicpc.net/problem/1507
🧭 풀이 시간
30분
👀 체감 난이도
✏️ 문제 설명
모든 쌍의 도시 사이의 최소 이동 시간이 주어졌을 때, 이 나라에 존재할 수 있는 도로의 개수의 최솟값일 때, 모든 도로의 시간의 합을 구하자. 만약 입력값이 잘못되었다면 -1을 출력하자
🔍 풀이 방법
어떻게 할까 하다가 플로이드-워셜이 떠올랐다.
흔히 하는 최솟값을 구하는게 아니라, 이미 나와있는 결과가 최솟값인지 검증하면 되는 것이다.
도시 i에서 도시 j로 가는 데 걸리는 최소 이동 시간이 a라고 하였을 때, 이를 다른 도시 k를 거쳐가서 움직이는 시간과 비교하였다.
즉
distance[i][j]vs (distance[i][k] + distance[k][j])이때 만약 두 값이 같다면, 굳이 i와 j가 직접적으로 연결되어있을 필요가 없다.
만약 다른 도시를 거쳐가는 게 더 빠르다면 이건 입력값이 잘못되었으므로 -1을 출력한다.
위 두 케이스에 해당하지 않는다면 i와j가 직접적으로 연결되어있어야 하기에
distance[i][j]를 도로 시간의 합에 더해줬다.⏳ 회고
골드2...보다는 쉬운듯?