๐ŸŽฒ ๋ฐฑ์ค€ 1162๋ฒˆ ๋„๋กœํฌ์žฅ

Jeongeunยท2024๋…„ 1์›” 7์ผ
0

๋ฐฑ์ค€

๋ชฉ๋ก ๋ณด๊ธฐ
151/187

๋ฐฑ์ค€ 1162๋ฒˆ

๐Ÿงธ ์ด๋ฒˆ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ ๋†“์น˜๊ณ  ์žˆ๋˜ ๋ถ€๋ถ„๊นŒ์ง€ ํ™•์‹คํ•˜๊ฒŒ ์•Œ๊ฒŒ ๋œ ๊ฒƒ ๊ฐ™๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํž™์„ ์‚ฌ์šฉํ•ด ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฐธ๊ณ  ์ฝ”๋“œ ์—†์ด ๊ตฌํ˜„ํ•˜๊ธฐ์œ„ํ•ด์„œ ๋งŽ์€ ์—ฐ์Šต์ด ํ•„์š”ํ•  ๊ฒƒ ๊ฐ™๋‹ค. ๋‹ค์Œ์€ ์šฐ์„  ์ˆœ์œ„ ํ ๋ฌธ์ œ๋ฅผ ํ’€์–ด ์ต์ˆ™ํ•ด์ง€๋Š” ์‹œ๊ฐ„์„ ๊ฐ€์ ธ์•ผ๊ฒ ๋‹ค.
๐Ÿ’ก ํฌ์žฅ ๋„๋กœ ๊ฐœ์ˆ˜๋ฅผ ๋”ฐ๋กœ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด์•ผํ•œ๋‹ค.
๐ŸŽจ ์šฐ์„ ์ˆœ์œ„ ํ
๐ŸŽจ ์ฐธ๊ณ  ์ฝ”๋“œ

์ฝ”๋“œ

const fs = require('fs'); 
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
const [N, M, K] = input.shift().split(" ").map(Number);
const road = Array.from(new Array(N + 1), () => []);
for (let m = 0; m < M; m++) {
  const [a, b, c] = input[m].split(" ").map(Number);
  road[a].push([b, c]);
  road[b].push([a, c]);
}

class Heap {
  constructor() {
    this.heap = [];
  }
  getLeftChildIndex = (parentIndex) => parentIndex * 2 + 1;
  getRightChildIndex = (parentIndex) => parentIndex * 2 + 2;
  getParentIndex = (childIndex) => Math.floor((childIndex - 1) / 2);

  peek = () => this.heap[0];

  insert = (data) => {
    this.heap.push(data);
    this.heapifyUp();
  };

  heapifyUp = () => {
    let index = this.heap.length - 1;
    const lastInsertedNode = this.heap[index];

    while (index > 0) {
      const parentIndex = this.getParentIndex(index);

      if (this.heap[parentIndex][1] > lastInsertedNode[1]) {
        this.heap[index] = this.heap[parentIndex];
        index = parentIndex;
      } else break;
    }

    this.heap[index] = lastInsertedNode;
  };

  remove = () => {
    const count = this.heap.length;
    const rootNode = this.heap[0];

    if (count <= 0) return undefined;
    if (count === 1) this.heap = [];
    else {
      this.heap[0] = this.heap.pop();
      this.heapifyDown();
    }

    return rootNode;
  };

  heapifyDown = () => {
    let index = 0;
    const count = this.heap.length;
    const rootNode = this.heap[index];

    while (this.getLeftChildIndex(index) < count) {
      const leftChildIndex = this.getLeftChildIndex(index);
      const rightChildIndex = this.getRightChildIndex(index);

      const smallerChildIndex =
        rightChildIndex < count &&
        this.heap[rightChildIndex][1] < this.heap[leftChildIndex][1]
          ? rightChildIndex
          : leftChildIndex;

      if (this.heap[smallerChildIndex][1] <= rootNode[1]) {
        this.heap[index] = this.heap[smallerChildIndex];
        index = smallerChildIndex;
      } else break;
    }

    this.heap[index] = rootNode;
  };
}

class PriorityQueue extends Heap {
  constructor() {
    super();
  }

  isEmpty() {
    return this.heap.length <= 0;
  }

  insert(data) {
    this.insert(data);
  }

  remove() {
    this.remove();
  }

  length() {
    return this.heap.length;
  }
}

const dijkstra = () => {
  const distArr = Array.from(new Array(N + 1), () =>
    new Array(K + 1).fill(Infinity)
  );
  const queue = new PriorityQueue();
  queue.insert([1, 0, 0]);
  distArr[1][0] = 0;

  while (queue.length()) {
    const [city, total, paved] = queue.remove();

    if (distArr[city][paved] < total) continue;

    for (let i = 0; i < road[city].length; i++) {
      const [nextCity, dist] = road[city][i];
      const nextTotal = total + dist;

      if (nextTotal < distArr[nextCity][paved]) {
        distArr[nextCity][paved] = nextTotal;
        queue.insert([nextCity, nextTotal, paved]);
      }
      if (paved < K && total < distArr[nextCity][paved + 1]) {
        distArr[nextCity][paved + 1] = total;
        queue.insert([nextCity, total, paved + 1]);
      }
    }
  }
  return Math.min(...distArr[N]);
};

console.log(dijkstra());

0๊ฐœ์˜ ๋Œ“๊ธ€