// File: /workspace/github_cpp_makefile_star2_O2/ekg[P]sgd2/src/sgd2.hpp
// Function: sgd2::dijkstra(unsigned long, unsigned long, unsigned long*, unsigned long*, double*)
std::vector<term> dijkstra(uint64_t n, uint64_t m, uint64_t* I, uint64_t* J, double* V)
{
    auto graph = build_graph_weighted(n, m, I, J, V);

    uint64_t nC2 = (n*(n-1))/2;
    std::vector<term> terms;
    terms.reserve(nC2);

    uint64_t terms_size_goal = 0; // to keep track of when to stop searching i<j

    for (uint64_t source=0; source<n-1; source++) // no need to do final vertex because i<j
    {
        std::vector<bool> visited(n, false);
        std::vector<double> d(n, std::numeric_limits<double>::max()); // init 'tentative' distances to infinity

        // I am not using a fibonacci heap. I AM NOT USING A FIBONACCI HEAP
        // edges are used 'invisibly' here
        std::priority_queue<edge, std::vector<edge>, edge_comp> pq;

        d[source] = 0;
        pq.push(edge(source,0));

        terms_size_goal += n-source-1; // this is how many terms exist for i<j

        while (!pq.empty() && terms.size() <= terms_size_goal)
        {
            uint64_t current = pq.top().target;
            double d_ij = pq.top().weight;
            pq.pop();
            
            if (!visited[current]) // ignore redundant elements in queue
            {
                visited[current] = true;

                if (source < current) // only add terms for i<j
                {
                    double w_ij = 1.0 / (d_ij*d_ij);
                    terms.push_back(term(source, current, d_ij, w_ij));
                }
                for (edge e : graph[current])
                {
                    // here the edge is not 'invisible'
                    uint64_t next = e.target;
                    double weight = e.weight;

                    if (d_ij + weight < d[next]) // update tentative value of d 
                    {
                        d[next] = d_ij + weight;
                        pq.push(edge(next, d[next]));
                    }
                }
            }
        }
        if (terms.size() != terms_size_goal)
        {
            throw "graph is not strongly connected, or is not indexed from zero";
        }
    }
    return terms;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rsi, %r14
movq %rdi, %rbx
leaq -0xc0(%rbp), %rdi
callq 0x28ea
leaq -0x1(%r14), %rsi
movq %rsi, -0x90(%rbp)
imulq %r14, %rsi
shrq %rsi
xorpd %xmm0, %xmm0
movupd %xmm0, (%rbx)
andq $0x0, 0x10(%rbx)
movq %rbx, %rdi
callq 0x5e8e
xorl %r15d, %r15d
leaq -0x80(%rbp), %r12
leaq -0x60(%rbp), %r13
movq $0x0, -0x30(%rbp)
movq %r14, -0x88(%rbp)
cmpq -0x90(%rbp), %r15
je 0x2d38
movb $0x0, -0x80(%rbp)
leaq -0xe8(%rbp), %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x60c4
movabsq $0x7fefffffffffffff, %rax # imm = 0x7FEFFFFFFFFFFFFF
movq %rax, -0x80(%rbp)
leaq -0xa8(%rbp), %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x6124
xorpd %xmm0, %xmm0
movapd %xmm0, -0x80(%rbp)
andq $0x0, -0x70(%rbp)
movq -0xa8(%rbp), %rax
andq $0x0, (%rax,%r15,8)
movq %r15, -0x60(%rbp)
andq $0x0, -0x58(%rbp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x6184
movq %r15, -0x38(%rbp)
movq %r15, %rax
notq %rax
movq -0x30(%rbp), %rcx
addq %r14, %rcx
addq %rax, %rcx
movq %rcx, -0x30(%rbp)
movq 0x8(%rbx), %rax
subq (%rbx), %rax
movq -0x80(%rbp), %rcx
sarq $0x5, %rax
cmpq -0x78(%rbp), %rcx
je 0x2cff
cmpq -0x30(%rbp), %rax
ja 0x2cff
movq (%rcx), %r14
movsd 0x8(%rcx), %xmm0
movsd %xmm0, -0x40(%rbp)
movq %r12, %rdi
callq 0x61a4
leaq -0xe8(%rbp), %rdi
movq %r14, %rsi
callq 0x61c6
testq %rdx, (%rax)
jne 0x2c06
leaq -0xe8(%rbp), %rdi
movq %r14, %rsi
callq 0x61c6
orq %rdx, (%rax)
movq -0x38(%rbp), %rax
cmpq %r14, %rax
jae 0x2ca3
movsd -0x40(%rbp), %xmm2
movapd %xmm2, %xmm0
mulsd %xmm2, %xmm0
movsd 0x8386(%rip), %xmm1     # 0xb008
divsd %xmm0, %xmm1
movq %rax, -0x60(%rbp)
movq %r14, -0x58(%rbp)
movsd %xmm2, -0x50(%rbp)
movsd %xmm1, -0x48(%rbp)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x7d9c
movq -0xc0(%rbp), %rax
imulq $0x18, %r14, %rcx
movq (%rax,%rcx), %r14
movq 0x8(%rax,%rcx), %r15
cmpq %r15, %r14
je 0x2c06
movq (%r14), %rax
movsd 0x8(%r14), %xmm0
addsd -0x40(%rbp), %xmm0
movq -0xa8(%rbp), %rcx
movsd (%rcx,%rax,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2cf9
movsd %xmm0, (%rcx,%rax,8)
movq %rax, -0x60(%rbp)
movsd %xmm0, -0x58(%rbp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x6184
addq $0x10, %r14
jmp 0x2cb7
cmpq -0x30(%rbp), %rax
jne 0x2d59
movq %r12, %rdi
callq 0x6750
leaq -0xa8(%rbp), %rdi
callq 0x89de
leaq -0xe8(%rbp), %rdi
callq 0x61e8
movq -0x38(%rbp), %r15
incq %r15
movq -0x88(%rbp), %r14
jmp 0x2b77
leaq -0xc0(%rbp), %rdi
callq 0x606c
movq %rbx, %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x8
popq %rdi
callq 0x2090
leaq 0x8359(%rip), %rcx      # 0xb0c1
movq %rcx, (%rax)
movq 0xe24e(%rip), %rsi      # 0x10fc0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2200
jmp 0x2d80
jmp 0x2d94
movq %rax, %r14
jmp 0x2db8
movq %rax, %r14
jmp 0x2dac
jmp 0x2d94
jmp 0x2d94
jmp 0x2d94
jmp 0x2d94
jmp 0x2d94
movq %rax, %r14
leaq -0x80(%rbp), %rdi
callq 0x6750
leaq -0xa8(%rbp), %rdi
callq 0x89de
leaq -0xe8(%rbp), %rdi
callq 0x61e8
movq %rbx, %rdi
callq 0x77d2
leaq -0xc0(%rbp), %rdi
callq 0x606c
movq %r14, %rdi
callq 0x2210