Memory Limit Exceeded

1. 使用bfs时候出现了这种错误

错误写法

Memory Limit Exceeded
void bfs()
{
    priority_queue<int, vector<int>, greater<int> > heap;
    heap.push(0);
    for (int i = 0; i <= n; i++) st[i] = false;
    st[0] = true;
    int cnt = 0;
    while (heap.size())
    {
        auto t = heap.top();
        // ans.push_back(t);
        cnt++;
        if (cnt >1)
        printf("%d%c", t, " \n"[cnt == n+1]);
        heap.pop();

/// 错误写法
        st[t] = true;
        for (int i = h[t]; i!= -1; i = ne[i])
        {
            int j = e[i];
            if (st[j]) continue;
            heap.push(j);

        }
    }
    return;
}
View Code

正确写法

Memory Limit Exceeded
void bfs()
{
    priority_queue<int, vector<int>, greater<int> > heap;
    heap.push(0);
    for (int i = 0; i <= n; i++) st[i] = false;
    st[0] = true;
    int cnt = 0;
    while (heap.size())
    {
        auto t = heap.top();
        // ans.push_back(t);
        cnt++;
        if (cnt >1)
        printf("%d%c", t, " \n"[cnt == n+1]);
        heap.pop();
        for (int i = h[t]; i!= -1; i = ne[i])
        {
            int j = e[i];
            if (st[j]) continue;
            heap.push(j);
            st[j] = true;
        }
    }
    return;
}
View Code

 

上一篇:MySQL事务锁问题-Lock wait timeout exceeded


下一篇:npm install 安装报错,Unhandled rejection RangeError: Maximum call stack size exceeded