// File: /workspace/github_cpp_makefile_star2_O2/keyan[P]ev_routing_engine/router.cpp
// Function: Router::route(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
std::string Router::route(std::string source_name, std::string target_name) {
  // All labels in the vector are non-dominating in respect to total_weight and state_of_charge.
  // That is, all labels for a node are Pareto optimal.
  std::unordered_map<NodeID, std::vector<Label>> label_map;
  // Once a NodeID is added to the spt, we know the best Label to use to get to it.
  NodeToLabelMap shortest_path_tree;

  int label_id = 0;
  MinHeapPriorityQueue label_queue;
  // Used to keep track of removed Labels in the pq, we can't issue deletes because
  // std::priority_queue doesn't provide pointers to allow arbitrary deletes of items.
  std::unordered_set<int> deleted_labels;

  NodeID source_node_id = node_name_map_[source_name];
  NodeID target_node_id = node_name_map_[target_name];

  label_queue.emplace(Label(source_node_id, label_id++, 0, 0, MAX_CHARGE, source_node_id));
  while (!label_queue.empty()) {
    Label curr_label = label_queue.top();
    label_queue.pop();

    const NodeID &curr_node_id = curr_label.node_id;

    // std::priority_queue has no decrease-weight operation, instead do a "lazy
    // deletion" by keeping the old node in the pq and just ignoring it when it
    // is eventually popped.
    if (deleted_labels.count(curr_label.label_id) == 1 ||
        shortest_path_tree.count(curr_node_id) == 1) {
      continue;
    }
    shortest_path_tree.emplace(curr_node_id, curr_label);

    // Search is done.
    if (curr_node_id == target_node_id) {
      break;
    }

    const Station &curr_station = network_.at(curr_node_id);

    // Update weights for all neighbors not in the spt.
    // This is the main departure from standard dijkstra's. Instead of relaxing edges between
    // neighbors, we construct "labels" up to 3 per neighbor, and try to merge them into the
    // LabelMap. Any non-dominated labels are also added to the priority queue.
    for (auto &edge : graph_.at(curr_node_id)) {
      const NodeID &adj_node_id = edge.first;
      const Kilometers &dist_to_neighbor = edge.second;

      if (shortest_path_tree.count(adj_node_id) == 1) {
        continue;
      }

      Weight direct_weight_to_neighbor = convert_km_to_ms_travel(dist_to_neighbor);

      // Three possible label cases.
      std::vector<Label> labels;
      // 1. Go to neighbor without any charging, if possible.
      if (dist_to_neighbor <= curr_label.state_of_charge) {
        labels.emplace_back(Label(adj_node_id, label_id++,
                                  curr_label.total_weight + direct_weight_to_neighbor, 0,
                                  curr_label.state_of_charge - dist_to_neighbor, curr_node_id));
      }
      // 2. Do a full recharge, if needed.
      if (curr_label.state_of_charge < MAX_CHARGE) {
        Weight addtl_charge_time =
            time_to_full_charge(curr_label.state_of_charge, curr_station.rate);
        labels.emplace_back(
            Label(adj_node_id, label_id++,
                  curr_label.total_weight + direct_weight_to_neighbor + addtl_charge_time,
                  addtl_charge_time, MAX_CHARGE - dist_to_neighbor, curr_node_id));
      }
      // 3. Only charge enough to get to neighbor.
      if (curr_label.state_of_charge < MAX_CHARGE &&
          curr_label.state_of_charge < dist_to_neighbor) {
        Weight addtl_charge_time =
            time_to_partial_charge(curr_label.state_of_charge, dist_to_neighbor, curr_station.rate);
        labels.emplace_back(
            Label(adj_node_id, label_id++,
                  curr_label.total_weight + direct_weight_to_neighbor + addtl_charge_time,
                  addtl_charge_time, 0, curr_node_id));
      }

      // Update this nodes label bag. This is similar to "relaxing" edges in standard Dijkstra's.
      auto search = label_map.find(adj_node_id);
      if (search == label_map.end()) {
        // No labels exist to dominate these ones, so add them all.
        for (auto &label : labels) {
          label_queue.push(label);
        }
        label_map[adj_node_id] = std::move(labels);
      } else {
        std::vector<Label> &bag = search->second;

        for (auto &label : labels) {
          auto search = std::find_if(bag.begin(), bag.end(),
                                     [&](const Label &other) { return other.dominates(label); });
          // This label is dominated, it can be ignored.
          if (search != bag.end()) {
            continue;
          }

          // Does this label dominate anything in the bag already? If so, remove those labels.
          auto d_it = std::partition(bag.begin(), bag.end(),
                                     [&](const Label &other) { return !label.dominates(other); });
          for (auto it = d_it; it != bag.end(); ++it) {
            deleted_labels.insert((*it).label_id);
          }
          bag.erase(d_it, bag.end());
          bag.push_back(label);
          label_queue.push(label);
        }
      }
    }
  }

  return build_result_string(shortest_path_tree, source_node_id, target_node_id);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq %rcx, %rbx
movq %rdi, %r15
leaq 0x158(%rsp), %rax
movq %rax, -0x30(%rax)
pushq $0x1
popq %rcx
movq %rcx, -0x28(%rax)
xorpd %xmm0, %xmm0
movupd %xmm0, -0x20(%rax)
movl $0x3f800000, %edi       # imm = 0x3F800000
movl %edi, -0x10(%rax)
movupd %xmm0, -0x8(%rax)
leaq 0x120(%rsp), %rax
movq %rax, -0x30(%rax)
movq %rcx, -0x28(%rax)
movupd %xmm0, -0x20(%rax)
movl %edi, -0x10(%rax)
movupd %xmm0, -0x8(%rax)
andq $0x0, 0xb0(%rsp)
movapd %xmm0, 0xa0(%rsp)
leaq 0x190(%rsp), %rax
movq %rax, -0x30(%rax)
movq %rcx, -0x28(%rax)
movupd %xmm0, -0x20(%rax)
movl %edi, -0x10(%rax)
movupd %xmm0, -0x8(%rax)
movq %rsi, 0x58(%rsp)
leaq 0x8(%rsi), %r14
movq %r14, %rdi
movq %rdx, %rsi
callq 0x8418
movzwl (%rax), %ebp
movq %r14, %rdi
movq %rbx, %rsi
callq 0x8418
movq %r15, 0xc8(%rsp)
movzwl (%rax), %eax
movw %ax, 0xc(%rsp)
leaq 0x10(%rsp), %rsi
movw %bp, (%rsi)
xorpd %xmm0, %xmm0
andl $0x0, 0x14(%rsi)
movupd %xmm0, 0x4(%rsi)
movabsq $0x4074000000000000, %rax # imm = 0x4074000000000000
movq %rax, 0x18(%rsi)
movw %bp, 0xe(%rsp)
movw %bp, 0x20(%rsi)
leaq 0xa0(%rsp), %rdi
callq 0x970c
movq 0x58(%rsp), %rax
addq $0x40, %rax
movq %rax, 0xd0(%rsp)
pushq $0x1
popq %r13
leaq 0x160(%rsp), %rbp
leaq 0xf0(%rsp), %rbx
movq 0xa0(%rsp), %rax
cmpq 0xa8(%rsp), %rax
je 0x932a
movq 0x20(%rax), %rcx
movq %rcx, 0x80(%rsp)
movupd (%rax), %xmm0
movupd 0x10(%rax), %xmm1
movapd %xmm1, 0x70(%rsp)
movapd %xmm0, 0x60(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x9722
movq %rbp, %rdi
leaq 0x64(%rsp), %rsi
callq 0x9f64
cmpq $0x1, %rax
je 0x8d74
movq %rbx, %rdi
leaq 0x60(%rsp), %rsi
callq 0x9ff4
cmpq $0x1, %rax
je 0x8d74
movq %rbx, %rdi
leaq 0x60(%rsp), %rdx
movq %rdx, %rsi
callq 0xa088
movzwl 0x60(%rsp), %esi
cmpw 0xc(%rsp), %si
je 0x932a
movq 0x58(%rsp), %rax
movq (%rax), %rdi
callq 0x814a
movq %rax, 0x90(%rsp)
movzwl 0x60(%rsp), %esi
movq 0xd0(%rsp), %rdi
callq 0x8168
movq (%rax), %r14
movq 0x8(%rax), %rax
movq %rax, 0xd8(%rsp)
cmpq 0xd8(%rsp), %r14
je 0x8d74
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9ff4
cmpq $0x1, %rax
je 0x9321
movsd 0x8(%r14), %xmm0
divsd 0x41db(%rip), %xmm0     # 0xd040
mulsd 0x41db(%rip), %xmm0     # 0xd048
addsd 0x41db(%rip), %xmm0     # 0xd050
cvttsd2si %xmm0, %eax
movslq %eax, %rbx
xorpd %xmm0, %xmm0
movapd %xmm0, 0x40(%rsp)
andq $0x0, 0x50(%rsp)
movsd 0x8(%r14), %xmm1
movsd 0x78(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x8eeb
movzwl (%r14), %eax
movq 0x68(%rsp), %rcx
addq %rbx, %rcx
movw %ax, 0x10(%rsp)
movl %r13d, 0x14(%rsp)
movq %rcx, 0x18(%rsp)
andq $0x0, 0x20(%rsp)
subsd %xmm1, %xmm0
movzwl 0x60(%rsp), %eax
movsd %xmm0, 0x28(%rsp)
movw %ax, 0x30(%rsp)
leaq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x973a
incl %r13d
movsd 0x78(%rsp), %xmm0
movsd 0x3115(%rip), %xmm2     # 0xc008
ucomisd %xmm0, %xmm2
jbe 0x8f7d
movapd %xmm2, %xmm1
subsd %xmm0, %xmm1
movq 0x90(%rsp), %rax
divsd 0x30(%rax), %xmm1
mulsd 0x412e(%rip), %xmm1     # 0xd048
addsd 0x412e(%rip), %xmm1     # 0xd050
cvttsd2si %xmm1, %eax
cltq
movzwl (%r14), %ecx
movq 0x68(%rsp), %rdx
addq %rbx, %rdx
movapd %xmm2, %xmm0
subsd 0x8(%r14), %xmm0
addq %rax, %rdx
movzwl 0x60(%rsp), %esi
movw %cx, 0x10(%rsp)
movl %r13d, 0x14(%rsp)
movq %rdx, 0x18(%rsp)
movq %rax, 0x20(%rsp)
movsd %xmm0, 0x28(%rsp)
movw %si, 0x30(%rsp)
leaq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x973a
incl %r13d
movsd 0x78(%rsp), %xmm0
movsd 0x3083(%rip), %xmm1     # 0xc008
ucomisd %xmm0, %xmm1
jbe 0x9000
movsd 0x8(%r14), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x9000
subsd %xmm0, %xmm1
movq 0x90(%rsp), %rax
divsd 0x30(%rax), %xmm1
mulsd 0x4098(%rip), %xmm1     # 0xd048
addsd 0x4098(%rip), %xmm1     # 0xd050
cvttsd2si %xmm1, %eax
cltq
movzwl (%r14), %ecx
addq 0x68(%rsp), %rbx
addq %rax, %rbx
movzwl 0x60(%rsp), %edx
movw %cx, 0x10(%rsp)
movl %r13d, 0x14(%rsp)
movq %rbx, 0x18(%rsp)
movq %rax, 0x20(%rsp)
andq $0x0, 0x28(%rsp)
movw %dx, 0x30(%rsp)
leaq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x973a
incl %r13d
leaq 0x128(%rsp), %rdi
movq %r14, 0x98(%rsp)
movq %r14, %rsi
callq 0xa4fc
movq %rax, %r15
testq %rax, %rax
je 0x92bd
movq %r15, %rax
addq $0x10, %rax
movq %rax, 0xe0(%rsp)
movq 0x40(%rsp), %r14
movq 0x48(%rsp), %rax
movq %rax, 0xe8(%rsp)
cmpq 0xe8(%rsp), %r14
je 0x9307
movq 0x10(%r15), %r12
movq 0x18(%r15), %rsi
movq %rsi, %rcx
subq %r12, %rcx
movq %rcx, %rax
cqto
pushq $0x28
popq %rdi
idivq %rdi
sarq $0x2, %rax
pushq $0x8
popq %r8
xorl %edi, %edi
testq %rax, %rax
jle 0x90ee
movq 0x8(%r14), %rdx
movsd 0x18(%r14), %xmm0
cmpq %rdx, (%r12,%r8)
jae 0x909c
movsd 0x10(%r12,%r8), %xmm1
ucomisd %xmm0, %xmm1
ja 0x9124
cmpq %rdx, 0x28(%r12,%r8)
jae 0x90b0
movsd 0x38(%r12,%r8), %xmm1
ucomisd %xmm0, %xmm1
ja 0x912f
cmpq %rdx, 0x50(%r12,%r8)
jae 0x90c4
movsd 0x60(%r12,%r8), %xmm1
ucomisd %xmm0, %xmm1
ja 0x913b
cmpq %rdx, 0x78(%r12,%r8)
jae 0x90db
movsd 0x88(%r12,%r8), %xmm1
ucomisd %xmm0, %xmm1
ja 0x9147
decq %rax
addq $-0xa0, %rdi
addq $0xa0, %r8
jmp 0x9076
addq %rdi, %rcx
movq %rcx, %rax
cqto
pushq $0x28
popq %rcx
idivq %rcx
movq %r12, %rcx
subq %rdi, %rcx
cmpq $0x3, %rax
je 0x915f
cmpq $0x2, %rax
je 0x9153
cmpq $0x1, %rax
jne 0x91bf
movq 0x8(%r14), %rax
movsd 0x18(%r14), %xmm0
jmp 0x919b
movq %r12, %rcx
subq %rdi, %rcx
jmp 0x91b6
movq %r12, %rcx
subq %rdi, %rcx
addq $0x28, %rcx
jmp 0x91b6
movq %r12, %rcx
subq %rdi, %rcx
addq $0x50, %rcx
jmp 0x91b6
movq %r12, %rcx
subq %rdi, %rcx
addq $0x78, %rcx
jmp 0x91b6
movq 0x8(%r14), %rax
movsd 0x18(%r14), %xmm0
jmp 0x9186
movq 0x8(%r14), %rax
movsd 0x18(%r14), %xmm0
cmpq %rax, (%r12,%r8)
jae 0x917c
movsd 0x10(%r12,%r8), %xmm1
ucomisd %xmm0, %xmm1
ja 0x91b6
movq %r12, %rcx
subq %rdi, %rcx
addq $0x28, %rcx
cmpq %rax, 0x8(%rcx)
jae 0x9197
movsd 0x18(%rcx), %xmm1
ucomisd %xmm0, %xmm1
ja 0x91b6
addq $0x28, %rcx
movsd 0x18(%rcx), %xmm1
ucomisd %xmm0, %xmm1
movq %rsi, %rdx
cmovaq %rcx, %rdx
cmpq %rax, 0x8(%rcx)
cmovaeq %rsi, %rdx
movq %rdx, %rcx
cmpq %rsi, %rcx
jne 0x92b4
cmpq %rsi, %r12
je 0x925f
movq 0x8(%r14), %rax
cmpq 0x8(%r12), %rax
jae 0x91e2
movsd 0x18(%r14), %xmm0
ucomisd 0x18(%r12), %xmm0
ja 0x91e8
addq $0x28, %r12
jmp 0x91bf
movq %rsi, %rcx
leaq -0x28(%rcx), %rsi
cmpq %r12, %rsi
je 0x925f
cmpq -0x20(%rcx), %rax
jae 0x9204
ucomisd -0x10(%rcx), %xmm0
movq %rsi, %rcx
ja 0x91eb
movq 0x20(%r12), %rax
movq %rax, 0x30(%rsp)
movups (%r12), %xmm0
movups 0x10(%r12), %xmm1
movaps %xmm1, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movzwl 0x20(%rsi), %eax
movw %ax, 0x20(%r12)
movups (%rsi), %xmm0
movups 0x10(%rsi), %xmm1
movups %xmm1, 0x10(%r12)
movups %xmm0, (%r12)
movzwl 0x30(%rsp), %eax
movw %ax, 0x20(%rsi)
movapd 0x10(%rsp), %xmm0
movapd 0x20(%rsp), %xmm1
movupd %xmm1, 0x10(%rsi)
movupd %xmm0, (%rsi)
jmp 0x91e2
movl %r13d, %ebx
movq %r12, %r13
movq 0x18(%r15), %rdx
cmpq %rdx, %r13
je 0x9283
addq $0x4, %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0xa8de
addq $0x24, %r13
jmp 0x9265
movq 0xe0(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x977e
movq %r13, %rdi
movq %r14, %rsi
callq 0x9784
movl %ebx, %r13d
leaq 0xa0(%rsp), %rdi
movq %r14, %rsi
callq 0x9768
addq $0x28, %r14
jmp 0x9045
movq 0x40(%rsp), %rbx
movq 0x48(%rsp), %r14
movq 0x98(%rsp), %r15
cmpq %r14, %rbx
je 0x92ea
leaq 0xa0(%rsp), %rdi
movq %rbx, %rsi
callq 0x9768
addq $0x28, %rbx
jmp 0x92cf
leaq 0x128(%rsp), %rdi
movq %r15, %rsi
callq 0xa57c
movq %rax, %rdi
leaq 0x40(%rsp), %rsi
callq 0xa87a
leaq 0x40(%rsp), %rdi
callq 0x9d06
leaq 0xf0(%rsp), %rbx
movq 0x98(%rsp), %r14
addq $0x10, %r14
jmp 0x8e34
movzwl 0xe(%rsp), %ecx
movzwl 0xc(%rsp), %r8d
leaq 0xf0(%rsp), %rdx
movq 0xc8(%rsp), %rbx
movq %rbx, %rdi
movq 0x58(%rsp), %rsi
callq 0x9406
leaq 0x160(%rsp), %rdi
callq 0x9a0a
leaq 0xa0(%rsp), %rdi
callq 0x9d06
leaq 0xf0(%rsp), %rdi
callq 0x9a90
leaq 0x128(%rsp), %rdi
callq 0x9b16
movq %rbx, %rax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x93b1
jmp 0x93b1
jmp 0x93b1
jmp 0x93b1
jmp 0x93b1
jmp 0x93bc
jmp 0x93b1
jmp 0x93b1
jmp 0x93bc
jmp 0x93bc
jmp 0x93bc
movq %rax, %rbx
jmp 0x93c9
jmp 0x93bc
jmp 0x93bc
jmp 0x93bc
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x9d06
leaq 0x160(%rsp), %rdi
callq 0x9a0a
leaq 0xa0(%rsp), %rdi
callq 0x9d06
leaq 0xf0(%rsp), %rdi
callq 0x9a90
leaq 0x128(%rsp), %rdi
callq 0x9b16
movq %rbx, %rdi
callq 0x2260
nop