// File: 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
subq $0x230, %rsp            # imm = 0x230
movq %rdi, -0x1a8(%rbp)
movq %rdi, %rax
movq %rax, -0x1b0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %r8
movq -0x30(%rbp), %r9
leaq -0x48(%rbp), %rdi
callq 0x3040
movq -0x1a8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq %rax, %rcx
decq %rcx
imulq %rcx, %rax
shrq %rax
movq %rax, -0x50(%rbp)
movb $0x0, -0x51(%rbp)
callq 0x9cd0
movq -0x1a8(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x9cf0
jmp 0x3528
movq $0x0, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq -0x10(%rbp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0x3aa8
movq -0x10(%rbp), %rax
movq %rax, -0x1c0(%rbp)
movb $0x0, -0xa1(%rbp)
leaq -0xa2(%rbp), %rdi
movq %rdi, -0x1b8(%rbp)
callq 0xa5e0
movq -0x1c0(%rbp), %rsi
movq -0x1b8(%rbp), %rcx
leaq -0xa0(%rbp), %rdi
leaq -0xa1(%rbp), %rdx
callq 0xa600
jmp 0x3595
leaq -0xa2(%rbp), %rdi
callq 0xa680
movq -0x10(%rbp), %rax
movq %rax, -0x1d0(%rbp)
callq 0xa6a0
movsd %xmm0, -0xc8(%rbp)
leaq -0xc9(%rbp), %rdi
movq %rdi, -0x1c8(%rbp)
callq 0xa6b0
movq -0x1d0(%rbp), %rsi
movq -0x1c8(%rbp), %rcx
leaq -0xc0(%rbp), %rdi
leaq -0xc8(%rbp), %rdx
callq 0xa6d0
jmp 0x35ef
leaq -0xc9(%rbp), %rdi
callq 0xa750
leaq -0xf0(%rbp), %rdi
callq 0xa770
jmp 0x3609
movq -0x78(%rbp), %rsi
leaq -0xc0(%rbp), %rdi
callq 0xa7a0
movq $0x0, (%rax)
movq -0x78(%rbp), %rsi
leaq -0x100(%rbp), %rdi
xorps %xmm0, %xmm0
callq 0xa4c0
jmp 0x3635
leaq -0xf0(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0xa7c0
jmp 0x364a
movq -0x10(%rbp), %rax
subq -0x78(%rbp), %rax
subq $0x1, %rax
addq -0x70(%rbp), %rax
movq %rax, -0x70(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0xa810
movb %al, -0x1d1(%rbp)
jmp 0x3672
movb -0x1d1(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x1d2(%rbp)
jne 0x36a0
jmp 0x3687
movq -0x1a8(%rbp), %rdi
callq 0x9f50
cmpq -0x70(%rbp), %rax
setbe %al
movb %al, -0x1d2(%rbp)
movb -0x1d2(%rbp), %al
testb $0x1, %al
jne 0x36af
jmp 0x3a1b
leaq -0xf0(%rbp), %rdi
callq 0xa830
movq %rax, -0x1e0(%rbp)
jmp 0x36c4
movq -0x1e0(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x108(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0xa830
movq %rax, -0x1e8(%rbp)
jmp 0x36ea
movq -0x1e8(%rbp), %rax
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x110(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0xa850
jmp 0x370c
movq -0x108(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0xa8a0
movq %rdx, -0x1f8(%rbp)
movq %rax, -0x1f0(%rbp)
jmp 0x372f
movq -0x1f8(%rbp), %rax
movq -0x1f0(%rbp), %rcx
movq %rcx, -0x120(%rbp)
movq %rax, -0x118(%rbp)
leaq -0x120(%rbp), %rdi
callq 0xa900
xorb $-0x1, %al
testb $0x1, %al
jne 0x3762
jmp 0x3a16
movq -0x108(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0xa8a0
movq %rdx, -0x208(%rbp)
movq %rax, -0x200(%rbp)
jmp 0x3785
movq -0x208(%rbp), %rax
movq -0x200(%rbp), %rcx
movq %rcx, -0x130(%rbp)
movq %rax, -0x128(%rbp)
leaq -0x130(%rbp), %rdi
movl $0x1, %esi
callq 0xa930
movq -0x78(%rbp), %rax
cmpq -0x108(%rbp), %rax
jae 0x389c
movsd -0x110(%rbp), %xmm1
mulsd %xmm1, %xmm1
movsd 0x1a831(%rip), %xmm0    # 0x1e008
divsd %xmm1, %xmm0
movsd %xmm0, -0x138(%rbp)
movq -0x78(%rbp), %rsi
movq -0x108(%rbp), %rdx
movsd -0x110(%rbp), %xmm0
movsd -0x138(%rbp), %xmm1
leaq -0x158(%rbp), %rdi
callq 0xa0a0
jmp 0x380c
movq -0x1a8(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0xa070
jmp 0x3821
jmp 0x389c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x3ad7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0xa2(%rbp), %rdi
callq 0xa680
jmp 0x3ad7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0xc9(%rbp), %rdi
callq 0xa750
jmp 0x3a9a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x3a8e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0xaa60
jmp 0x3a8e
movq -0x108(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0xa470
movq %rax, -0x160(%rbp)
movq -0x160(%rbp), %rdi
callq 0xa980
movq %rax, -0x168(%rbp)
movq -0x160(%rbp), %rdi
callq 0xa9b0
movq %rax, -0x170(%rbp)
leaq -0x168(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0xa9e0
testb $0x1, %al
jne 0x38f5
jmp 0x3a14
leaq -0x168(%rbp), %rdi
callq 0xaa20
movq (%rax), %rcx
movq %rcx, -0x180(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x178(%rbp)
movq -0x180(%rbp), %rax
movq %rax, -0x188(%rbp)
movsd -0x178(%rbp), %xmm0
movsd %xmm0, -0x190(%rbp)
movsd -0x110(%rbp), %xmm0
addsd -0x190(%rbp), %xmm0
movsd %xmm0, -0x210(%rbp)
movq -0x188(%rbp), %rsi
leaq -0xc0(%rbp), %rdi
callq 0xa7a0
movsd -0x210(%rbp), %xmm1
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3a01
movsd -0x110(%rbp), %xmm0
movsd -0x190(%rbp), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0x228(%rbp)
movq -0x188(%rbp), %rsi
leaq -0xc0(%rbp), %rdi
movq %rdi, -0x220(%rbp)
callq 0xa7a0
movsd -0x228(%rbp), %xmm0
movq -0x220(%rbp), %rdi
movsd %xmm0, (%rax)
movq -0x188(%rbp), %rsi
movq %rsi, -0x218(%rbp)
callq 0xa7a0
movq -0x218(%rbp), %rsi
movsd (%rax), %xmm0
leaq -0x1a0(%rbp), %rdi
callq 0xa4c0
jmp 0x39ea
leaq -0xf0(%rbp), %rdi
leaq -0x1a0(%rbp), %rsi
callq 0xa7c0
jmp 0x39ff
jmp 0x3a01
jmp 0x3a03
leaq -0x168(%rbp), %rdi
callq 0xaa40
jmp 0x38d9
jmp 0x3a16
jmp 0x365e
movq -0x1a8(%rbp), %rdi
callq 0x9f50
cmpq -0x70(%rbp), %rax
je 0x3a59
movl $0x8, %edi
callq 0x2080
movq %rax, %rdi
leaq 0x1a650(%rip), %rax     # 0x1e091
movq %rax, (%rdi)
movq 0x2a575(%rip), %rsi     # 0x2dfc0
xorl %eax, %eax
movl %eax, %edx
callq 0x2290
jmp 0x3af5
leaq -0xf0(%rbp), %rdi
callq 0xaa60
leaq -0xc0(%rbp), %rdi
callq 0xaa80
leaq -0xa0(%rbp), %rdi
callq 0xaae0
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x3538
leaq -0xc0(%rbp), %rdi
callq 0xaa80
leaq -0xa0(%rbp), %rdi
callq 0xaae0
jmp 0x3ad7
movb $0x1, -0x51(%rbp)
testb $0x1, -0x51(%rbp)
jne 0x3abe
movq -0x1a8(%rbp), %rdi
callq 0xa190
leaq -0x48(%rbp), %rdi
callq 0xa520
movq -0x1b0(%rbp), %rax
addq $0x230, %rsp            # imm = 0x230
popq %rbp
retq
movq -0x1a8(%rbp), %rdi
callq 0xa190
leaq -0x48(%rbp), %rdi
callq 0xa520
movq -0x60(%rbp), %rdi
callq 0x22a0
nopw %cs:(%rax,%rax)