Title Link: http://poj.org/problem?id=2431
Test instructions: A truck needs to travel L distance, the oil content of the car is P, in the course of travel there are n gas stations
#include <stdio.h>#include<stdio.h>#include<string.h>#include<queue>#include<algorithm>#defineN 11000#defineINF 0x3f3f3f3fusing namespacestd;intMax_heap[n], K;///K represents the number of max_heap;intp;voidPush (intX///Insert x in the maximum heap;{max_heap[k+ +] = x;///put it at the last node; intt = k1; while(Max_heap[t] > max_heap[t/2] )///when its value is greater than the value of its parent node, swap the position;{swap (max_heap[t], max_heap[t/2]); T= t/2; }}voidPop () {K--;///Delete the element, the total number of heap minus one;max_heap[1] = Max_heap[k];///Put the last number on top, then update the heap step by step;Max_heap[k]= -1;///prevention of cross-border, equivalent to the establishment of Sentinel; intt =1;///from the first node to the beginning of the update; while(Max_heap[t] < max_heap[t*2] || Max_heap[t] < max_heap[t*2+1]) { if(Max_heap[t] < max_heap[t*2] && max_heap[t*2] > max_heap[t*2+1]) {///when the left son is greater than it, and satisfies the left son greater than the right son, let the left son go up;Swap (Max_heap[t], max_heap[t*2]); T= t*2; } Else{swap (max_heap[t], max_heap[t*2+1]); T= t*2+1; } }}intN, P, L, ans, F;structnode{intb;} S[n];intCMP (node N1,node n2) {returnn1.a<n2.a;}voidSlove () {memset (max_heap,-1,sizeof(max_heap)); max_heap[0] =INF; K=1; intpos =0, i =0; while(I <=N) {intD = s[i].a-POS; while(k!=1&& P <d) {intp = max_heap[1]; Pop (); P+=p; Ans++; } if(P >=d) {Push (s[i].b); POS=s[i].a; P-=D; } Else{ans= -1; return ; } I++; }}intMain () {inti; while(SCANF ("%d", &n)! =EOF) {memset (s),0,sizeof(s)); Ans=0; for(i=0; i<n; i++) scanf ("%d%d", &S[I].A, &s[i].b); scanf ("%d%d", &l, &q); for(i=0; i<n; i++) s[i].a= L-s[i].a; S[I].A=L; S[I].B=0; Sort (s, S+n+1, CMP); Slove (); printf ("%d\n", ans); } return 0;}
View Code
Expedition---poj2431 (priority queue-heap)