From 2f2a14feaf7bd942f841759853fc081b89a38bee Mon Sep 17 00:00:00 2001 From: Joao Gouveia Date: Fri, 30 Dec 2022 14:33:19 +0000 Subject: [PATCH] Even faster --- src/p2.cpp | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/p2.cpp b/src/p2.cpp index 9182804..661f769 100644 --- a/src/p2.cpp +++ b/src/p2.cpp @@ -137,20 +137,28 @@ void read_input (heap_t *heap, std::vector &nodes) { } int id1, id2; - size_t weight; + size_t weight, min; heap->edges.push_back(nullptr); for (int i = 0; i < e_count; i++) { std::cin >> id1 >> id2 >> weight; - edge_t* new_edge = new edge_t({.u = nodes[id1-1], .v = nodes[id2-1], .weight = weight}); - max_heap_insert(heap, new_edge); + + if (i == 0) + min = weight; + + if (i < v_count || weight > min) { + edge_t* new_edge = new edge_t({.u = nodes[id1-1], .v = nodes[id2-1], .weight = weight}); + max_heap_insert(heap, new_edge); + + if (weight < min) + min = weight; + } } - - heap->size = e_count; } int main(void) { heap_t* heap = new heap_t(); std::vector nodes; + heap->size = 0; read_input(heap, nodes); std::cout << get_maximum_cost_spanning_tree(heap) << std::endl;