POJ 1040 Transportation

Source: Internet
Author: User

Links: http://poj.org/problem?id=1040

Transportation
Time limit:1000ms Memory limit:10000ktotal submissions:4235 accepted:1725
DescriptionRuratania is just entering capitalism and are establishing new enterprising activities in many fields in-cluding transport . The transportation company Transruratania are starting a new express train from City A to city B with several stops in the Stations on the. The stations is successively numbered, city A station had 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 are 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 is 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 the it either completely accept or completely reject single orders from a single stations.

Write a program which for the given list of orders from single stations on the the the the-the-from-A to B determines the biggest poss Ible Total earning of the Transruratania company. The earning from one accepted order are 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.

InputThe input file is divided into blocks. The first line is each block contains three Integers:passenger capacity N of the train and the number of the city B station The number of ticket orders from all stations. The next lines contain the ticket orders. Each ticket order consists of three Integers:starting station, destination station, number of passengers. In one block there can is maximum on orders. The number of the city B station would be in most 7. The block where all three numbers in the first line is equal to zero denotes the end of the input file.

OutputThe output file consists of lines corresponding to the blocks of the input file except the terminating block. Each such line contains the biggest possible total earning.

Sample Input10 3 4
0 2 1
1 3 5
1 2 7
2 3 10
10 5 4
3 5 10
2 4 9
0 2 5
2 5 8
0 0 0

Sample Output19
34

Source
Central Europe 1995

The main idea-from a to B there are several stations, numbering from 0 to M, the maximum capacity of the train is N. Tickets are collected from various stations before each train is driven. A booking information includes: Starting point, terminal, number of people. The fare is numerically equal to the number of stations between the starting point and the terminal (including the terminal, excluding starting point). As the maximum capacity of the train is certain, it is not always possible to accept all bookings. For a booking, you can only accept it all, or reject it all. Now give you the maximum capacity n, the total number of stations m, and the order bar booking information, you have to calculate the acceptance of certain bookings to make the most profit, and output this maximum profit.

Idea-Obviously this is a DFS problem, but pruning must be done. Sort by starting point first, and if the starting point is the same, the terminal row. And then in the order of Dfs: each to accept an order, the line of the station capacity of all add the number of people in this order, if found that the number of people exceeding the maximum capacity of N, it must stop, this order is unacceptable, the number of passengers in the station to reduce the amount of all. If all the stations along the line are not more than N, you can add the profit of the order, start testing the next order, and then cancel the number of stations along the line after the test is complete. Whenever you start a test order, pass in the form of an already available profit as a parameter and save this Max Max. Finally, after all the cases have been traversed, output max is the result.

Complexity analysis--time complexity: O (order*log (order)) +o ((N/passe) ^order), spatial complexity: O (Order)

Attach the AC code:


#include <iostream> #include <cstdio> #include <iomanip> #include <string> #include <cstring > #include <cmath> #include <algorithm> #include <queue>using namespace std;const double PI = ACOs (- 1.0); const DOUBLE E = exp (1.0); const short Maxorder = 22; Maximum Booking amount const short Maxsta = 8; Maximum number of stations, including the first station short cap, STA, order; Indicates the capacity of the train, the number of stations (excluding the first station) and the booking volume short down[maxsta]; Number of stops at station I, including the first stop short ans;    Final result struct ticket{short start, Des, pass;        BOOL Operator < (const ticket & P) const{if (start = = P.start) return des < p.des;    return start < P.start;    }//Sort by this rule} ord[maxorder];void DFS (short ord_num, short passe, short money); int main () {Ios::sync_with_stdio (false); while (CIN >> caps >> STA >> order && (Cap | | sta | | order)) {for (int i=0; I<ord Er i++) Cin >> ord[i].start >> ord[i].des >> ord[i].pass; Booking information sort (ord, Ord+order);        Ans = 0;        DFS (0, 0, 0);    cout << ans << endl; } return 0;} void Dfs (short ord_num, short passe, short) {if (Ord_num = = order)//booking volume all checked {ans = max (ans, money)        ;    Return } if (Ord_num > 0) for (int i=ord[ord_num-1].start+1; i<=ord[ord_num].start; ++i) passe-= down [i]; Minus the number of alighted, calculate the current number of in-car if (passe+ord[ord_num].pass <= cap)//check for overloading {Down[ord[ord_num].des] + + ord[ord_num]. Pass Do not overload, accept booking Dfs (ord_num+1, Passe+ord[ord_num].pass, money+ord[ord_num].pass* (Ord[ord_num].des-ord[ord_num].start))        ; Down[ord[ord_num].des]-= Ord[ord_num].pass; Resume the scene so that back backtrack} dfs (ord_num+1, passe, money); Booking not accepted}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 1040 Transportation

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.