結果
問題 |
No.3111 Toll Optimization
|
ユーザー |
|
提出日時 | 2025-04-18 21:42:35 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 2,265 bytes |
コンパイル時間 | 946 ms |
コンパイル使用メモリ | 81,960 KB |
実行使用メモリ | 7,848 KB |
最終ジャッジ日時 | 2025-04-18 21:42:47 |
合計ジャッジ時間 | 10,277 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | RE * 3 |
other | RE * 70 |
ソースコード
#include <iostream> #include <vector> #include <queue> #include <algorithm> #include <climits> 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<vector<Edge>> 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}); } // dp[town][couponCount] := townに到達するための最小コスト vector<vector<int>> dp(N + 1, vector<int>(K + 1, INT_MAX)); dp[1][0] = 0; // 最初は町1にいて、クーポン0枚の状態 // 最小優先キュー(ダイクストラ法) priority_queue<State, vector<State>, greater<State>> pq; pq.push({1, 0, 0}); // 町1にクーポン0枚で0円のコスト while (!pq.empty()) { State current = pq.top(); pq.pop(); int town = current.town; int couponCount = current.couponCount; int cost = current.cost; // もし、すでに最適なコストが確定していればスキップ if (cost > dp[town][couponCount]) continue; // すべての隣接する町に対して遷移を試みる for (const Edge& edge : graph[town]) { int nextTown = edge.to; int bridgeCost = edge.cost; // クーポンを使わない場合 if (cost + bridgeCost < dp[nextTown][couponCount]) { dp[nextTown][couponCount] = cost + bridgeCost; pq.push({nextTown, couponCount, dp[nextTown][couponCount]}); } // クーポンを使う場合 if (couponCount + 1 <= K && cost < dp[nextTown][couponCount + 1]) { dp[nextTown][couponCount + 1] = cost; pq.push({nextTown, couponCount + 1, cost}); } } } // 町Nに到達するための最小コストを求める int result = INT_MAX; for (int k = 0; k <= K; ++k) { result = min(result, dp[N][k]); } cout << result << endl; return 0; }