옳은 길로 바르게

  • 홈
  • 태그
  • 방명록

Algorithm & PS 2

Dijkstra: 최단거리, 근데 이제 음수가 아닌 가중치를 곁들인.

2025년 11월 5일Dijkstra는 가중치를 고려해 어떤 노드에서 다른 노드의 최단거리를 구할 때 사용할 수 있다.음수 가중치나 싸이클이 있을 때는 사용할 수 없다.BFS에서 queue에 enqueue할 때, 같은 거리를 가진 케이스들이 연속적으로 enqueue 됨을 보장해야 하는 것처럼Dijkstra에서는 priority_queue(heap)을 사용해 지금 가진 정보가 최신 정보임을 보장해야 한다.즉, pq를 믿어야 동작한다. pq는 greater와 같은 비교 기준을 가지고 노드를 배치하기 때문에기준을 잘 정하고기준에 맞게 heapPush 해야한다.계속 dist 배열을 빼먹고 있다는 것은 Dijkstra를 이해하지 못했기 때문에 생기는 일이다.

Algorithm & PS/Algorithm 2025.11.05

binarySearch

Use binarySearch to find an element in sorted or monotonic space.#include #include using namespace std;int binarySearch(vector &arr, int x){ int start = 0; int end = (int)arr.size() - 1; while (start Both average and worst case has O(log N) time complexity.

Algorithm & PS/Algorithm 2025.10.12
이전
1
다음
더보기
프로필사진

옳은 길로 바르게

sgwin 님의 블로그 입니다.

  • 분류 전체보기 (5)
    • Programming Language (3)
      • cpp (3)
    • Algorithm & PS (2)
      • Algorithm (2)
    • AI & ML (0)
      • Papers (0)

Tag

CPP, cpp algorithm, postfix, register cost, binarySearch in C++, binarySearch in Cpp, C++ algorithm, binarysearch, C++, prefix,

최근글과 인기글

  • 최근글
  • 인기글

최근댓글

공지사항

페이스북 트위터 플러그인

  • Facebook
  • Twitter

Archives

Calendar

«   2025/12   »
일 월 화 수 목 금 토
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

방문자수Total

  • Today :
  • Yesterday :

Copyright © Kakao Corp. All rights reserved.

티스토리툴바