P-Full tank?

Source: Internet
Author: User

Give a graph and the oil price at each point, know the fuel consumption of each road, and give some inquiries, calculate the minimum consumption of a car with the specified tank capacity from the start point to the end point.
Solution: BFS + priority queue
Introduction to priority queue: Use priority_queue in STL. Priority_queue is the maximum priority queue by default. Only priority_queue <int> q is required for declaration. If it is the smallest heap, It is troublesome.

Priority_queue <int, vector <int>, CMP> q. The CMP function is as follows: struct CMP {bool operator () (const Int & I, const Int & J) {return I> J; // note: is ">" rather than "<". }}


Graph storage: This question uses the form of an adjacent table to store graphs. Use the vector in STL. First, define the data structure as follows:
 

Typedef struct {int e; // adjacent point int Dist; // DISTANCE} edge;


Then define the Vector Array:
 

Vector <edge> graph [N]; // n is the number of vertices.


Vector implements an adjacent table better than a common two-dimensional array in that it does not need to record the number of adjacent points of each vertex, but simply uses a simple push_back () function to easily implement edge recording.
Answer: 1. The fee spent in city I is used as the weight of the priority queue.
2. Define the following struct to record the current status:
 

Typedef struct {int P; // int Q of the current city; // int SP of the remaining oil in the fuel tank; // current expense} state;


3. Define the tag array isvis [N] [m] to record the state where Q is raised to the city p.
4. during initialization, the starting state (u, 0, 0) is added to the queue.
5. Then, expand the cities in the queue headers each time taking the priority queue Header element (Mark isvis [p] [Q] When exiting the queue ). You can choose to refuel or not. If you increase the fuel per upgrade (if you increase the fuel tank capacity, the corresponding status changes (u, + 1, + p) [p indicates the oil price of the current city]. If the current status can reach the next city, the status of the next city will also enter the queue. Then execute 5 in a loop. Until the city in which the queue is sent is the target city.
Note: 1. Because the queue is weighted by cost, when a status has been marked as true, it indicates that the cost is already the smallest. Therefore, the same status after the queue can be skipped without processing.
2. When the current city is expanded, the extended status may be out of the queue. This indicates that the extended status has a better solution than the current status (the result of the priority queue ), however, the current State scaling costs more than the current state Scaling (because it is necessary to increase fuel and fuel ). Therefore, this status does not need to be added to the queue. This is Two pruning methods that use the priority queue.
Core code:
 

Void BFS () {int I, J, K, W; priority_queue <state, vector <State>, CMP> que; for (I = 0; I <N; I ++) {for (j = 0; j <101; j ++) {us [I] [J] = max; // The initial value is infinity isvis [I] [J] = false ;}} State S; S. P = u; S. Q = 0; S. SP = 0; US [u] [0] = 0; que. push (s); // isvis [S. p] [S. q] = true; State cur, TMP; while (! Que. empty () {cur = que. top (); que. pop (); If (isvis [cur. p] [cur. q] = true) continue; isvis [cur. p] [cur. q] = true; If (cur. P = V) {res = cur. SP; return;} If (cur. q + 1 <= C & isvis [cur. p] [cur. q + 1] = false) // The current fuel tank capacity cannot exceed {us [cur. p] [cur. q + 1] = us [cur. p] [cur. q] + P [cur. p]; TMP. P = cur. p; TMP. Q = cur. q + 1; TMP. SP = us [cur. p] [cur. q + 1]; que. push (TMP) ;}for (I = 0; I <graph [cur. p]. size (); I ++) {W = graph [cur. p] [I]. e; k = cur. q-graph [cur. p] [I]. DIS; // the remaining oil from city p to city I if (k> = 0 & isvis [w] [k] = false & cur. sp <us [w] [k]) // The minimum fee for the current consumption of less than W oil in the city {us [w] [k] = cur. SP; TMP. P = W; TMP. Q = K; TMP. SP = cur. SP; que. push (TMP); // isvis [TMP. p] [TMP. q] = true ;}}}}

P-Full tank?

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.