1 条题解

  • 0
    @ 2026-11-2 14:09:02

    旅游巴士(Tourist Bus) 题解

    思路分析

    按题目要求使用对应数据结构与算法模拟/递推求解,注意数据范围边界与时间复杂度。

    参考代码

    #include <iostream>
    #include <vector>
    #include <queue>
    #include <tuple>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int n, m, k;
        if (!(cin >> n >> m >> k)) return 0;
        vector<vector<pair<int, long long>>> adj(n + 1);
        for (int i = 0; i < m; ++i) {
            int u, v;
            long long a;
            cin >> u >> v >> a;
            adj[u].push_back({v, a});
        }
        const long long INF = (1LL << 58);
        vector<vector<long long>> dist(n + 1, vector<long long>(k, INF));
        using State = tuple<long long, int, int>;
        priority_queue<State, vector<State>, greater<State>> pq;
        dist[1][0] = 0;
        pq.push({0, 1, 0});
        while (!pq.empty()) {
            auto [d, u, r] = pq.top();
            pq.pop();
            if (d != dist[u][r]) continue;
            int nr = (r + 1) % k;
            for (auto& edge : adj[u]) {
                int v = edge.first;
                long long a = edge.second;
                long long nd = d;
                if (nd < a) {
                    long long wait_k = (a - nd + k - 1) / k;
                    nd += wait_k * k;
                }
                nd += 1;
                if (nd < dist[v][nr]) {
                    dist[v][nr] = nd;
                    pq.push({nd, v, nr});
                }
            }
        }
        if (dist[n][0] >= INF) cout << -1 << "\n";
        else cout << dist[n][0] << "\n";
        return 0;
    }
    
    • 1

    信息

    ID
    8799
    时间
    1000ms
    内存
    256MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者