DJ堆优化,#5WA,蒟蒻求调

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

Otion @ 2023-05-28 17:38:45


#include <bits/stdc++.h>

using namespace std;
typedef pair<int, int> PII;
priority_queue<PII, vector<PII>, greater<PII>> line;
// 第一个int代表dis
// 第二个int代表编号

int total_area;
int total_road;
const int n = 5e5 + 100;
int start;
int head[n];
int area[n];
int next_area[n];
int idx;
int scale[n];
bool state[n];
long long dis[n];
void add(int a, int b, int c)
{
    area[idx] = b;
    next_area[idx] = head[a];
    head[a] = idx;
    scale[idx] = c;
    idx++;
}

void dj()
{
    line.push({0, start});
    while (line.size())
    {
        auto a = line.top();
        line.pop();
        int version = a.second;
        if (state[version])
        {
            continue;
        }
        else
        {
            state[version] = 1;
        }
        for (int i = head[version]; i != -1; i = next_area[i])
        {
            int now_area = area[i];
            if (dis[now_area] > dis[version] + scale[i])
            {
                dis[now_area] = dis[version] + scale[i];
                line.push({dis[now_area], now_area});
            }
        }
    }
}

int main()
{
    cin >> total_area >> total_road;
    cin >> start;
    memset(dis, 0x3f3f3f3f, sizeof(dis));
    memset(head, -1, sizeof(head));
    dis[start] = 0;
    for (int i = 1; i <= total_road; i++)
    {
        int bind_1, bind_2, bind_3;
        cin >> bind_1 >> bind_2 >> bind_3;
        add(bind_1, bind_2, bind_3);
    }
    dj();
    for (int i = 1; i <= total_area; i++)
    {
        if (dis[i] > 0x3f3f3f3f / 2)
        {
            long long a = pow(2, 31) - 1;
            cout << a << " ";
        }
        else
        {
            cout << dis[i] << " ";
        }
    }
    return 0;
}```

|