[백준] 1446 지름길
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int N, D;
cin >> N >> D;
vector<pair<int, pair<int, int>>> shortCuts;
for (int i = 0; i < N; ++i) {
int start, end, dist;
cin >> start >> end >> dist;
if (end > D) continue;
if (dist >= (end - start)) continue;
bool replaceDist = false;
for (int i = 0; i < shortCuts.size(); ++i) {
int shortCutEnd = shortCuts[i].first;
int shortCutStart = shortCuts[i].second.first;
if ((end == shortCutEnd) && (start == shortCutStart)) {
int& shortCutDist = shortCuts[i].second.second;
shortCutDist = min(shortCutDist, dist);
replaceDist = true;
break;
}
}
if (!replaceDist) shortCuts.push_back({ end, {start, dist} });
}
int dp[10001];
dp[0] = 0;
for (int pos = 1; pos <= D; ++pos) {
dp[pos] = dp[pos - 1] + 1;
for (int i = 0; i < shortCuts.size(); ++i) {
int shortCutEnd = shortCuts[i].first;
if (shortCutEnd == pos) {
int shortCutStart = shortCuts[i].second.first;
int shortCutDist = shortCuts[i].second.second;
dp[pos] = min(dp[pos], dp[shortCutStart] + shortCutDist);
}
}
}
cout << dp[D];
return 0;
}