結果

問題 No.3111 Toll Optimization
ユーザー よいちなすの
提出日時 2025-04-18 21:43:57
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
RE  
実行時間 -
コード長 2,189 bytes
コンパイル時間 794 ms
コンパイル使用メモリ 79,564 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-04-18 21:44:09
合計ジャッジ時間 10,635 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample RE * 3
other RE * 70
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
#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});
    }

    // dist[町番号][クーポン使用枚数]
    vector<vector<int>> dist(N + 1, vector<int>(K + 1, INT_MAX));
    dist[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 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;
}
0