회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
nodejs 다익스트라 시간초과
13907번 - 세금
hobag6078
2년 전
0
다익스트라 코드 실행 과정에서 실행시간 제한에 걸리는데, 어떤 문제인지 잘 모르겠습니다
const fs = require('fs'); const input = fs.readFileSync('dev/stdin').toString().trim().split('\n'); const [N, M, _K] = input[0].split(' ').map(Number); const [S, E] = input[1].split(' ').map(Number); class PriorityQueue { heap = new Array(1000); size = 0; constructor(comparator) { this.comparator = comparator; } enq(item) { const heap = this.heap; const size = ++this.size; if (heap.length === size) { heap.length *= 2; } heap[size] = item; this.percolateUp(); } percolateUp() { const heap = this.heap; const size = this.size; const comparator = this.comparator; const item = heap[size]; let pos = size; while (pos > 1) { const parentPos = Math.floor(pos / 2); const parent = heap[parentPos]; if (comparator(item, parent) >= 0) { break; } heap[pos] = parent; pos = parentPos; } heap[pos] = item; } deq() { const heap = this.heap; const item = heap[1]; if (item === undefined) { return; } const size = --this.size; heap[1] = heap[size + 1]; heap[size + 1] = undefined; this.percolateDown(); return item; } percolateDown() { const heap = this.heap; const size = this.size; const comparator = this.comparator; const item = heap[1]; let pos = 1; while (pos * 2 <= size) { let childPos = pos * 2 + 1; if (childPos > size || comparator(heap[pos * 2], heap[childPos]) < 0) { childPos = pos * 2; } const child = heap[childPos]; if (comparator(item, child) <= 0) { break; } heap[pos] = child; pos = childPos; } heap[pos] = item; } isEmpty() { return this.size == 0; } } const minHeap = new PriorityQueue((a, b) => a[2] - b[2]); const edges = Array.from({ length: N + 1 }, () => Array.from({ length: N + 1 }, () => Infinity), ); for (let i = 2; i < 2 + M; i++) { const [a, b, cost] = input[i].split(' ').map(Number); edges[a][b] = Math.min(edges[a][b], cost); edges[b][a] = Math.min(edges[b][a], cost); } const dist = Array.from({ length: N + 1 }, () => Array.from({ length: N + 1 }, () => Infinity), ); minHeap.enq([0, S, 0]); dist[0][S] = 0; while (!minHeap.isEmpty()) { const [count, node, currentDist] = minHeap.deq(); if (dist[count][node] < currentDist) continue; for (let next = 1; next <= N; next++) if ( edges[node][next] != Infinity && dist[count + 1][next] > currentDist + edges[node][next] ) { dist[count + 1][next] = currentDist + edges[node][next]; if (count + 1 < N - 1) minHeap.enq([count + 1, next, currentDist + edges[node][next]]); } } const res = []; let diff = 0; const findMinDist = () => { let min = Infinity; for (let n = 0; n <= N; n++) min = Math.min(min, dist[n][E] + n * diff); return min; }; res.push(findMinDist()); for (let i = 2 + M; i < input.length; i++) { const tax = Number(input[i]); diff += tax; res.push(findMinDist()); } console.log(res.join('\n'));
댓글을 작성하려면
로그인
해야 합니다.
hobag6078 2년 전
다익스트라 코드 실행 과정에서 실행시간 제한에 걸리는데, 어떤 문제인지 잘 모르겠습니다