Skip to content
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

개선된 다익스트라 자바 코드 질문 #203

Open
Ahrang777 opened this issue Apr 19, 2023 · 0 comments
Open

개선된 다익스트라 자바 코드 질문 #203

Ahrang777 opened this issue Apr 19, 2023 · 0 comments

Comments

@Ahrang777
Copy link

Ahrang777 commented Apr 19, 2023

int cost = d[now] + graph.get(now).get(i).getDistance();

개선된 다익스트라 자바 코드에서 cost 부분이 dist 가 아닌 d[now] 가 오는게 잘 이해가 안됩니다.

아래 내용의 설명을 봤을때 dist가 d[now] 보다 작거나 같은 경우 이후 for문을 돌릴 수 있는 것 같은데
굳이 dist보다 크거나 같은 d[now]로 cost를 계산하는 이유가 있나요? 단순히 생각했을때 더 작거나 같은 dist로 cost를 계산하는게
맞는거 같은데 해당 부분이 잘 이해가 안됩니다. 실제로 c++, 파이썬에서는 dist로 계산하셨는데 자바만 다르더라구요 이유가 있을까요?

// 현재 노드가 이미 처리된 적이 있는 노드라면 무시
if (d[now] < dist) continue;

// 현재 노드와 연결된 다른 인접한 노드들을 확인
for (int i = 0; i < graph.get(now).size(); i++) {
        int cost = d[now] + graph.get(now).get(i).getDistance();
        // 현재 노드를 거쳐서, 다른 노드로 이동하는 거리가 더 짧은 경우
        if (cost < d[graph.get(now).get(i).getIndex()]) {
            d[graph.get(now).get(i).getIndex()] = cost;
            pq.offer(new Node(graph.get(now).get(i).getIndex(), cost));
        }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant