-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1753.js
134 lines (109 loc) · 3.25 KB
/
1753.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const { kill } = require("process");
var readline = require("readline");
var r = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let input = [];
r.on("line", function (line) {
input.push(line);
}).on("close", function () {
main(input);
process.exit();
});
class Heap {
constructor() {
this.heap = [null];
}
getMin() {
return this.heap[1] ? this.heap[1] : null;
}
size() {
return this.heap.length - 1;
}
swap(a, b) {
[this.heap[a], this.heap[b]] = [this.heap[b], this.heap[a]];
}
push(value) {
this.heap.push(value);
let curIdx = this.heap.length - 1;
let parIdx = Math.floor(curIdx / 2);
while (curIdx > 1 && this.heap[parIdx].cost > this.heap[curIdx].cost) {
this.swap(curIdx, parIdx);
curIdx = parIdx;
parIdx = Math.floor(curIdx / 2);
}
}
pop() {
const min = this.heap[1];
if (this.heap.length <= 2) this.heap = [null];
else this.heap[1] = this.heap.pop();
let curidx = 1;
let leftidx = curidx * 2;
let rightidx = curidx * 2 + 1;
if (!this.heap[leftidx]) return min;
if (!this.heap[rightidx]) {
if (this.heap[leftidx].cost < this.heap[curidx].cost) {
this.swap(leftidx, curidx);
}
return min;
}
while (
leftidx < this.size() &&
(this.heap[leftidx].cost < this.heap[curidx].cost ||
this.heap[rightidx].cost < this.heap[curidx].cost)
) {
const minidx =
this.heap[leftidx].cost > this.heap[rightidx].cost ? rightidx : leftidx;
this.swap(minidx, curidx);
curidx = minidx;
leftidx = curidx * 2;
rightidx = curidx * 2 + 1;
}
return min;
}
}
function main(input) {
const [V, E] = input.shift().split(' ').map(Number)
const start = +input.shift()
const arr = {}
let result = new Array(V + 1).fill(Infinity);
let out = ""
const minHeap = new Heap();
input.forEach(str => {
const [from, to, cost] = str.split(' ').map(Number)
if (!arr[from]) {
arr[from] = []
}
arr[from].push([to, cost])
});
function dijkstra(start) {
visited = [];
result[start] = 0
minHeap.push({
vertex: start,
cost: 0,
});
while (minHeap.size() !== 0) {
let { vertex, cost } = minHeap.pop();
visited[vertex] = true
if (arr[vertex]) {
for (let i = 0; i < arr[vertex].length; i++) {
const [nextTo, nextCost] = arr[vertex][i]
if (visited[nextTo]) continue;
let data = cost + nextCost
if (data < result[nextTo]) {
minHeap.push({ vertex: nextTo, cost: data })
result[nextTo] = data
}
}
}
}
}
dijkstra(start)
for (let i = 1; i < result.length; i++) {
if (result[i] === Infinity) { out += "INF\n" }
else { out += `${result[i]}\n` }
}
console.log(out)
}