// File: /workspace/github_cpp_makefile_star2_O2/ekg[P]sgd2/src/sgd2.hpp
// Function: sgd2::MSSP_weighted(std::vector<std::vector<sgd2::edge, std::allocator<sgd2::edge>>, std::allocator<std::vector<sgd2::edge, std::allocator<sgd2::edge>>>> const&, std::vector<unsigned long, std::allocator<unsigned long>> const&)
std::vector<term_sparse> MSSP_weighted(const std::vector<std::vector<edge>>& graph, const std::vector<uint64_t>& closest_pivots)
{
    uint64_t n = graph.size();

    // get pivots and their regions, but in sets
    std::unordered_map<uint64_t, std::unordered_set<uint64_t>> regions;
    for (uint64_t i = 0; i < n; i++)
    {
        if (regions.find(closest_pivots[i]) == regions.end())
        {
            regions[closest_pivots[i]] = std::unordered_set<uint64_t>();
        }
        regions[closest_pivots[i]].insert(i);
    }

    std::unordered_map<uint64_t, std::unordered_map<uint64_t, term_sparse>> termsDict;
    for (const auto& region : regions)
    {
        uint64_t p = region.first;

        std::vector<bool> visited(n, false);
        std::vector<double> d(n, std::numeric_limits<double>::max()); // init 'tentative' distances to infinity

        // edges are used 'invisibly' in this queue
        std::priority_queue<edge, std::vector<edge>, edge_comp> pq;

        // init initial edges so that pivot-pivot term is avoided
        for (edge e : graph[p])
        {
            // here the edge is not 'invisible'
            uint64_t next = e.target;
            double weight = e.weight;

            d[next] = weight; // init tentative value of d
            pq.push(edge(next, d[next]));
        }
        d[p] = 0;
        visited[p] = true;

        // q2 contains visited vertices' distances for s calculation
        std::queue<double> q2;
        uint64_t s = 1;

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

                // empty the second queue enough to calculate s
                while (!q2.empty() && q2.front() <= d_pi/2)
                {
                    q2.pop();
                    s += 1;
                }
                if (region.second.find(current) != region.second.end())
                {
                    q2.push(d_pi);
                }

                uint64_t i = current;
                if (i < p)
                {
                    if (termsDict.find(i) == termsDict.end())
                        termsDict[i] = std::unordered_map<uint64_t, term_sparse>();
                    if (termsDict[i].find(p) == termsDict[i].end())
                        termsDict[i].insert({ p, term_sparse(i, p, d_pi) });

                    termsDict[i].at(p).w_ij = s / ((double)d_pi * d_pi);
                }
                else
                {
                    if (termsDict.find(p) == termsDict.end())
                        termsDict[p] = std::unordered_map<uint64_t, term_sparse>();
                    if (termsDict[p].find(i) == termsDict[p].end())
                        termsDict[p].insert({ i, term_sparse(p, i, d_pi) });

                    termsDict[p].at(i).w_ji = s / ((double)d_pi * d_pi);
                }

                // update tentative distances
                for (edge e : graph[current])
                {
                    // here the edge is not 'invisible'
                    uint64_t next = e.target;
                    double weight = e.weight;

                    if (d_pi + weight < d[next]) 
                    {
                        d[next] = d_pi + weight; // update tentative value of d
                        pq.push(edge(next, d[next]));
                    }
                }
            }
        }
    }
    // 1-stress
    for (uint64_t i=0; i<n; i++)
    {
        for (edge e : graph[i])
        {
            uint64_t j = e.target;
            double d_ij = e.weight;
            if (i < j)
            {
                if (termsDict.find(i) == termsDict.end())
                    termsDict[i] = std::unordered_map<uint64_t, term_sparse>();
                if (termsDict[i].find(j) == termsDict[i].end())
                    termsDict[i].insert({ j, term_sparse(i, j, d_ij) });
                else
                    termsDict[i].at(j).d = d_ij;

                termsDict[i].at(j).w_ij = termsDict[i].at(j).w_ji = 1/(d_ij*d_ij);
            }
        }
    }
    std::vector<term_sparse> terms;
    for (const auto& i : termsDict)
    {
        for (const auto& j : i.second)
        {
            terms.push_back(j.second);
        }
    }
    return terms;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c8, %rsp            # imm = 0x1C8
movq %rdx, %rbx
movq %rdi, -0x110(%rbp)
movq 0x8(%rsi), %rax
movq %rsi, -0xa8(%rbp)
subq (%rsi), %rax
pushq $0x18
popq %rcx
cqto
idivq %rcx
movq %rax, -0x98(%rbp)
leaq -0x1b8(%rbp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
pxor %xmm0, %xmm0
movdqu %xmm0, -0x20(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movdqu %xmm0, -0x8(%rax)
xorl %esi, %esi
leaq -0x1e8(%rbp), %r14
leaq -0xf8(%rbp), %r15
leaq -0x190(%rbp), %r12
movq %rsi, -0x190(%rbp)
cmpq -0x98(%rbp), %rsi
jae 0x49ea
shlq $0x3, %rsi
addq (%rbx), %rsi
movq %r14, %rdi
callq 0x8fbe
testq %rax, %rax
jne 0x49ba
andq $0x0, -0xd8(%rbp)
leaq -0xc8(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq $0x1, -0xf0(%rbp)
pxor %xmm0, %xmm0
movdqu %xmm0, -0x20(%rax)
movl $0x3f800000, -0xd8(%rbp) # imm = 0x3F800000
movdqu %xmm0, -0x8(%rax)
movq -0x190(%rbp), %rsi
shlq $0x3, %rsi
addq (%rbx), %rsi
movq %r14, %rdi
callq 0x9048
movq %rax, %rdi
movq %r15, %rsi
callq 0x678e
movq %r15, %rdi
callq 0x68ba
movq -0x190(%rbp), %rsi
shlq $0x3, %rsi
addq (%rbx), %rsi
movq %r14, %rdi
callq 0x9048
movq %rax, %rdi
movq %r12, %rsi
callq 0x72ea
movq -0x190(%rbp), %rsi
incq %rsi
jmp 0x4930
leaq -0x160(%rbp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
leaq -0x180(%rbp), %rax
pxor %xmm0, %xmm0
movdqu %xmm0, (%rax)
movl $0x3f800000, 0x10(%rax) # imm = 0x3F800000
movq %rax, -0x100(%rbp)
movdqu %xmm0, 0x18(%rax)
movq -0x1d8(%rbp), %rax
leaq -0x158(%rbp), %r15
leaq -0x90(%rbp), %r12
leaq -0x1b0(%rbp), %r14
testq %rax, %rax
movq -0x98(%rbp), %rbx
je 0x4f40
movq %rax, -0x108(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
movb $0x0, -0xf8(%rbp)
movq %r15, %rdi
movq %rbx, %rsi
leaq -0xf8(%rbp), %r15
movq %r15, %rdx
movq %r12, %rcx
callq 0x60c4
movabsq $0x7fefffffffffffff, %rax # imm = 0x7FEFFFFFFFFFFFFF
movq %rax, -0xf8(%rbp)
leaq -0x130(%rbp), %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r12, %r13
movq %r12, %rcx
callq 0x6124
pxor %xmm0, %xmm0
movdqa %xmm0, -0x1b0(%rbp)
andq $0x0, -0x1a0(%rbp)
movq -0xa8(%rbp), %rax
movq (%rax), %rax
imulq $0x18, -0x30(%rbp), %rcx
movq (%rax,%rcx), %rbx
movq 0x8(%rax,%rcx), %r15
cmpq %r15, %rbx
je 0x4b0b
movq (%rbx), %rax
movq 0x8(%rbx), %xmm0
movq -0x130(%rbp), %rcx
movq %xmm0, (%rcx,%rax,8)
movq %rax, -0xf8(%rbp)
movq %xmm0, -0xf0(%rbp)
movq %r14, %rdi
leaq -0xf8(%rbp), %rsi
callq 0x6184
addq $0x10, %rbx
jmp 0x4ace
movq -0x30(%rbp), %rsi
movq -0x130(%rbp), %rax
andq $0x0, (%rax,%rsi,8)
leaq -0x158(%rbp), %r15
movq %r15, %rdi
callq 0x61c6
orq %rdx, (%rax)
leaq -0xf8(%rbp), %rdi
callq 0x655e
movq -0x108(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x118(%rbp)
pushq $0x1
popq %rbx
movq -0x1b0(%rbp), %rax
cmpq -0x1a8(%rbp), %rax
je 0x4f06
movq (%rax), %rcx
movq %rcx, -0x58(%rbp)
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x40(%rbp)
movq %r14, %rdi
callq 0x61a4
movq -0x58(%rbp), %rsi
movq %r15, %rdi
callq 0x61c6
testq %rdx, (%rax)
jne 0x4b4e
movq -0x58(%rbp), %rsi
movq %r15, %rdi
callq 0x61c6
orq %rdx, (%rax)
leaq -0xf8(%rbp), %r15
movq -0xe8(%rbp), %rax
cmpq %rax, -0xc8(%rbp)
je 0x4bd2
movsd -0x40(%rbp), %xmm0
mulsd 0x6451(%rip), %xmm0     # 0xb010
ucomisd (%rax), %xmm0
jb 0x4bd2
movq %r15, %rdi
callq 0x9ff4
incq %rbx
jmp 0x4ba2
movq -0x118(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x9466
testq %rax, %rax
je 0x4bf7
leaq -0xf8(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0xa05c
movq -0x58(%rbp), %rax
movq %rax, -0x38(%rbp)
movq %rbx, %xmm0
movdqa %xmm0, -0x50(%rbp)
cmpq -0x30(%rbp), %rax
jae 0x4d57
leaq -0x190(%rbp), %r15
movq %r15, %rdi
leaq -0x38(%rbp), %rsi
callq 0x9488
testq %rax, %rax
jne 0x4c7a
andq $0x0, -0x70(%rbp)
leaq -0x60(%rbp), %rax
movq %rax, -0x90(%rbp)
movq $0x1, -0x88(%rbp)
pxor %xmm0, %xmm0
movdqu %xmm0, -0x20(%rax)
movl $0x3f800000, -0x70(%rbp) # imm = 0x3F800000
movdqu %xmm0, -0x8(%rax)
movq %r15, %rdi
leaq -0x38(%rbp), %rsi
callq 0x9512
movq %rax, %rdi
movq %r13, %rsi
callq 0x691e
movq %r13, %rdi
callq 0x6a4a
movq %r15, %rdi
leaq -0x38(%rbp), %rsi
callq 0x9512
movq %rax, %rdi
leaq -0x30(%rbp), %rsi
callq 0x9862
movq %rax, %r12
movq %r15, %rdi
leaq -0x38(%rbp), %rsi
callq 0x9512
testq %r12, %r12
jne 0x4cee
movq %r15, %rdi
leaq -0x38(%rbp), %rsi
callq 0x9512
movq -0x38(%rbp), %rcx
movq -0x30(%rbp), %rdx
movq -0x40(%rbp), %xmm0
xorpd %xmm1, %xmm1
leaq -0x60(%rbp), %rsi
movupd %xmm1, -0x10(%rsi)
movq %rdx, -0x90(%rbp)
movq %rcx, -0x88(%rbp)
movq %rdx, -0x80(%rbp)
movq %xmm0, -0x78(%rbp)
movq %rax, %rdi
movq %r13, %rsi
callq 0x98ec
movapd -0x50(%rbp), %xmm0
unpcklps 0x6366(%rip), %xmm0     # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x636e(%rip), %xmm0     # 0xb070
movapd %xmm0, -0x50(%rbp)
movq -0x40(%rbp), %xmm0
movq %xmm0, -0xa0(%rbp)
movq %r15, %rdi
leaq -0x38(%rbp), %rsi
callq 0x9512
movq %rax, %rdi
leaq -0x30(%rbp), %rsi
callq 0x9c1c
movapd -0x50(%rbp), %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movsd -0xa0(%rbp), %xmm1
mulsd %xmm1, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rax)
jmp 0x4e96
leaq -0x190(%rbp), %r15
movq %r15, %rdi
leaq -0x30(%rbp), %rsi
callq 0x9488
testq %rax, %rax
jne 0x4dbe
andq $0x0, -0x70(%rbp)
leaq -0x60(%rbp), %rax
movq %rax, -0x90(%rbp)
movq $0x1, -0x88(%rbp)
pxor %xmm0, %xmm0
movdqu %xmm0, -0x20(%rax)
movl $0x3f800000, -0x70(%rbp) # imm = 0x3F800000
movdqu %xmm0, -0x8(%rax)
movq %r15, %rdi
leaq -0x30(%rbp), %rsi
callq 0x9512
movq %rax, %rdi
movq %r13, %rsi
callq 0x691e
movq %r13, %rdi
callq 0x6a4a
movq %r15, %rdi
leaq -0x30(%rbp), %rsi
callq 0x9512
movq %rax, %rdi
leaq -0x38(%rbp), %rsi
callq 0x9862
movq %rax, %r12
movq %r15, %rdi
leaq -0x30(%rbp), %rsi
callq 0x9512
testq %r12, %r12
jne 0x4e32
movq %r15, %rdi
leaq -0x30(%rbp), %rsi
callq 0x9512
movq -0x30(%rbp), %rcx
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %xmm0
xorpd %xmm1, %xmm1
leaq -0x60(%rbp), %rsi
movupd %xmm1, -0x10(%rsi)
movq %rdx, -0x90(%rbp)
movq %rcx, -0x88(%rbp)
movq %rdx, -0x80(%rbp)
movq %xmm0, -0x78(%rbp)
movq %rax, %rdi
movq %r13, %rsi
callq 0x98ec
movapd -0x50(%rbp), %xmm0
unpcklps 0x6222(%rip), %xmm0     # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x622a(%rip), %xmm0     # 0xb070
movapd %xmm0, -0x50(%rbp)
movq -0x40(%rbp), %xmm0
movq %xmm0, -0xa0(%rbp)
movq %r15, %rdi
leaq -0x30(%rbp), %rsi
callq 0x9512
movq %rax, %rdi
leaq -0x38(%rbp), %rsi
callq 0x9c1c
movapd -0x50(%rbp), %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movsd -0xa0(%rbp), %xmm1
mulsd %xmm1, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x20(%rax)
movq -0xa8(%rbp), %rax
movq (%rax), %rax
imulq $0x18, -0x58(%rbp), %rcx
movq (%rax,%rcx), %r12
movq 0x8(%rax,%rcx), %r15
cmpq %r15, %r12
je 0x4efa
movq (%r12), %rax
movsd 0x8(%r12), %xmm0
addsd -0x40(%rbp), %xmm0
movq -0x130(%rbp), %rcx
movsd (%rcx,%rax,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x4ef4
movsd %xmm0, (%rcx,%rax,8)
movq %rax, -0x90(%rbp)
movsd %xmm0, -0x88(%rbp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x6184
addq $0x10, %r12
jmp 0x4eae
leaq -0x158(%rbp), %r15
jmp 0x4b4e
leaq -0xf8(%rbp), %rdi
callq 0x6c6a
movq %r14, %rdi
callq 0x6750
leaq -0x130(%rbp), %rdi
callq 0x89de
movq %r15, %rdi
callq 0x61e8
movq -0x108(%rbp), %rax
movq (%rax), %rax
movq %r13, %r12
jmp 0x4a3b
xorl %eax, %eax
leaq -0x190(%rbp), %rbx
leaq -0x90(%rbp), %r14
movq %rax, -0x90(%rbp)
cmpq -0x98(%rbp), %rax
jae 0x5135
movq -0xa8(%rbp), %rcx
movq (%rcx), %rcx
imulq $0x18, %rax, %rax
movq (%rcx,%rax), %r15
movq 0x8(%rcx,%rax), %r12
cmpq %r12, %r15
je 0x5126
movq (%r15), %rax
movsd 0x8(%r15), %xmm0
movsd %xmm0, -0x50(%rbp)
movq %rax, -0x158(%rbp)
cmpq %rax, -0x90(%rbp)
jae 0x511d
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9488
testq %rax, %rax
jne 0x5014
andq $0x0, -0xd8(%rbp)
leaq -0xc8(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq $0x1, -0xf0(%rbp)
xorpd %xmm0, %xmm0
movupd %xmm0, -0x20(%rax)
movl $0x3f800000, -0xd8(%rbp) # imm = 0x3F800000
movupd %xmm0, -0x8(%rax)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9512
movq %rax, %rdi
leaq -0xf8(%rbp), %r13
movq %r13, %rsi
callq 0x691e
movq %r13, %rdi
callq 0x6a4a
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9512
movq %rax, %rdi
leaq -0x158(%rbp), %rsi
callq 0x9862
movq %rax, %r13
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9512
testq %r13, %r13
je 0x5067
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9512
movq %rax, %rdi
leaq -0x158(%rbp), %rsi
callq 0x9c1c
movsd -0x50(%rbp), %xmm0
movsd %xmm0, 0x10(%rax)
jmp 0x50c0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9512
movq -0x90(%rbp), %rcx
movq -0x158(%rbp), %rdx
leaq -0xd8(%rbp), %rsi
xorpd %xmm0, %xmm0
movupd %xmm0, (%rsi)
movq %rdx, -0xf8(%rbp)
movq %rcx, -0xf0(%rbp)
movq %rdx, -0xe8(%rbp)
movsd -0x50(%rbp), %xmm0
movsd %xmm0, -0xe0(%rbp)
movq %rax, %rdi
leaq -0xf8(%rbp), %rsi
callq 0x98ec
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9512
movq %rax, %rdi
leaq -0x158(%rbp), %rsi
callq 0x9c1c
movsd -0x50(%rbp), %xmm1
mulsd %xmm1, %xmm1
movsd 0x5f1d(%rip), %xmm0     # 0xb008
divsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
movsd %xmm0, 0x20(%rax)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9512
movq %rax, %rdi
leaq -0x158(%rbp), %rsi
callq 0x9c1c
movsd -0x50(%rbp), %xmm0
movsd %xmm0, 0x18(%rax)
addq $0x10, %r15
jmp 0x4f7b
movq -0x90(%rbp), %rax
incq %rax
jmp 0x4f50
pxor %xmm0, %xmm0
movq -0x110(%rbp), %r14
movdqu %xmm0, (%r14)
andq $0x0, 0x10(%r14)
movq -0x100(%rbp), %rax
movq (%rax), %rax
testq %rax, %rax
je 0x517a
movq %rax, -0x100(%rbp)
leaq 0x20(%rax), %rbx
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x514a
leaq 0x10(%rbx), %rsi
movq %r14, %rdi
callq 0x652a
jmp 0x5164
leaq -0x190(%rbp), %rdi
callq 0x6aae
leaq -0x1e8(%rbp), %rdi
callq 0x6b8c
movq %r14, %rax
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0xf8(%rbp), %rdi
callq 0x68ba
jmp 0x526f
jmp 0x5224
jmp 0x5224
movq %rax, %rbx
jmp 0x5257
jmp 0x522b
jmp 0x51cb
movq %rax, %rbx
jmp 0x526f
jmp 0x5230
jmp 0x51d9
jmp 0x5230
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0x6a4a
jmp 0x5233
movq %rax, %rbx
leaq -0xf8(%rbp), %rdi
callq 0x6a4a
jmp 0x5263
jmp 0x522b
jmp 0x5230
jmp 0x5230
jmp 0x5230
jmp 0x5230
jmp 0x5230
jmp 0x5230
jmp 0x5230
jmp 0x5230
jmp 0x522b
jmp 0x522b
jmp 0x5230
movq %rax, %rbx
movq %r14, %rdi
callq 0x9c3c
jmp 0x5263
jmp 0x522b
jmp 0x5230
movq %rax, %rbx
jmp 0x523f
jmp 0x5230
movq %rax, %rbx
jmp 0x5263
movq %rax, %rbx
leaq -0xf8(%rbp), %rdi
callq 0x6c6a
leaq -0x1b0(%rbp), %rdi
callq 0x6750
leaq -0x130(%rbp), %rdi
callq 0x89de
leaq -0x158(%rbp), %rdi
callq 0x61e8
leaq -0x190(%rbp), %rdi
callq 0x6aae
leaq -0x1e8(%rbp), %rdi
callq 0x6b8c
movq %rbx, %rdi
callq 0x2210