关于队列中使用 pair
  • 板块灌水区
  • 楼主plokmnjiu
  • 当前回复3
  • 已保存回复3
  • 发布时间2024/10/20 17:04
  • 上次更新2024/10/20 19:08:21
查看原帖
关于队列中使用 pair
525033
plokmnjiu楼主2024/10/20 17:04

刚在做 P2296 寻找道路,使用了用 pair 的队列,获得了整整 10 分,爆了MLE

inline void bfs2() {
	queue<pair<int, int> > q;
	q.push(make_pair(s, 0));
    while (!q.empty()) {
        int x = q.front().first;
        int d = q.front().second; 
        q.pop();
        if (x == t && d) {
            cout << d;
            exit(0);
        }
        for (auto to : side[x])
            if (inroad[to]) 
    			q.push(make_pair(to, d + 1));
    }
}

看了看题解改成使用数组存距离,结果过了。

inline void bfs2() {
	queue<int> q;
	q.push(s);
	dis[s] = 0;
    while (!q.empty()) {
        int x = q.front();
        q.pop();
        if (x == t) {
            cout << dis[t];
            exit(0);
        }
        for (auto to : side[x]) {
            if (inroad[to] && !dis[to]) {
                dis[to] = dis[x] + 1;
                q.push(to);
            }
        }
    }
}

是因为 pair 比较耗空间吗

2024/10/20 17:04
加载中...