// File: 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:
subq $0x408, %rsp            # imm = 0x408
movq %rcx, 0xb0(%rsp)
movq %rdx, 0xb8(%rsp)
movq %rdi, 0xc0(%rsp)
movq %rdi, %rax
movq %rax, 0xc8(%rsp)
movq %rdi, 0x400(%rsp)
movq %rsi, 0x3f8(%rsp)
movq %rdx, 0x3f0(%rsp)
movq %rcx, 0x3e8(%rsp)
movq 0x3f8(%rsp), %rax
movq %rax, 0xd0(%rsp)
leaq 0x3b0(%rsp), %rdi
callq 0x15420
leaq 0x378(%rsp), %rdi
callq 0x15430
movl $0x0, 0x374(%rsp)
leaq 0x350(%rsp), %rdi
callq 0x15440
jmp 0x13e6e
leaq 0x308(%rsp), %rdi
callq 0x15470
movq 0xd0(%rsp), %rdi
movq 0xb8(%rsp), %rsi
addq $0x8, %rdi
callq 0x10170
movq %rax, 0xa8(%rsp)
jmp 0x13e9e
movq 0xb0(%rsp), %rsi
movq 0xd0(%rsp), %rdi
movq 0xa8(%rsp), %rax
movw (%rax), %ax
movw %ax, 0x306(%rsp)
addq $0x8, %rdi
callq 0x10170
movq %rax, 0xa0(%rsp)
jmp 0x13ed4
movq 0xa0(%rsp), %rax
movw (%rax), %ax
movw %ax, 0x304(%rsp)
movzwl 0x306(%rsp), %esi
movl 0x374(%rsp), %edx
movl %edx, %eax
incl %eax
movl %eax, 0x374(%rsp)
movzwl 0x306(%rsp), %r9d
xorl %eax, %eax
movl %eax, %r8d
leaq 0x2d8(%rsp), %rdi
movsd 0xa0e9(%rip), %xmm0     # 0x1e008
movq %r8, %rcx
callq 0x154e0
jmp 0x13f29
leaq 0x350(%rsp), %rdi
leaq 0x2d8(%rsp), %rsi
callq 0x15480
jmp 0x13f40
jmp 0x13f42
leaq 0x350(%rsp), %rdi
callq 0x15550
movb %al, 0x9f(%rsp)
jmp 0x13f58
movb 0x9f(%rsp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x13f6a
jmp 0x14899
leaq 0x350(%rsp), %rdi
callq 0x15570
movq %rax, 0x90(%rsp)
jmp 0x13f81
movq 0x90(%rsp), %rax
movq 0x20(%rax), %rcx
movq %rcx, 0x2d0(%rsp)
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movaps %xmm1, 0x2c0(%rsp)
movaps %xmm0, 0x2b0(%rsp)
leaq 0x350(%rsp), %rdi
callq 0x15580
jmp 0x13fbb
leaq 0x2b0(%rsp), %rax
movq %rax, 0x2a8(%rsp)
leaq 0x2b4(%rsp), %rsi
leaq 0x308(%rsp), %rdi
callq 0x155d0
movq %rax, 0x88(%rsp)
jmp 0x13fea
movq 0x88(%rsp), %rax
cmpq $0x1, %rax
je 0x14025
movq 0x2a8(%rsp), %rsi
leaq 0x378(%rsp), %rdi
callq 0x15600
movq %rax, 0x80(%rsp)
jmp 0x14017
movq 0x80(%rsp), %rax
cmpq $0x1, %rax
jne 0x1405c
jmp 0x13f42
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x348(%rsp)
movl %eax, 0x344(%rsp)
jmp 0x14927
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x348(%rsp)
movl %eax, 0x344(%rsp)
jmp 0x1490d
movq 0x2a8(%rsp), %rsi
leaq 0x378(%rsp), %rdi
leaq 0x2b0(%rsp), %rdx
callq 0x15630
movb %dl, 0x77(%rsp)
movq %rax, 0x78(%rsp)
jmp 0x14084
movb 0x77(%rsp), %al
movq 0x78(%rsp), %rcx
movq %rcx, 0x298(%rsp)
movb %al, 0x2a0(%rsp)
movq 0x2a8(%rsp), %rax
movzwl (%rax), %eax
movzwl 0x304(%rsp), %ecx
cmpl %ecx, %eax
jne 0x140b8
jmp 0x14899
movq 0xd0(%rsp), %rax
movq (%rax), %rdi
movq 0x2a8(%rsp), %rax
movzwl (%rax), %eax
movl %eax, %esi
callq 0x101a0
movq %rax, 0x68(%rsp)
jmp 0x140dc
movq 0xd0(%rsp), %rdi
movq 0x68(%rsp), %rax
movq %rax, 0x290(%rsp)
addq $0x40, %rdi
movq 0x2a8(%rsp), %rax
movzwl (%rax), %eax
movl %eax, %esi
callq 0x101e0
movq %rax, 0x60(%rsp)
jmp 0x1410e
movq 0x60(%rsp), %rax
movq %rax, 0x288(%rsp)
movq 0x288(%rsp), %rdi
callq 0x13020
movq %rax, 0x280(%rsp)
movq 0x288(%rsp), %rdi
callq 0x12e90
movq %rax, 0x278(%rsp)
leaq 0x280(%rsp), %rdi
leaq 0x278(%rsp), %rsi
callq 0x15680
testb $0x1, %al
jne 0x14163
jmp 0x14894
leaq 0x280(%rsp), %rdi
callq 0x156c0
movq %rax, 0x270(%rsp)
movq 0x270(%rsp), %rax
movq %rax, 0x268(%rsp)
movq 0x270(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x260(%rsp)
movq 0x268(%rsp), %rsi
leaq 0x378(%rsp), %rdi
callq 0x15600
movq %rax, 0x58(%rsp)
jmp 0x141b8
movq 0x58(%rsp), %rax
cmpq $0x1, %rax
jne 0x141c8
jmp 0x14882
movq 0x260(%rsp), %rax
movsd (%rax), %xmm0
callq 0x156d0
movq %rax, 0x50(%rsp)
jmp 0x141e0
movq 0x50(%rsp), %rax
movq %rax, 0x258(%rsp)
leaq 0x240(%rsp), %rdi
callq 0x15710
movq 0x260(%rsp), %rax
movsd (%rax), %xmm1
movsd 0x2c8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x142c1
movq 0x268(%rsp), %rax
movzwl (%rax), %esi
movl 0x374(%rsp), %edx
movl %edx, %eax
incl %eax
movl %eax, 0x374(%rsp)
movq 0x2b8(%rsp), %rcx
movq 0x258(%rsp), %rax
addq %rax, %rcx
movsd 0x2c8(%rsp), %xmm0
movq 0x260(%rsp), %rax
movsd (%rax), %xmm1
subsd %xmm1, %xmm0
movq 0x2a8(%rsp), %rax
movzwl (%rax), %r9d
xorl %eax, %eax
movl %eax, %r8d
leaq 0x218(%rsp), %rdi
callq 0x154e0
jmp 0x14282
leaq 0x240(%rsp), %rdi
leaq 0x218(%rsp), %rsi
callq 0x15720
jmp 0x14299
jmp 0x142c1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x348(%rsp)
movl %eax, 0x344(%rsp)
leaq 0x240(%rsp), %rdi
callq 0x15bf0
jmp 0x1490d
movsd 0x9d3f(%rip), %xmm0     # 0x1e008
ucomisd 0x2c8(%rsp), %xmm0
jbe 0x1438e
movsd 0x2c8(%rsp), %xmm0
movq 0x290(%rsp), %rax
movsd 0x30(%rax), %xmm1
callq 0x15790
movq %rax, 0x48(%rsp)
jmp 0x142fa
movq 0x48(%rsp), %rax
movq %rax, 0x210(%rsp)
movq 0x268(%rsp), %rax
movzwl (%rax), %esi
movl 0x374(%rsp), %edx
movl %edx, %eax
incl %eax
movl %eax, 0x374(%rsp)
movq 0x2b8(%rsp), %rcx
movq 0x258(%rsp), %rax
addq %rax, %rcx
movq 0x210(%rsp), %r8
addq %r8, %rcx
movq 0x260(%rsp), %rax
movsd (%rax), %xmm1
movsd 0x9cb2(%rip), %xmm0     # 0x1e008
subsd %xmm1, %xmm0
movq 0x2a8(%rsp), %rax
movzwl (%rax), %r9d
leaq 0x1e8(%rsp), %rdi
callq 0x154e0
jmp 0x14375
leaq 0x240(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
callq 0x15720
jmp 0x1438c
jmp 0x1438e
movsd 0x9c72(%rip), %xmm0     # 0x1e008
ucomisd 0x2c8(%rsp), %xmm0
jbe 0x14471
movsd 0x2c8(%rsp), %xmm1
movq 0x260(%rsp), %rax
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x14471
movsd 0x2c8(%rsp), %xmm0
movq 0x260(%rsp), %rax
movsd (%rax), %xmm1
movq 0x290(%rsp), %rax
movsd 0x30(%rax), %xmm2
callq 0x157c0
movq %rax, 0x40(%rsp)
jmp 0x143f2
movq 0x40(%rsp), %rax
movq %rax, 0x1e0(%rsp)
movq 0x268(%rsp), %rax
movzwl (%rax), %esi
movl 0x374(%rsp), %edx
movl %edx, %eax
incl %eax
movl %eax, 0x374(%rsp)
movq 0x2b8(%rsp), %rcx
movq 0x258(%rsp), %rax
addq %rax, %rcx
movq 0x1e0(%rsp), %r8
addq %r8, %rcx
movq 0x2a8(%rsp), %rax
movzwl (%rax), %r9d
leaq 0x1b8(%rsp), %rdi
xorps %xmm0, %xmm0
callq 0x154e0
jmp 0x14458
leaq 0x240(%rsp), %rdi
leaq 0x1b8(%rsp), %rsi
callq 0x15720
jmp 0x1446f
jmp 0x14471
movq 0x268(%rsp), %rsi
leaq 0x3b0(%rsp), %rdi
callq 0x15810
movq %rax, 0x38(%rsp)
jmp 0x1448d
movq 0x38(%rsp), %rax
movq %rax, 0x1b0(%rsp)
leaq 0x3b0(%rsp), %rdi
callq 0x15860
movq %rax, 0x1a8(%rsp)
leaq 0x1b0(%rsp), %rdi
leaq 0x1a8(%rsp), %rsi
callq 0x15840
testb $0x1, %al
jne 0x144cd
jmp 0x14592
leaq 0x240(%rsp), %rax
movq %rax, 0x1a0(%rsp)
movq 0x1a0(%rsp), %rdi
callq 0x15890
movq %rax, 0x198(%rsp)
movq 0x1a0(%rsp), %rdi
callq 0x158c0
movq %rax, 0x190(%rsp)
leaq 0x198(%rsp), %rdi
leaq 0x190(%rsp), %rsi
callq 0x158f0
testb $0x1, %al
jne 0x14522
jmp 0x1455f
leaq 0x198(%rsp), %rdi
callq 0x15930
movq %rax, 0x188(%rsp)
movq 0x188(%rsp), %rsi
leaq 0x350(%rsp), %rdi
callq 0x15940
jmp 0x1454e
jmp 0x14550
leaq 0x198(%rsp), %rdi
callq 0x159a0
jmp 0x14507
movq 0x268(%rsp), %rsi
leaq 0x3b0(%rsp), %rdi
callq 0x159c0
movq %rax, 0x30(%rsp)
jmp 0x1457b
movq 0x30(%rsp), %rdi
leaq 0x240(%rsp), %rsi
callq 0x159f0
jmp 0x14875
leaq 0x1b0(%rsp), %rdi
callq 0x15a30
addq $0x8, %rax
movq %rax, 0x180(%rsp)
leaq 0x240(%rsp), %rax
movq %rax, 0x178(%rsp)
movq 0x178(%rsp), %rdi
callq 0x15890
movq %rax, 0x170(%rsp)
movq 0x178(%rsp), %rdi
callq 0x158c0
movq %rax, 0x168(%rsp)
leaq 0x170(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0x158f0
testb $0x1, %al
jne 0x14603
jmp 0x14873
leaq 0x170(%rsp), %rdi
callq 0x15930
movq %rax, 0x160(%rsp)
movq 0x180(%rsp), %rdi
callq 0x15890
movq %rax, 0x150(%rsp)
movq 0x180(%rsp), %rdi
callq 0x158c0
movq %rax, 0x148(%rsp)
movq 0x160(%rsp), %rax
movq %rax, 0x140(%rsp)
movq 0x150(%rsp), %rdi
movq 0x148(%rsp), %rsi
movq 0x140(%rsp), %rdx
callq 0x14950
movq %rax, 0x28(%rsp)
jmp 0x14676
movq 0x28(%rsp), %rax
movq %rax, 0x158(%rsp)
movq 0x180(%rsp), %rdi
callq 0x158c0
movq %rax, 0x138(%rsp)
leaq 0x158(%rsp), %rdi
leaq 0x138(%rsp), %rsi
callq 0x158f0
testb $0x1, %al
jne 0x146b3
jmp 0x146b8
jmp 0x14861
movq 0x180(%rsp), %rdi
callq 0x15890
movq %rax, 0x128(%rsp)
movq 0x180(%rsp), %rdi
callq 0x158c0
movq %rax, 0x120(%rsp)
movq 0x160(%rsp), %rax
movq %rax, 0x118(%rsp)
movq 0x128(%rsp), %rdi
movq 0x120(%rsp), %rsi
movq 0x118(%rsp), %rdx
callq 0x149c0
movq %rax, 0x20(%rsp)
jmp 0x14716
movq 0x20(%rsp), %rax
movq %rax, 0x130(%rsp)
movq 0x130(%rsp), %rax
movq %rax, 0x110(%rsp)
movq 0x180(%rsp), %rdi
callq 0x158c0
movq %rax, 0x108(%rsp)
leaq 0x110(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0x158f0
testb $0x1, %al
jne 0x14763
jmp 0x147b9
leaq 0x110(%rsp), %rdi
callq 0x15930
movq %rax, %rsi
addq $0x4, %rsi
leaq 0x308(%rsp), %rdi
callq 0x15a50
movb %dl, 0x17(%rsp)
movq %rax, 0x18(%rsp)
jmp 0x1478f
movb 0x17(%rsp), %al
movq 0x18(%rsp), %rcx
movq %rcx, 0xf8(%rsp)
movb %al, 0x100(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x159a0
jmp 0x14733
movq 0x180(%rsp), %rax
movq %rax, (%rsp)
leaq 0xf0(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0x15b40
movq 0x180(%rsp), %rdi
callq 0x158c0
movq %rax, 0xe0(%rsp)
leaq 0xe8(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x15b40
movq (%rsp), %rdi
movq 0xf0(%rsp), %rsi
movq 0xe8(%rsp), %rdx
callq 0x15aa0
movq %rax, 0x8(%rsp)
jmp 0x14824
movq 0x8(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq 0x180(%rsp), %rdi
movq 0x160(%rsp), %rsi
callq 0x15b80
jmp 0x14848
movq 0x160(%rsp), %rsi
leaq 0x350(%rsp), %rdi
callq 0x15940
jmp 0x1485f
jmp 0x14861
leaq 0x170(%rsp), %rdi
callq 0x159a0
jmp 0x145e5
jmp 0x14875
leaq 0x240(%rsp), %rdi
callq 0x15bf0
leaq 0x280(%rsp), %rdi
callq 0x15c50
jmp 0x14145
jmp 0x13f42
movq 0xd0(%rsp), %rsi
movq 0xc0(%rsp), %rdi
movzwl 0x306(%rsp), %ecx
movzwl 0x304(%rsp), %r8d
leaq 0x378(%rsp), %rdx
callq 0x14a20
jmp 0x148c9
leaq 0x308(%rsp), %rdi
callq 0x15c70
leaq 0x350(%rsp), %rdi
callq 0x15c80
leaq 0x378(%rsp), %rdi
callq 0x15c90
leaq 0x3b0(%rsp), %rdi
callq 0x15ca0
movq 0xc8(%rsp), %rax
addq $0x408, %rsp            # imm = 0x408
retq
leaq 0x308(%rsp), %rdi
callq 0x15c70
leaq 0x350(%rsp), %rdi
callq 0x15c80
leaq 0x378(%rsp), %rdi
callq 0x15c90
leaq 0x3b0(%rsp), %rdi
callq 0x15ca0
movq 0x348(%rsp), %rdi
callq 0x2370
nop