UVa 301:transportation

Source: Internet
Author: User
Tags ticket

Topic Link:

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_ problem&problem=237

Topic Type: Backtracking method

Original title:

Ruratania is just entering capitalism and are establishing new enterprising activities in many fields including. The transportation company Transruratania are 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 number M. The company runs a experiment in order to improve passenger transportation capacity and thus to increase its earnings. The train has a maximum capacity n passengers. The price of the train ticket was 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 to the city A, ticket orders are collected to all Onroute stations. The ticket order from the station's means all reservations of the 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 are that it either completely accept or completely the single orders from single reject.

Write a program which for the given list of orders from single stations on the way from A to B determines the biggest poss Ible Total earning to the Transruratania company. The earning from one accepted order is the product of the number of passengers included in the order and the price of thei R train tickets. The total earning are the sum of the earnings from all accepted orders.

Sample Input

3 4
0 2 1 1 3 5 1 2 7 2 3 ten 5 4 3 5 2 4 9 0 2 5 2 5 8 0 0 0

Sample Output


34

The main effect of the topic:

There is a transportation company that runs a railway, the railway A to station B. Starting from a station to B station number 0 .... N-1.

The limit of each train is n people, the price of the ticket by the number of stations, take a station to collect 1 yuan, n station that is n yuan.

In order to maximize the benefits, every time before driving, will first analyze all the tickets, the same starting point and end of the same order. Because of the size limit, if the number of too many

Words, you have to give up some orders. The company's approach is a bit extreme, either the entire order is abandoned, or the entire order is accepted.

Write a program that outputs the maximum amount of money you can earn.

Analysis and Summary:

This problem makes me vomit blood tle several times.

The reason is that, in accordance with the inertial thinking, and processing the whole arrangement of the same, open a vis array, and then recursively traverse all possible. And this one is different from the whole arrangement,

The whole arrangement is where all points will eventually be accessed, so open Vis array tag is necessary, but this problem because some orders are not, for not order, will not go to access it, and because there was no access, so the VIS array on its record or stay in no access to the state, So after each of the recursion will again to test those not to order, and when the temptation to judge whether it will overload, so the waste of time is very huge!

So, I have been tle ..........

To solve this problem, you can not open the VIS array. Recursion, only to the front search, the previous has visited do not go back to visit a, so the access must have not been visited. There is a parameter cur in the recursive function that represents the current search from the element in the array, and then the recursive search begins after that.

How to indicate the number of people on the car, check whether overload? I opened an array of Mark, which represents how many people are on each station. Each additional order, the

The number of this order is added to its starting station to the terminal (not including the terminal). Then the next time to check whether the overload, it depends on the number of the increase, there will be

Exceeding the limit of the number of people can.

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #define MAXN 30  
      
0 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 is added to the smaller than the largest, direct back stack if (Sum+arr[cur].leftsum < maxsum) return;  
            For (I=arr[cur].start i<arr[cur].end; ++i) {Mark[i] + = Arr[cur].num; if (Mark[i] > N) break;  
            If the limit is exceeded} if (i==arr[cur].end) {///No more than the limited-load 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%d%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%d%d", &a, &b, &c);  
                if (c <= N) {//number greater than the limit number of orders does not consider Arr[numticket].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; }

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.