How to speed up dijkstra Single source, single target with retreat?

I tried to solve the Alpha # 20 Prob C Dijkstra problem , and I get TLE on Case 31, which has 100000nodes from the 99999edge. I guess my code complexity is O (E lg V), which is about 499995. I assumed this was fast enough, but due to bad results, I speed it up a bit using the built-in code for backtracking and some optimizations like dijkstra breaking as soon as the node target is removed from the queue. I do not think that this should affect the results, as if the node was deleted, it would mean that the best path was found, and we can enjoy. I now lack ideas for optimizing this code, so they arrived here. Code follows:

#include <iostream>
#include <vector>
#include <set>
#include <cstdio>
#include <climits>
#include <limits>
using namespace std;

typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vii> vvii;

vi D;
vi parent;
vi path;
vvii graph;


void dijkstra(int i, int j)
{
        set<ii> Q;
        Q.insert(ii(0, i));
        D[i] = 0; parent[i] = -555;
        bool checked = false;
        while(!Q.empty())
        {
                ii top = *Q.begin();
            Q.erase(Q.begin());
            int topnode = top.second;
            for(vii::iterator it = graph[topnode].begin();it != graph[topnode].end();it++)
            {
                int v = it->first, d2 = it->second;
                if(D[v] > D[topnode] + d2)
                    {
                        if(D[v] != INT_MAX)
                        {
                                Q.erase(Q.find(ii(D[v], v)));
                        }
                        D[v] = D[topnode] + d2; parent[v] = topnode;
                        Q.insert(ii(D[v], v));
                        if(v == j)
                                checked = true;
                    }
            }
            if(checked)
            {
                if(Q.find(ii(D[j], j)) == Q.end())
                    break;
            }
        }
}

/* void backtrack(int n)
{
    if(parent[n] != -555)
    {
        path.push_back(n);
        backtrack(parent[n]);
    }
}

void backtrack2(int n)
{
    while(parent[n] != -555)
    {
        path.push_back(n);
        n = parent[n];
    }
} */

int main(void) {
    int n, m, x, y, z;
    scanf("%d%d", &n, &m);
    graph.clear(); graph.resize(n); D.resize(n, INT_MAX); parent.resize(n, -1);
    while(m--)
    {
        scanf("%d%d%d", &x, &y, &z);
        graph[x-1].push_back(ii(y-1, z));
        graph[y-1].push_back(ii(x-1, z));
    }
    dijkstra(0, n-1);
    if(D[n-1] == INT_MAX)
        printf("-1\n");
    else
    {
        int x = n-1;
        while(parent[x] != -555)
        {
            path.push_back(x);
            x = parent[x];
        }
        printf("1 ");
        for(int i = int(path.size())-1;i >= 0;i--)
        {
            printf("%d ", path[i]+1);
        }
        printf("\n");
    }
}

, , . , . - - - ( - ( )), . , , , priority_queue ( set), , .

!

+4
1

, O(E log V) .

, , , , , INT_MAX , . 10^5 int . , dijkstra , .

int long long s INT_MAX - , 10^11, , , .

+5

All Articles