#include #include #include #include using namespace std; struct Edge { int to, cost; }; struct State { int town, couponCount, cost; bool operator>(const State& other) const { return cost > other.cost; // コストが小さい方が優先 } }; int main() { int N, M, K; cin >> N >> M >> K; vector> graph(N + 1); for (int i = 0; i < M; i++) { int u, v, c; cin >> u >> v >> c; graph[u].push_back({v, c}); graph[v].push_back({u, c}); } // dist[町番号][クーポン使用枚数] vector> dist(N + 1, vector(K + 1, INT_MAX)); dist[1][0] = 0; // 町1にいて、クーポンを使わない状態でのコストは0 priority_queue, greater> pq; pq.push({1, 0, 0}); // (町1, クーポン使用0枚, コスト0) while (!pq.empty()) { State curr = pq.top(); pq.pop(); int town = curr.town; int couponCount = curr.couponCount; int cost = curr.cost; // すでにこの状態で最小コストを達成している場合 if (cost > dist[town][couponCount]) continue; // 隣接する町に遷移 for (const Edge& edge : graph[town]) { int nextTown = edge.to; int edgeCost = edge.cost; // クーポンを使わない場合 if (cost + edgeCost < dist[nextTown][couponCount]) { dist[nextTown][couponCount] = cost + edgeCost; pq.push({nextTown, couponCount, cost + edgeCost}); } // クーポンを使う場合(クーポン使用がまだ残っている場合) if (couponCount < K && cost < dist[nextTown][couponCount + 1]) { dist[nextTown][couponCount + 1] = cost; pq.push({nextTown, couponCount + 1, cost}); } } } // 最終的に町Nに到達するための最小コストを求める int result = INT_MAX; for (int i = 0; i <= K; i++) { result = min(result, dist[N][i]); } cout << result << endl; return 0; }