void Timer :: RemoveTimer(const size_t timer_id) { if (timer_id == 0) { return; } size_t now_idx = timer_id - 1; if (now_idx >= timer_heap_.size()) { return; } TimerObj obj = timer_heap_[now_idx]; UThreadSocketSetTimerID(*obj.socket_, 0); std::swap(timer_heap_[timer_heap_.size() - 1], timer_heap_[now_idx]); timer_heap_.pop_back(); if (timer_heap_.empty()) { return; } if (timer_heap_[now_idx] < obj) { heap_up(now_idx + 1); } else if (timer_heap_[now_idx] == obj) { UThreadSocketSetTimerID(*timer_heap_[now_idx].socket_, now_idx + 1); } else { heap_down(now_idx); } }
void heap_down(int i) { int a = 2*i+1; int b = 2*i+2; if(b < heapsize) { if(heap_val[b] < heap_val[a] && heap_val[b] < heap_val[i]) { heap_swap(i, b); heap_down(b); return; } } if(a < heapsize && heap_val[a] < heap_val[i]) { heap_swap(i, a); heap_down(a); } }
long heap_pop() { long ret = heap[0]; heap[0] = heap[tot - 1]; pos[heap[0]] = 0; tot--; heap_down(0); return ret; }
void matcher::heap::pop(){ if (len <= 0) return; swap(nodes[1], nodes[len]); swap(heap_p(1), heap_p(len)); len--; heap_down(1); }
int poll_heap () { int k = key[0]; n--; if (n > 0) { key[0] = key[n]; back[key[0]] = 0; heap_down(0); } back[k] = -1; return k; }
void push_heap (int cur) { if (back[cur] != -1) { heap_down(back[cur]); heap_up(back[cur]); } else { key[n] = cur; back[cur] = n; n++; heap_up(n-1); } }
/* * camq_change_priority: Given an array of cam_pinfo* elements with the * Heap(1, num_entries) property, an index such that 1 <= index <= num_elements, * and a new priority for the element at index, change the priority of * element index and restore the Heap(0, num_elements) property. */ void camq_change_priority(struct camq *queue, int index, u_int32_t new_priority) { if (new_priority > queue->queue_array[index]->priority) { queue->queue_array[index]->priority = new_priority; heap_down(queue->queue_array, index, queue->entries); } else { /* new_priority <= old_priority */ queue->queue_array[index]->priority = new_priority; heap_up(queue->queue_array, index); } }
matcher::heap::heap(int n, int m, class matcher *o) { owner = o; len = 0; memset(heap_pos, 0, sizeof(heap_pos)); for (int i=1; i<=n; i++) for (int j=1; j<=m; j++){ heap_pos[i][j] = ++len; nodes[len] = heap_node(i, j); } for (int i=len/2; i>0; i--) heap_down(i); }
void heap_down (int cur) { int n1 = cur * 2 + 1; int n2 = cur * 2 + 2; int next = n1; if (n2 < n && timer_timeout[key[n2]] < timer_timeout[key[n1]]) { next = n2; } if (next < n && timer_timeout[key[cur]] > timer_timeout[key[next]]) { swap(cur, next); heap_down(next); } }
matcher::heap::heap(map <int, double> *weights, int n, class matcher *o) { owner = o; len = 0; for (int i=1; i<=n; i++){ for (std::map<int, double> ::iterator it=weights[i].begin(); it != weights[i].end(); it++) { heap_pos[i][it->first] = ++len; nodes[len] = heap_node(i, it->first); } } for (int i=len/2; i>0; i--) heap_down(i); }
UThreadSocket_t * Timer::PopTimeout() { if (timer_heap_.empty()) { return nullptr; } UThreadSocket_t * socket = timer_heap_[0].socket_; UThreadSocketSetTimerID(*socket, 0); std::swap(timer_heap_[0], timer_heap_[timer_heap_.size() - 1]); timer_heap_.pop_back(); if (timer_heap_.size() > 0) { heap_down(0); } return socket; }
/* * camq_remove: Given an array of cam_pinfo* elevements with the * Heap(1, num_elements) property and an index such that 1 <= index <= * num_elements, remove that entry and restore the Heap(1, num_elements-1) * property. */ cam_pinfo * camq_remove(struct camq *queue, int index) { cam_pinfo *removed_entry; if (index == 0 || index > queue->entries) return (NULL); removed_entry = queue->queue_array[index]; if (queue->entries != index) { queue->queue_array[index] = queue->queue_array[queue->entries]; queue->queue_array[index]->index = index; heap_down(queue->queue_array, index, queue->entries - 1); } removed_entry->index = CAM_UNQUEUED_INDEX; queue->entries--; return (removed_entry); }
/* * camq_remove: Given an array of cam_pinfo* elevements with the * Heap(1, num_elements) property and an index such that 1 <= index <= * num_elements, remove that entry and restore the Heap(1, num_elements-1) * property. */ cam_pinfo * camq_remove(struct camq *queue, int index) { cam_pinfo *removed_entry; if (index <= 0 || index > queue->entries) panic("%s: Attempt to remove out-of-bounds index %d " "from queue %p of size %d", __func__, index, queue, queue->entries); removed_entry = queue->queue_array[index]; if (queue->entries != index) { queue->queue_array[index] = queue->queue_array[queue->entries]; queue->queue_array[index]->index = index; heap_down(queue->queue_array, index, queue->entries - 1); } removed_entry->index = CAM_UNQUEUED_INDEX; queue->entries--; return (removed_entry); }
int main() { FILE *filein = fopen("butter.in", "r"); fscanf(filein, "%d %d %d", &cows, &v, &e); for(int i = 0; i < cows; ++i) { fscanf(filein, "%d", &cow_pos[i]); --cow_pos[i]; } for(int i = 0; i < v; ++i) { degree[i] = 0; } for(int i = 0; i < e; ++i) { int a,b,c; fscanf(filein, "%d %d %d", &a, &b, &c); --a; --b; con[a][degree[a]] = b; cost[a][degree[a]] = c; ++degree[a]; con[b][degree[b]] = a; cost[b][degree[b]] = c; ++degree[b]; } fclose(filein); for(int i = 0; i < cows; ++i) { heapsize = v; for(int j = 0; j < v; ++j) { heap_id[j] = j; heap_val[j] = BIG; heap_lookup[j] = j; } heap_val[cow_pos[i]] = 0; heap_up(cow_pos[i]); bool fixed[MAXV]; memset(fixed, false, v); for(int j = 0; j < v; ++j) { int p = heap_id[0]; dist[i][p] = heap_val[0]; fixed[p] = true; heap_swap(0, heapsize-1); --heapsize; heap_down(0); for(int k = 0; k < degree[p]; ++k) { int q = con[p][k]; if(!fixed[q]) { if(heap_val[heap_lookup[q]] > dist[i][p] + cost[p][k]) { heap_val[heap_lookup[q]] = dist[i][p] + cost[p][k]; heap_up(heap_lookup[q]); } } } } } int best = BIG; for(int i = 0; i < v; ++i) { int total = 0; for(int j = 0; j < cows; ++j) { total += dist[j][i]; } if( best > total ) best = total ; } FILE *fileout = fopen("butter.out", "w"); fprintf(fileout, "%d\n", best); fclose(fileout); return(0); }