-
Notifications
You must be signed in to change notification settings - Fork 1
[한종욱-16주차 알고리즘 스터디] #74
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
Ukj0ng
wants to merge
83
commits into
main
Choose a base branch
from
ukjong/week-16
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
oncsr
reviewed
May 12, 2025
Comment on lines
+57
to
+89
// 변형된 다익스트라 알고리즘 - 최대 중량 경로를 찾는 함수 | ||
private static void dijkstra(int start) { | ||
// 우선순위 큐를 중량이 큰 것이 우선순위가 높도록 구현 (최대 중량 경로를 찾기 위함) | ||
PriorityQueue<Node> pq = new PriorityQueue<>((o1, o2) -> Integer.compare(o2.cost, o1.cost)); | ||
|
||
// 모든 노드의 초기 중량을 0으로 설정 | ||
Arrays.fill(value, 0); | ||
|
||
// 시작점의 중량을 INF로 설정 | ||
value[start] = INF; | ||
|
||
// 시작점을 우선순위 큐에 추가 | ||
pq.add(new Node(start, value[start])); | ||
|
||
while (!pq.isEmpty()) { | ||
Node current = pq.poll(); // 현재 중량이 가장 큰 노드를 꺼냄 | ||
|
||
// 현재 노드의 중량이 이미 알려진 중량보다 작으면 무시 | ||
if (current.cost < value[current.dest]) continue; | ||
|
||
// 현재 노드와 연결된 모든 노드 탐색 | ||
for (Node next : graph[current.dest]) { | ||
// 현재 노드까지의 중량과 다음 간선의 중량 중 작은 값이 경로의 최대 중량 | ||
int nCost = Math.min(current.cost, next.cost); | ||
|
||
// 새로운 경로의 중량이 기존 중량보다 크면 갱신 | ||
if (nCost > value[next.dest]) { | ||
value[next.dest] = nCost; | ||
pq.add(new Node(next.dest, value[next.dest])); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
코드 보니까 변형된 다익스트라도 맞는 말인 거 같은데, 먼가 이론적으로는 Prim 알고리즘에 조금 더 가까워 보이기도 하네요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
그러네요. Prim은 다익스트라와 비슷한데 안쓴지 오래돼서 다익스트라의 변형이라고만 생각한 거 같아요.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
🚀 싸피 15반 알고리즘 스터디 16주차 [한종욱]
📌 문제 풀이 개요
✅ 문제 해결 여부
💡 풀이 방법
문제 1: 흩날리는 시험지 속에서 내 평점이 느껴진거야
문제 난이도
Gold 3
문제 유형
이분 탐색, 매개 변수 탐색
접근 방식 및 풀이
결정 문제입니다. 현수가 받을 수 있는 최대 점수를 결정하는데, 최대 점수를 이분 탐색으로 찾아서 현수가 해당 점수를 받을 수 있으면 더 높은 점수를 탐색하고, 받을 수 없으면 더 낮은 점수를 탐색하며 최대 점수를 탐색합니다.
시간복잡도:$O(n \times log(2,000,000))$
문제 2: 구슬 탈출 4
문제 난이도
Gold 1
문제 유형
구현, BFS
접근 방식 및 풀이
빨간 구슬과 파란 구슬의 위치를 추적해야 하고, 각 상태에 기울인 횟수를 저장하기 위해 Status 클래스로 관리했습니다.
각 상황을 BFS로 탐색했습니다.
주의 사항
파란 구슬이 먼저 들어가도 바로 BFS를 끝내지 않고 이후 빨간 구슬만 빼낼 수 있는 경우가 있는지 확인하기 위해
continue
해야 합니다.어떻게 움직여도 빨간 구슬을 빼낼 수 없다면 같은 곳을 반복해서 도착할 수 있으므로, 빨간 구슬과 파란 구슬의 방문을 탐색하기 위해 visited 배열을 [빨간 구슬.x][빨간 구슬.y][파란 구슬.x][파란 구슬.y]로 초기화해 사용했습니다.
구현
빨간 구슬과 파란 구슬 중 기울이는 방향에 따라 먼저 떨어지는 구슬이 다릅니다. 따라서, priority()를 통해 둘 중 뭐가 먼저 떨어지는지 판단했습니다.
그 후, 구슬을 움직이는데 목적지에 떨어지면 각 좌표를 -1로 했습니다.
둘 중 하나의 구슬이 들어가도 다음 상태에 도달할 수 있고, 두 구슬 다 구멍으로 빠지지 않고 보드에 있어도 다음 상태에 도달할 수 있기 때문에 이 점들을 고려해 BFS 완전 탐색을 통해 풀었습니다.
시간복잡도:$O((n \times m)^{2} \times max(n, m))$
문제 3: 세부
문제 난이도
Gold 3
문제 유형
dijkstra
접근 방식 및 풀이
MST로 분류되어 있지만 dijkstra 변형으로 풀었습니다. 문제를 재해석하면, s에서 e까지의 경로 중 지나간 가중치의 최소값의 최대값을 찾아야 합니다.
pq에서 cost(weight) 최대값부터 꺼내어, 기존 다익스트라의 형태를 변형해 풀었습니다.
시간복잡도: $ O(n*log(n+m)) $
문제 4: 커플 만들기
문제 난이도
Gold 2
문제 유형
DP
접근 방식 및 풀이
시간복잡도:
문제 5: Ignition
문제 난이도
Platium5
문제 유형
접근 방식 및 풀이
시간복잡도: