Esempio n. 1
0
File: map.c Progetto: 91he/Test
struct Path *road_min(struct Graph *graph, int from, int to){
	Heap *heap;
	HNode *array;
	Edge *edge, *tmp;
	struct Path *p, *q;
	int i, j, val, n = graph->num;
	int end = find_node(graph, to)->adj;
	int start = find_node(graph, from)->adj;
	struct VNode **path = malloc(sizeof(struct VNode*) * n);

	heap = heap_init();
	heap->num = n;
	array = heap->array;
	bzero(path, sizeof(struct VNode*) * n);

	for(i = 1; i <= n; i++){
		array[i].value = INF;
		array[i].vertex = i;
		array[i].pos = i;
	}

	array[start + 1].value = 0;
	heap_up(heap, start + 1);

	do{
		j = array[1].vertex - 1;

		if(j == end){
			break;
		}
		val = array[1].value;

		for(edge = graph->array[j]->link; edge; tmp = edge->next, edge = tmp){
			i = edge->node->adj + 1;
			if(array[array[i].pos].value > val + edge->dut){
				array[array[i].pos].value = val + edge->dut;
				heap_up(heap, array[i].pos);
				path[i - 1] = graph->array[j];
			}
		}
	}while(heap_top_del(heap));

	heap_destroy(heap);
	
	i = end;
	q = NULL;
	while(path[i]){
		j = path[i]->adj;
		p = malloc(sizeof(Path));
		p->node = path[i];
		p->next = q;
		q = p;
		i = j;
	}

	free(path);

	return q;
}
Esempio n. 2
0
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);
  }
}
Esempio n. 3
0
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);
    } 
}
Esempio n. 4
0
void dij()
{
	int i, j, t, k;
	init_heap();
	heap[++heap[0]] = 1;
	d[1] = 0; pos[1] = heap[0];
	for(i = 2;i <= n; i++) 
	{
		d[i] = oo;
		heap[++heap[0]] = i;
		pos[i] = heap[0];
	}
	for(k = 1;k <= n; k++)
	{
		i = pop();
		t = first[i];
		while(t != -1)
		{
			j = e[t].v;
			if(d[j] > d[i] + e[t].w)
			{
				d[j] = d[i] + e[t].w;
				heap_up(pos[j]);
			}
			t = e[t].next;
		}
	}
	printf("%d\n", d[n]);
}
Esempio n. 5
0
void heap_up (int cur) {
  int prev = (cur - 1) / 2;
  if (prev >= 0 && timer_timeout[key[cur]] < timer_timeout[key[prev]]) {
    swap(cur, prev);
    heap_up(prev);
  }
}
Esempio n. 6
0
void matcher::heap::push(int u, int v) {
	int pos = heap_pos[u][v];
	if (pos <= len && nodes[pos].u == u && nodes[pos].v == v)
		return;
	nodes[++len] = heap_node(u, v);
	heap_pos[u][v] = len;
	heap_up(len);
}
Esempio n. 7
0
/*
 * 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);
	}
}
Esempio n. 8
0
/*
 * camq_insert: Given an array of cam_pinfo* elememnts with
 * the Heap(1, num_elements) property and array_size - num_elements >= 1,
 * output Heap(1, num_elements+1) including new_entry in the array.
 */
void
camq_insert(struct camq *queue, cam_pinfo *new_entry)
{

	KASSERT(queue->entries < queue->array_size,
	    ("camq_insert: Attempt to insert into a full queue (%d >= %d)",
	    queue->entries, queue->array_size));
	queue->entries++;
	queue->queue_array[queue->entries] = new_entry;
	new_entry->index = queue->entries;
	if (queue->entries != 0)
		heap_up(queue->queue_array, queue->entries);
}
Esempio n. 9
0
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);
}
Esempio n. 10
0
void heap_up(int i) {
    if(i > 0 && heap_val[(i-1) / 2] > heap_val[i]) {
        heap_swap(i, (i-1)/2);
        heap_up((i-1)/2);
    }
}
Esempio n. 11
0
void Timer :: AddTimer(uint64_t abs_time, UThreadSocket_t * socket) {
    TimerObj obj(abs_time, socket);
    timer_heap_.push_back(obj);
    heap_up(timer_heap_.size());
}
Esempio n. 12
0
void heap_update(int index) {heap_up(index);}
Esempio n. 13
0
void heap_insert(int value) {
    heap[tot] = value;
    pos[value] = tot;
    heap_up(tot);
    tot++;
}