poj:2413--Priority Queue--a sort of structure CMP function level

Source: Internet
Author: User

I want to translate the topic!!!

/*

A group of cows grabbed a truck and ventured on an ExpeditionDeep into the jungle. Being rather poor drivers, the cows unfortunately managed to run over a rock and puncture the truck ' s fuel tank. The truck now leaks one unit of fuel every unit of distance it travels. A group of cows dragged a lorry in preparation for an expedition into the depths of the jungle. Because of the damn driver, the cows are now being asked to pull a big rock and they break the fuel tank of the truck. Now the tank will leak a unit of fuel every time it goes on a trip.
To repair the truck, the cows need to drive to the nearest town (no more than 1,000,000 units distant) down a long, Windin G Road. On this road, between the town and the current location of the truck, there is n (1 <= n <=) fuel stops wher E The cows can stop to acquire additional fuel (1..100 units at each stop). To repair the tank, the cows need to pull the truck to the nearest town (no more than 1 million distance units) as soon as possible, and go through a long, tortuous path. On this road, the cows are in the exact location and the town is separated by N gas stations. They can get any amount of fuel there (the data will be between 1-100)

The jungle is a dangerous place for humans and was especially dangerous for cows. Therefore, the cows want to make the minimum possible number of stops for fuel on the the-the-the-town. Fortunately, the capacity of the fuel tank on their truck are so large that there are effectively no limit to the amount of Fuel it can hold. The truck is currently L units away from the town and have p units of fuel (1 <= P <= 1,000,000). The jungle is a dangerous place for humans, especially for cows. So, cows want to go through the fewest gas stations (it takes time to refuel at a gas station, right?). )。 Fortunately, the truck tank is large in capacity. Now the truck is in the distance of the specific town L unit, and at this time the car has p units of fuel.
Determine the minimum number of stops needed to reach the town, or if the cows cannot reach the town at all. Figure out they need to pass the least gas station, if not to the town, the output -1.*/a bit machine translation feel, but really I translated T, T Code has been on the POJ AC, thinking I reference the information, but each line of code is I think after writing down. I'm going to write down my notes where I'm going to pay attention. And the code has two versions, one is to use pair<> to sort the structure, one is to use the CMP function to sort. I'm just sticking to the CMP part.
/*
Train of thought: In the case of the same distance, in order to make the least number of refueling, it is necessary to refuel every time as much as possible. At the time of planning the gas station, if you find that the current amount of oil does not allow them to reach the next stop, in front of the most refueling gas station to refuel. This will not happen at the next stop. You can then use the priority queue for operations.
*/
1#include <cstdio>2#include <algorithm>3#include <queue>4 #defineMax_n 10000+55 using namespacestd;6 7 structdis_fuel{8 intdis;9 intfuel;Ten };
/*
CMP (defines two null pointers, const void *a,const void *b)
Casts two null pointers to the Dis_fuel type and points to the struct member dis in the struct. If a>b, returns 1 if A<b returns-1 for ascending processing.
*/ One intcmpConst void*a,Const void*b) { Areturn * (Dis_fuel *a). Dis > * (dis_fuel *b)? 1:-1; the } - intMain () - { - //establishing a priority queue +Priority_queue <int>que; - + structDis_fuel Ab[max_n]; A at intnum,a[max_n],b[max_n],l,p; -scanf"%d",&num); - - for(inti =0; I < num;i++){ -scanf"%d%d",&a[i],&b[i]); - } inscanf"%d%d",&l,&P); - to for(inti =0; I < num;i++){ +Ab[i].dis = L-A[i]; -Ab[i].fuel =B[i]; the }
/*
The structure is sorted by qsort. The format is qsort (the structure array name, the number of arrays, the memory space of the array elements, the name of the function);
*/ *Qsort (Ab,num,sizeof(ab[0]), CMP); $ Panax NotoginsengAb[num].dis =L; -Ab[num].fuel =0; thenum++; + A intTank = P,pos =0, ans =0, Dis_; the/*
Each currently ponit is traversed within the for loop. If the current amount of oil does not keep him going to the next stop, stop to refuel. The amount of oil at this time is the maximum amount of oil available at the front of the gas station. If you can, go ahead and put the amount of oil that each gas station can supply into the priority queue.
*/
+ for(inti =0; I < num;i++){ -Dis_ = Ab[i].dis-Pos; $ $ while(Dis_ >tank) { - if(Que.empty ()) { -printf"-1\n"); the return 0; - }WuyiTank + =que.top (); the Que.pop (); -ans++; Wu } - AboutTank-=Dis_; $pos =Ab[i].dis; - Que.push (ab[i].fuel); - } - Aprintf"%d\n", ans); + the return 0; - $}

poj:2413--Priority Queue--a sort of structure CMP function level

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.