第一次写堆,样例能过,但两个tle是什么问题,求教

P4779 【模板】单源最短路径(标准版)

ASL123 @ 2023-04-21 22:15:36

#include <bits/stdc++.h>

using namespace std;

const int lnf = INT_MAX/2;

struct Pair {
    int to, cost;
};

bool operator>(Pair a, Pair b){
    return a.cost < b.cost;
}

int main() {
    int n, m, start;
    scanf("%d%d%d", &n, &m, &start);
    start--;
    // Build Graph
    vector<vector<Pair>> g(n);
    for (int i = 0; i < m; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        u--; v--;
        g[u].push_back({ v, w });
    }

    vector<int> dis(n, lnf);
    dis[start] = 0;

    priority_queue<Pair, vector<Pair>, greater<Pair>> q;
    q.push({ start, 0 });
    while (!q.empty()) {
        Pair p = q.top(); q.pop();
        int x = p.to, cost = p.cost;
        if (dis[x] < cost) continue;
        for (Pair &y : g[x]) {
            int di = dis[x] + y.cost;
            if (di < dis[y.to]) {
                dis[y.to] = di;
                q.push({ y.to, di });
            }
        }
    }

    for (auto &d : dis) {
        printf("%d ", d);
    }

    return 0;
}

by Loic_ @ 2023-04-21 22:27:32

停止使用不关流同步的cin或开启O2。


by RP_INT_MAX @ 2023-04-21 22:34:22

@ASL123 cin 不关流/jk


by ASL123 @ 2023-04-21 23:34:02

@Loic_ 大佬的意思是什么,就是将cin替换成scanf吗,我全部修改了,或者开启O2吗,都试过了好像还是超时


by ASL123 @ 2023-04-21 23:34:44

@RP_INT_MAX 大佬是是将cin替换成scanf就行了吗


|