[20250818] BOJ / G2 / 칵테일 / 이종환 #684
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/1033
🧭 풀이 시간
60분
👀 체감 난이도
✏️ 문제 설명
총 재료 쌍 N-1개의 비율이 입력으로 주어진다. 이때, 칵테일을 만드는데 필요한 각 재료의 양을 구하는 프로그램을 작성하시오. 이때, 필요한 재료의 질량을 모두 더한 값이 최소가 되어야 한다. 칵테일을 만드는 재료의 양은 정수이고, 총 질량은 0보다 커야한다.
🔍 풀이 방법
결국 이 문제의 포인트는 두 개이다.
우선 1번의 경우, 하나의 그래프에 하나씩 노드를 추가하는 방식으로 접근하였다.
그리고 그러면서 기존에 붙어있는 노드를 a, 새로붙은 노드가 b이고, 비율이 A:B라면,
b에는 a값*B를 대입하였다. 이후 a노드에서부터 A를 전파하였다.
이 전파는 자신의 숫자를 받은 값으로 곱한 후, 연결된 노드 중 아직 전파되지 않은 노드를 찾아서 같은 로직을 하게 하는 것이다.
이렇게 총 비율을 구한 후, 모든 재료에 대해서 유클리드 호제법을 적용하여서 gcd를 구하여 이값으로 나누어주었다.
⏳ 회고
오랜만에 문제다운 문제를 푼 것 같다. 다만 e.to, e.from... 이렇게 표현하지 말고 배열로 했으면 코드가 훨씬 줄어들었을 것이다.