티스토리 뷰
https://www.acmicpc.net/problem/16562
class Node {
constructor(item) {
this.item = item;
this.next = null;
}
}
class Queue {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
push(item) {
const node = new Node(item);
if (this.head == null) {
this.head = node;
} else {
this.tail.next = node;
}
this.tail = node;
this.length += 1;
}
pop() {
const popItem = this.head;
this.head = this.head.next;
this.length -= 1;
return popItem.item;
}
}
const input = require('fs')
.readFileSync('./dev/stdin')
.toString()
.trim()
.split('\n')
.map((v) => v.split(' ').map(Number));
// 학생수, 친구관계 수 , 가지고 있는돈
// 친구비
// 친구관게...
const [N, M, K] = input[0];
const cost = input[1];
cost.unshift(null);
const friendLink = Array.from(Array(N + 1), () => []);
for (let i = 2; i < input.length; i++) {
const [x, y] = input[i];
friendLink[x].push(y);
friendLink[y].push(x);
}
const q = new Queue();
let visited = Array.from(Array(N + 1), () => false);
let need = 0;
for (let i = 1; i < N + 1; i++) {
if (visited[i]) continue;
q.push(i);
visited[i] = true;
let min = cost[i];
while (q.length > 0) {
const now = q.pop();
const nowCost = cost[now];
min = min > nowCost ? nowCost : min;
const flist = friendLink[now];
for (let f = 0; f < flist.length; f++) {
const next = flist[f];
if (visited[next]) continue;
visited[next] = true;
q.push(next);
}
}
need += min;
}
console.log(need > K ? 'Oh no' : need);
728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
Node.js) 백준 16120번: PPAP (0) | 2023.11.09 |
---|---|
Node.js) 백준 13975번: 파일합치기 3 (1) | 2023.11.09 |
Node.js) 백준 20040번: 사이클 게임 (0) | 2023.11.07 |
Node.js) 백준 5052번: 전화번호 목록 (1) | 2023.11.02 |
Node.js) 백준 1043번: 거짓말 (1) | 2023.11.01 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 롱베케이션
- BFS
- 면접질문
- 동적프로그래밍
- 서버점검
- 투포인터 연습
- 다이나밍프로그래밍
- 다이나믹프로그래밍
- 개발자면접
- create databases;
- 면접비
- 서버개발
- MOD
- DB 생성
- MySQL
- 최소공통조상
- 은둔청년체험
- create db
- 그래프
- 로드나인
- node.js
- 투포인터
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함