Skip to content

Commit 5e9f27f

Browse files
Merge pull request #695 from jinoo0306/main
[조진우] 106차 라이브 코테 제출
2 parents 6c072fc + 3d084d9 commit 5e9f27f

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const input = require("fs")
2+
.readFileSync(
3+
process.platform === "linux"
4+
? "/dev/stdin"
5+
: require("path").join(__dirname, "input.txt"),
6+
"utf8"
7+
)
8+
.toString()
9+
.trim()
10+
.split("\n");
11+
12+
function solution(input) {
13+
const n = Number(input[0]);
14+
const m = Number(input[1]);
15+
16+
const map = Array.from({ length: n + 1 }, () => Array(n + 1).fill(Infinity));
17+
18+
for (let i = 1; i <= n; i++) {
19+
map[i][i] = 0;
20+
}
21+
22+
for (let i = 2; i < 2 + m; i++) {
23+
const [a, b, c] = input[i].split(" ").map(Number);
24+
if (map[a][b] > c) {
25+
map[a][b] = c;
26+
}
27+
}
28+
29+
for (let k = 1; k <= n; k++) {
30+
for (let i = 1; i <= n; i++) {
31+
for (let j = 1; j <= n; j++) {
32+
if (map[i][j] > map[i][k] + map[k][j]) {
33+
map[i][j] = map[i][k] + map[k][j];
34+
}
35+
}
36+
}
37+
}
38+
39+
const answer = [];
40+
41+
for (let i = 1; i <= n; i++) {
42+
let line = [];
43+
for (let j = 1; j <= n; j++) {
44+
if (map[i][j] === Infinity) {
45+
line.push(0);
46+
} else {
47+
line.push(map[i][j]);
48+
}
49+
}
50+
answer.push(line.join(" "));
51+
}
52+
53+
return answer.join("\n");
54+
}
55+
56+
console.log(solution(input));
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
const input = require("fs")
2+
.readFileSync(
3+
process.platform === "linux"
4+
? "/dev/stdin"
5+
: require("path").join(__dirname, "input.txt"),
6+
"utf8"
7+
)
8+
.toString()
9+
.trim()
10+
.split("\n");
11+
12+
function solution(input) {
13+
const [n, m] = input[0].split(" ").map(Number);
14+
const map = Array.from({ length: n + 1 }, () => []);
15+
16+
for (let i = 1; i <= m; i++) {
17+
const [a, b, cost] = input[i].split(" ").map(Number);
18+
map[a].push([b, cost]);
19+
map[b].push([a, cost]);
20+
}
21+
22+
const distance = Array(n + 1).fill(Infinity);
23+
distance[1] = 0;
24+
25+
const heap = new MinHeap();
26+
heap.push([0, 1]);
27+
28+
while (!heap.isEmpty()) {
29+
const [curCost, curNode] = heap.pop();
30+
31+
if (distance[curNode] < curCost) continue;
32+
33+
for (let [next, nextCost] of map[curNode]) {
34+
const newCost = curCost + nextCost;
35+
if (newCost < distance[next]) {
36+
distance[next] = newCost;
37+
heap.push([newCost, next]);
38+
}
39+
}
40+
}
41+
42+
return distance[n];
43+
}
44+
45+
class MinHeap {
46+
constructor() {
47+
this.heap = [];
48+
}
49+
50+
push(value) {
51+
this.heap.push(value);
52+
let i = this.heap.length - 1;
53+
while (i > 0) {
54+
const parent = Math.floor((i - 1) / 2);
55+
if (this.heap[parent][0] <= this.heap[i][0]) break;
56+
[this.heap[parent], this.heap[i]] = [this.heap[i], this.heap[parent]];
57+
i = parent;
58+
}
59+
}
60+
61+
pop() {
62+
if (this.heap.length === 1) return this.heap.pop();
63+
const top = this.heap[0];
64+
this.heap[0] = this.heap.pop();
65+
66+
let i = 0;
67+
while (true) {
68+
let left = i * 2 + 1;
69+
let right = i * 2 + 2;
70+
let smallest = i;
71+
72+
if (
73+
left < this.heap.length &&
74+
this.heap[left][0] < this.heap[smallest][0]
75+
) {
76+
smallest = left;
77+
}
78+
if (
79+
right < this.heap.length &&
80+
this.heap[right][0] < this.heap[smallest][0]
81+
) {
82+
smallest = right;
83+
}
84+
if (smallest === i) break;
85+
86+
[this.heap[i], this.heap[smallest]] = [this.heap[smallest], this.heap[i]];
87+
i = smallest;
88+
}
89+
90+
return top;
91+
}
92+
93+
isEmpty() {
94+
return this.heap.length === 0;
95+
}
96+
}
97+
98+
console.log(solution(input));
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function solution(m, n, puddles) {
2+
const dp = Array.from({ length: n + 1 }, () => Array(m + 1).fill(0));
3+
4+
for (let [x, y] of puddles) {
5+
dp[y][x] = -1;
6+
}
7+
8+
dp[1][1] = 1;
9+
10+
for (let i = 1; i <= n; i++) {
11+
for (let j = 1; j <= m; j++) {
12+
if (i === 1 && j === 1) continue;
13+
if (dp[i][j] === -1) {
14+
dp[i][j] = 0;
15+
} else {
16+
dp[i][j] += (dp[i - 1][j] + dp[i][j - 1]) % 1000000007;
17+
}
18+
}
19+
}
20+
21+
return dp[n][m];
22+
}
23+
24+
console.log(solution(4, 3, [[2, 2]]));

0 commit comments

Comments
 (0)