회원가입
로그인
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
글쓰기
질문 도움말
자주묻는 질문
왜 시간 초과가 뜨는지 모르겠습니다. (소스코드 첨부)
22865번 - 가장 먼 곳
ahhpc2012
2년 전
0
어느 부분에서 시간초과가 발생하는 것인지 모르겠습니다..
const filePath = process.platform === 'linux' ? '/dev/stdin' : './Javascript/input.txt'; const input = require('fs').readFileSync(filePath).toString().trim().split('\n'); const N = +input[0]; const [A, B, C] = input[1].split(' ').map(Number); const M = +input[2]; const road = input.slice(3).map((e) => e.split(' ').map(Number)); const graph = Array.from({ length: N + 1 }, () => []); road.forEach(([s, e, v]) => { graph[s].push([e, v]); graph[e].push([s, v]); }); class PriorityQueue { constructor() { this.heap = []; } getSize = () => this.heap.length; getLCIdx = (pIdx) => pIdx * 2 + 1; getRCIdx = (pIdx) => pIdx * 2 + 2; getPIdx = (cIdx) => Math.floor((cIdx - 1) / 2); enqueue(key, value) { const node = { key, value }; this.heap.push(node); this.heapifyUp(); } heapifyUp() { let idx = this.heap.length - 1; const lastInsertedNode = this.heap[idx]; while (idx > 0) { const pIdx = this.getPIdx(idx); if (this.heap[pIdx].key > this.heap[idx].key) { this.heap[idx] = this.heap[pIdx]; idx = pIdx; } else break; } this.heap[idx] = lastInsertedNode; } dequeue() { const rootNode = this.heap[0]; const count = this.heap.length; if (count === 0) return undefined; if (count === 1) this.heap = []; else this.heapifyDown(); return rootNode; } heapifyDown() { this.heap[0] = this.heap.pop(); const rootNode = this.heap[0]; const count = this.heap.length; let idx = 0; while (this.getLCIdx(idx) < count) { const lcIdx = this.getLCIdx(idx); const rcIdx = this.getRCIdx(idx); const smallerCIdx = rcIdx < count && this.heap[rcIdx].key < this.heap[lcIdx].key ? rcIdx : lcIdx; if (this.heap[smallerCIdx] < this.heap[idx]) { this.heap[idx] = this.heap[smallerCIdx]; idx = smallerCIdx; } else break; } this.heap[idx] = rootNode; } } function dijkstra(start, distance) { distance[start] = 0; const pq = new PriorityQueue(); pq.enqueue(0, start); while (pq.getSize()) { const [dist, cur] = Object.values(pq.dequeue()); if (dist > distance[cur]) continue; for (let [v, c] of graph[cur]) { const cost = dist + c; if (cost < distance[v]) { distance[v] = cost; pq.enqueue(cost, v); } } } } const INF = Infinity; const totalDist = Array(N + 1).fill(INF); for (let point of [A, B, C]) { const distance = Array(N + 1).fill(INF); dijkstra(point, distance); for (let i = 1; i <= N; i++) { totalDist[i] = Math.min(totalDist[i], distance[i]); } } totalDist.shift(); const maxDist = Math.max(...totalDist); const answer = totalDist.findIndex((d) => d === maxDist); console.log(answer + 1);
댓글을 작성하려면
로그인
해야 합니다.
ahhpc2012 2년 전
어느 부분에서 시간초과가 발생하는 것인지 모르겠습니다..