|
301-transportation |
4584 |
32.83% |
1338 |
76.08% |
Question link:
Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 108 & page = show_problem & problem = 237
Question type: backtracking
Original question:
Ruratania is just entering capitalism and is establishing new Login Ising activities in your fields including transport. The transportation company transruratania is starting a new express train from City
A to City B with several stops in the stations on the way. The stations are successively numbered, city a station has number 0, City B station numberM. The company runs an experiment in order to improve passenger transportation capacity and thus
To increase its earnings. The train has a maximum capacityNPassengers. The price of the train ticket is equal to the number of stops (stations) between the starting station and the destination station (including the destination station). Before
The train starts its route from the city a, ticket orders are collected from all onroute stations. the ticket order from the station s means all reservations of tickets from s to a fixed destination station. in case the company cannot accept all orders because
Of the passenger capacity limitations, its rejection policy is that it either completely accept or completely reject single orders from single stations.
Write a program which for the given list of orders from single stations on the way from A to B determines the biggest possible total earning of the transruratania company. The earning from one accepted
Order is the product of the number of passengers ordered in the order and the price of their train tickets. The total earning is the sum of the earnings from all accepted orders.
Sample Input
10 3 40 2 11 3 51 2 72 3 1010 5 43 5 102 4 90 2 52 5 80 0 0
Sample output
1934
Question:
There is a transportation company that operates a section of the railway. Station A of the Railway Station B. From station a to Station B numbered 0... N-1.
Each train has a maximum of N passengers. The price of a ticket is calculated based on the number of stations. One station is charged at RMB 1, and N stations are charged at RMB n.
In order to maximize the benefits, all tickets are analyzed before each trip. orders with the same start point and end point are placed in the same order. Because the number of people is limited, if the number of people is too large
You must give up some orders. The practice of this company is a bit extreme, either the entire order is abandoned, or the entire order is accepted.
Write a program to output the maximum revenue.
Analysis and Summary:
I have been vomiting blood for countless times.
The reason is that, according to the inertial thinking, it is the same as handling the full arrangement, opening a vis array, and then recursively traversing all possibilities. This question is not in full order,
The full arrangement is accessible to all vertices, so it is necessary to mark the VIS array. However, some orders are not required, it will not be accessed in the future, but because it has not been accessed before, the records on the VIS array remain in the unaccessed status, so in the future, every recursion will try again to test the orders that don't need them, and judge whether they will be overloaded during the test, so the waste of time is very huge!
So, I 've been pushed to tle ..............
To solve this problem, you do not need to open the VIS array. In recursion, only search for the previous one. Do not go back to the previous one. Therefore, you must have never accessed the previous one. There is a parameter cur in the recursive function, which indicates that the element in the array is to be searched, and then the recursion starts to search.
How does one indicate the number of people on the vehicle and check whether the vehicle is overloaded? I opened a mark array, which indicates the number of people on each site. Each time an order is added
The number of people in this order is added to its start station (excluding the terminal station. Next time I want to check whether there is overload, I will check whether there will be
The number of persons allowed exceeds the limit.
# Include <iostream> # include <cstdio> # include <algorithm> # include <cstring> # define maxn 300 using namespace STD; int N, no_ B, numticket, maxsum, mark [maxn], last [maxn]; int status [maxn]; struct ticket {int start, end, num; int earn; int leftsum;} arr [maxn]; void DFS (INT cur, int sum) {If (sum> maxsum) {maxsum = sum ;}for (; cur <numticket; ++ cur) {int I; // pruning. If the remaining values are smaller than the maximum value, the stack is directly rolled back If (sum + arr [cur]. leftsum <Max Sum) return; for (I = arr [cur]. start; I <arr [cur]. end; ++ I) {mark [I] + = arr [cur]. num; If (MARK [I]> N) break; // if the load limit is exceeded} if (I = arr [cur]. end) {// DFS (cur + 1, sum + arr [cur]. earn); -- I ;}for (; I >= arr [cur]. start; -- I) {mark [I]-= arr [cur]. num ;}} int main () {# ifdef local freopen ("input.txt", "r", stdin); # endif int order; while (scanf ("% d", & N, & no_ B, & Order )! = EOF) {If (! N &&! No_ B &&! Order) break; numticket = 0; int A, B, C; For (INT I = 0; I <order; ++ I) {scanf ("% d", & A, & B, & C); If (C <= N) {// if the number of users exceeds the limit, arr [numticket] is not considered. start = A, arr [numticket]. end = B, arr [numticket]. num = C; arr [numticket]. earn = (B-a) * C; arr [numticket ++]. leftsum = (B-a) * C ;}} for (INT I = numTicket-2; I> = 0; -- I) Arr [I]. leftsum + = arr [I + 1]. leftsum; memset (VIS, 0, sizeof (VIS); memset (mark, 0, sizeof (Mark); maxsum =-2147483646; DFS (0, 0 ); printf ("% d \ n", maxsum);} return 0 ;}
-- The meaning of life is to give it meaning.
Original
Http://blog.csdn.net/shuangde800
,
D_double(For details, refer)