1 条题解

  • 0
    @ 2026-11-2 15:26:41

    游戏关卡(Game Levels) 题解

    思路分析

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

    参考代码

    #include <iostream>
    #include <vector>
    #include <queue>
    #include <functional>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int n;
        if (!(cin >> n)) return 0;
        vector<vector<pair<int, long long>>> adj(n + 1);
        for (int i = 1; i <= n - 1; ++i) {
            long long a, b;
            int x;
            cin >> a >> b >> x;
            adj[i].push_back({i + 1, a});
            adj[i].push_back({x, b});
        }
        const long long INF = (1LL << 60);
        vector<long long> dist(n + 1, INF);
        priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;
        dist[1] = 0;
        pq.push({0, 1});
        while (!pq.empty()) {
            auto top = pq.top();
            pq.pop();
            long long d = top.first;
            int u = top.second;
            if (d > dist[u]) continue;
            for (auto& edge : adj[u]) {
                int v = edge.first;
                long long w = edge.second;
                if (dist[u] + w < dist[v]) {
                    dist[v] = dist[u] + w;
                    pq.push({dist[v], v});
                }
            }
        }
        cout << dist[n] << "\n";
        return 0;
    }
    
    • 1

    信息

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