Problem | 1446 지름길
실버 1
// 예제
5 150
0 50 10
0 50 20
50 100 10
100 151 10
110 140 90
start =0 , end = 50, 지름길 길이 = 60 이면 안가는 것이 낫다.
예제에서 만약 100에서 출발해서 151에 도착하는 길은 선택할 수 없다. 모든 지름길은 일방통행이고, 고속도로를 역주행할 수는 없다. 역주행 할 수 없기 때문이다.
const inputs = require("fs")
.readFileSync(process.platform === "linux" ? "dev/stdin" : "input.txt")
.toString()
.trim()
.split("\n");
const [n, d] = inputs[0].split(" ").map(Number);
let dist = Array.from(Array(d + 1).fill(Infinity));
const graph = Array.from(Array(d + 1), () => []);
for (let i = 0; i < n; i++) {
const [start, end, w] = inputs[i + 1].split(" ").map(Number);
if (end > d) continue;
if (end - start <= w) continue;
graph[start].push([end, w]);
}
let prev = -1;
for (let i = 0; i <= d; i++) {
if (i) prev = dist[i - 1];
dist[i] = Math.min(dist[i], prev + 1);
for (let [next, cost] of graph[i]) {
if (dist[next] > dist[i] + cost) {
dist[next] = dist[i] + cost;
}
}
}
console.log(dist[d]);