Skip to content

Commit 6c072fc

Browse files
Merge pull request #696 from eric-hjh/main
[황장현] 106차 라이브 코테 제출
2 parents 652d569 + 9808f1c commit 6c072fc

File tree

3 files changed

+192
-0
lines changed

3 files changed

+192
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const input = require('fs')
2+
.readFileSync(process.platform === 'linux' ? '/dev/stdin' : './input.txt')
3+
.toString()
4+
.trim()
5+
.split('\n')
6+
.map((el) => el.split(' ').map(Number));
7+
8+
function solution(input) {
9+
const n = input[0][0];
10+
const m = input[0][1];
11+
const busInfo = input.slice(2);
12+
const map = Array.from({ length: n }, () => Array(n).fill(Infinity));
13+
14+
for (let i = 0; i < n; i++) {
15+
map[i][i] = 0;
16+
}
17+
18+
for (const [start, end, cost] of busInfo) {
19+
map[start - 1][end - 1] = Math.min(map[start - 1][end - 1], cost);
20+
}
21+
22+
for (let k = 0; k < n; k++) {
23+
for (let i = 0; i < n; i++) {
24+
for (let j = 0; j < n; j++) {
25+
if (map[i][j] > map[i][k] + map[k][j]) {
26+
map[i][j] = map[i][k] + map[k][j];
27+
}
28+
}
29+
}
30+
}
31+
32+
return map
33+
.map((row) => row.map((cost) => (cost === Infinity ? 0 : cost)).join(' '))
34+
.join('\n');
35+
}
36+
37+
console.log(solution(input));
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
class MinHeap {
2+
constructor() {
3+
this.heap = [];
4+
}
5+
push(value) {
6+
this.heap.push(value);
7+
this.bubbleUp();
8+
}
9+
10+
pop() {
11+
if (this.heap.length === 1) return this.heap.pop();
12+
const min = this.heap[0];
13+
this.heap[0] = this.heap.pop();
14+
this.bubbleDown();
15+
return min;
16+
}
17+
18+
bubbleUp() {
19+
let index = this.heap.length - 1;
20+
21+
while (index > 0) {
22+
const parentIndex = Math.floor((index - 1) / 2);
23+
if (this.heap[parentIndex] <= this.heap[index]) break;
24+
[this.heap[parentIndex], this.heap[index]] = [
25+
this.heap[index],
26+
this.heap[parentIndex],
27+
];
28+
index = parentIndex;
29+
}
30+
}
31+
32+
bubbleDown() {
33+
let index = 0;
34+
const length = this.heap.length;
35+
36+
while (true) {
37+
const left = 2 * index + 1;
38+
const right = 2 * index + 2;
39+
let smallest = index;
40+
41+
if (left < length && this.heap[left] < this.heap[smallest]) {
42+
smallest = left;
43+
}
44+
45+
if (right < length && this.heap[right] < this.heap[smallest]) {
46+
smallest = right;
47+
}
48+
49+
if (smallest === index) break;
50+
51+
[this.heap[index], this.heap[smallest]] = [
52+
this.heap[smallest],
53+
this.heap[index],
54+
];
55+
56+
index = smallest;
57+
}
58+
}
59+
60+
least() {
61+
return this.heap[0];
62+
}
63+
64+
size() {
65+
return this.heap.length;
66+
}
67+
}
68+
69+
const input = require('fs')
70+
.readFileSync(process.platform === 'linux' ? '/dev/stdin' : './input.txt')
71+
.toString()
72+
.trim()
73+
.split('\n')
74+
.map((el) => el.split(' ').map(Number));
75+
76+
function solution(input) {
77+
const [N, M] = input[0];
78+
const adjList = Array.from({ length: N + 1 }, () => []);
79+
80+
for (let i = 1; i <= M; i++) {
81+
const [A, B, C] = input[i];
82+
adjList[A].push([B, C]);
83+
adjList[B].push([A, C]);
84+
}
85+
86+
function dijkstra(start) {
87+
const distances = Array(N + 1).fill(Infinity);
88+
const minHeap = new MinHeap();
89+
minHeap.push([0, start]);
90+
distances[start] = 0;
91+
92+
while (minHeap.size() > 0) {
93+
const [currentCost, currentNode] = minHeap.pop();
94+
95+
if (currentCost > distances[currentNode]) continue;
96+
97+
for (const [nextNode, nextCost] of adjList[currentNode]) {
98+
const newCost = currentCost + nextCost;
99+
100+
if (newCost < distances[nextNode]) {
101+
distances[nextNode] = newCost;
102+
minHeap.push([newCost, nextNode]);
103+
}
104+
}
105+
}
106+
107+
return distances[N];
108+
}
109+
110+
return dijkstra(1);
111+
}
112+
113+
console.log(solution(input));
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
function solution(m, n, puddles) {
2+
const dp = Array.from({ length: n }, () => Array(m).fill(0));
3+
4+
for (const puddle of puddles) {
5+
const [col, row] = puddle;
6+
7+
dp[row - 1][col - 1] = -1;
8+
}
9+
10+
for (let i = 0; i < m; i++) {
11+
if (dp[0][i] === -1) {
12+
break;
13+
}
14+
15+
dp[0][i] = 1;
16+
}
17+
18+
for (let i = 0; i < n; i++) {
19+
if (dp[i][0] === -1) {
20+
break;
21+
}
22+
23+
dp[i][0] = 1;
24+
}
25+
26+
for (let i = 1; i < n; i++) {
27+
for (let j = 1; j < m; j++) {
28+
if (dp[i][j] === -1) {
29+
continue;
30+
}
31+
32+
if (dp[i - 1][j] === -1 || dp[i][j - 1] === -1) {
33+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
34+
} else {
35+
dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % 1000000007;
36+
}
37+
}
38+
}
39+
40+
return dp[n - 1][m - 1];
41+
}
42+
console.log(solution(4, 3, [[2, 2]]));

0 commit comments

Comments
 (0)