1 条题解

  • 0
    @ 2026-11-2 14:08:55

    不在场的证明(Checking an Alibi) 题解

    思路分析

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

    参考代码

    #include <iostream>
    #include <vector>
    #include <queue>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int f, p, c;
        long long m;
        if (!(cin >> f >> p >> c >> m)) return 0;
        vector<vector<pair<int, long long>>> adj(f + 1);
        for (int i = 0; i < p; ++i) {
            int u, v;
            long long w;
            cin >> u >> v >> w;
            adj[u].push_back({v, w});
            adj[v].push_back({u, w});
        }
        const long long INF = (1LL << 58);
        vector<long long> dist(f + 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 [d, u] = pq.top();
            pq.pop();
            if (d != dist[u]) continue;
            for (auto& edge : adj[u]) {
                int v = edge.first;
                long long nd = d + edge.second;
                if (nd < dist[v]) {
                    dist[v] = nd;
                    pq.push({nd, v});
                }
            }
        }
        vector<int> ans;
        for (int i = 1; i <= c; ++i) {
            int pos;
            cin >> pos;
            if (dist[pos] <= m) ans.push_back(i);
        }
        cout << ans.size() << "\n";
        for (int x : ans) cout << x << "\n";
        return 0;
    }
    
    • 1

    信息

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