Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions live10/test106/문제1/황장현.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const input = require('fs')
.readFileSync(process.platform === 'linux' ? '/dev/stdin' : './input.txt')
.toString()
.trim()
.split('\n')
.map((el) => el.split(' ').map(Number));

function solution(input) {
const n = input[0][0];
const m = input[0][1];
const busInfo = input.slice(2);
const map = Array.from({ length: n }, () => Array(n).fill(Infinity));

for (let i = 0; i < n; i++) {
map[i][i] = 0;
}

for (const [start, end, cost] of busInfo) {
map[start - 1][end - 1] = Math.min(map[start - 1][end - 1], cost);
}

for (let k = 0; k < n; k++) {
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (map[i][j] > map[i][k] + map[k][j]) {
map[i][j] = map[i][k] + map[k][j];
}
}
}
}

return map
.map((row) => row.map((cost) => (cost === Infinity ? 0 : cost)).join(' '))
.join('\n');
}

console.log(solution(input));
113 changes: 113 additions & 0 deletions live10/test106/문제2/황장현.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
class MinHeap {
constructor() {
this.heap = [];
}
push(value) {
this.heap.push(value);
this.bubbleUp();
}

pop() {
if (this.heap.length === 1) return this.heap.pop();
const min = this.heap[0];
this.heap[0] = this.heap.pop();
this.bubbleDown();
return min;
}

bubbleUp() {
let index = this.heap.length - 1;

while (index > 0) {
const parentIndex = Math.floor((index - 1) / 2);
if (this.heap[parentIndex] <= this.heap[index]) break;
[this.heap[parentIndex], this.heap[index]] = [
this.heap[index],
this.heap[parentIndex],
];
index = parentIndex;
}
}

bubbleDown() {
let index = 0;
const length = this.heap.length;

while (true) {
const left = 2 * index + 1;
const right = 2 * index + 2;
let smallest = index;

if (left < length && this.heap[left] < this.heap[smallest]) {
smallest = left;
}

if (right < length && this.heap[right] < this.heap[smallest]) {
smallest = right;
}

if (smallest === index) break;

[this.heap[index], this.heap[smallest]] = [
this.heap[smallest],
this.heap[index],
];

index = smallest;
}
}

least() {
return this.heap[0];
}

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

const input = require('fs')
.readFileSync(process.platform === 'linux' ? '/dev/stdin' : './input.txt')
.toString()
.trim()
.split('\n')
.map((el) => el.split(' ').map(Number));

function solution(input) {
const [N, M] = input[0];
const adjList = Array.from({ length: N + 1 }, () => []);

for (let i = 1; i <= M; i++) {
const [A, B, C] = input[i];
adjList[A].push([B, C]);
adjList[B].push([A, C]);
}

function dijkstra(start) {
const distances = Array(N + 1).fill(Infinity);
const minHeap = new MinHeap();
minHeap.push([0, start]);
distances[start] = 0;

while (minHeap.size() > 0) {
const [currentCost, currentNode] = minHeap.pop();

if (currentCost > distances[currentNode]) continue;

for (const [nextNode, nextCost] of adjList[currentNode]) {
const newCost = currentCost + nextCost;

if (newCost < distances[nextNode]) {
distances[nextNode] = newCost;
minHeap.push([newCost, nextNode]);
}
}
}

return distances[N];
}

return dijkstra(1);
}

console.log(solution(input));
42 changes: 42 additions & 0 deletions live10/test106/문제3/황장현.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
function solution(m, n, puddles) {
const dp = Array.from({ length: n }, () => Array(m).fill(0));

for (const puddle of puddles) {
const [col, row] = puddle;

dp[row - 1][col - 1] = -1;
}

for (let i = 0; i < m; i++) {
if (dp[0][i] === -1) {
break;
}

dp[0][i] = 1;
}

for (let i = 0; i < n; i++) {
if (dp[i][0] === -1) {
break;
}

dp[i][0] = 1;
}

for (let i = 1; i < n; i++) {
for (let j = 1; j < m; j++) {
if (dp[i][j] === -1) {
continue;
}

if (dp[i - 1][j] === -1 || dp[i][j - 1] === -1) {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
} else {
dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % 1000000007;
}
}
}

return dp[n - 1][m - 1];
}
console.log(solution(4, 3, [[2, 2]]));