// File: /workspace/github_cpp_makefile_star2_O3/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 -0xf0(%rbp), %rdi
callq 0x2dae
leaq -0x1(%r14), %r15
movq %r15, %rsi
movq %r14, -0x38(%rbp)
imulq %r14, %rsi
shrq %rsi
xorpd %xmm0, %xmm0
movupd %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
movq %rbx, %rdi
callq 0x744e
movq %r15, -0xa8(%rbp)
testq %r15, %r15
movq -0x38(%rbp), %rsi
je 0x332f
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
leaq -0x60(%rbp), %r12
leaq -0x88(%rbp), %r13
addq $0x3f, %rax
movq %rax, -0xb0(%rbp)
movq $0x0, -0x30(%rbp)
xorl %r14d, %r14d
movq %r14, -0x40(%rbp)
movb $0x0, -0x60(%rbp)
leaq -0xd8(%rbp), %rdi
movq %r12, %rdx
movq %r13, %rcx
movq %rsi, %r14
callq 0x7766
movabsq $0x7fefffffffffffff, %rax # imm = 0x7FEFFFFFFFFFFFFF
movq %rax, -0x60(%rbp)
leaq -0xa0(%rbp), %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x77cc
xorl %ecx, %ecx
movq %rcx, -0x50(%rbp)
xorpd %xmm0, %xmm0
movapd %xmm0, -0x60(%rbp)
movq -0xa0(%rbp), %rax
movq -0x40(%rbp), %r14
movq %rcx, (%rax,%r14,8)
movq %r14, -0x88(%rbp)
movq %rcx, -0x80(%rbp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x7858
movq %r14, %rax
notq %rax
movq -0x30(%rbp), %rcx
addq -0x38(%rbp), %rcx
addq %rax, %rcx
movq %rcx, -0x30(%rbp)
movq -0x60(%rbp), %rdi
cmpq -0x58(%rbp), %rdi
je 0x32b0
movq 0x8(%rbx), %rax
subq (%rbx), %rax
sarq $0x5, %rax
cmpq -0x30(%rbp), %rax
ja 0x32b0
movq (%rdi), %r15
movsd 0x8(%rdi), %xmm0
movsd %xmm0, -0x68(%rbp)
movq %r12, %rdi
callq 0x78f8
leaq 0x3f(%r15), %rax
testq %r15, %r15
cmovnsq %r15, %rax
sarq $0x6, %rax
shlq $0x3, %rax
addq -0xd8(%rbp), %rax
movq %r15, %rcx
andq -0xb0(%rbp), %rcx
xorl %edx, %edx
movabsq $-0x8000000000000000, %rsi # imm = 0x8000000000000000
cmpq %rsi, %rcx
setbe %dl
movl $0x1, %esi
movl %r15d, %ecx
shlq %cl, %rsi
movq -0x8(%rax,%rdx,8), %rcx
btq %r15, %rcx
jb 0x314e
leaq -0x8(,%rdx,8), %rdx
orq %rsi, %rcx
movq %rcx, (%rax,%rdx)
movq -0x40(%rbp), %rax
cmpq %r15, %rax
jae 0x3251
movsd -0x68(%rbp), %xmm2
movapd %xmm2, %xmm0
mulsd %xmm2, %xmm0
movsd 0x7e05(%rip), %xmm1     # 0xb008
divsd %xmm0, %xmm1
movq %rax, -0x88(%rbp)
movq %r15, -0x80(%rbp)
movsd %xmm2, -0x78(%rbp)
movsd %xmm1, -0x70(%rbp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x3246
movupd -0x88(%rbp), %xmm0
movupd -0x78(%rbp), %xmm1
movupd %xmm1, 0x10(%rsi)
movupd %xmm0, (%rsi)
addq $0x20, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x3251
movq %rbx, %rdi
movq %r13, %rdx
callq 0x8b9c
movq -0xf0(%rbp), %rax
leaq (%r15,%r15,2), %rcx
movq (%rax,%rcx,8), %r15
movq 0x8(%rax,%rcx,8), %r14
cmpq %r14, %r15
je 0x314e
movq (%r15), %rax
movsd 0x8(%r15), %xmm0
addsd -0x68(%rbp), %xmm0
movq -0xa0(%rbp), %rcx
movsd (%rcx,%rax,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x32aa
movsd %xmm0, (%rcx,%rax,8)
movq %rax, -0x88(%rbp)
movsd %xmm0, -0x80(%rbp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x7858
addq $0x10, %r15
jmp 0x3265
movq 0x8(%rbx), %rax
subq (%rbx), %rax
sarq $0x5, %rax
cmpq -0x30(%rbp), %rax
jne 0x3350
testq %rdi, %rdi
je 0x32d6
movq -0x50(%rbp), %rsi
subq %rdi, %rsi
callq 0x2170
movq -0xa0(%rbp), %rdi
testq %rdi, %rdi
movq -0x40(%rbp), %r14
je 0x32f5
movq -0x90(%rbp), %rsi
subq %rdi, %rsi
callq 0x2170
movq -0xd8(%rbp), %rdi
testq %rdi, %rdi
je 0x331b
movq -0xb8(%rbp), %rsi
subq %rdi, %rsi
callq 0x2170
movq $0x0, -0xd8(%rbp)
incq %r14
cmpq -0xa8(%rbp), %r14
movq -0x38(%rbp), %rsi
jne 0x30c5
leaq -0xf0(%rbp), %rdi
callq 0x76ae
movq %rbx, %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x8, %edi
callq 0x2080
leaq 0x7d60(%rip), %rcx      # 0xb0c1
movq %rcx, (%rax)
movq 0xbc55(%rip), %rsi      # 0xefc0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2220
jmp 0x337b
jmp 0x3389
jmp 0x3389
movq %rax, %r14
jmp 0x33ec
movq %rax, %r14
jmp 0x33bc
jmp 0x3389
jmp 0x3389
movq %rax, %r14
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x33a1
movq -0x50(%rbp), %rsi
subq %rdi, %rsi
callq 0x2170
movq -0xa0(%rbp), %rdi
testq %rdi, %rdi
je 0x33bc
movq -0x90(%rbp), %rsi
subq %rdi, %rsi
callq 0x2170
movq -0xd8(%rbp), %rdi
testq %rdi, %rdi
je 0x33ec
movq -0xb8(%rbp), %rsi
subq %rdi, %rsi
callq 0x2170
movq $0x0, -0xd8(%rbp)
movl $0x0, -0xd0(%rbp)
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x3400
movq 0x10(%rbx), %rsi
subq %rdi, %rsi
callq 0x2170
leaq -0xf0(%rbp), %rdi
callq 0x76ae
movq %r14, %rdi
callq 0x2230