Transportation
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5047 | Accepted: 2073 |
Description
Ruratania is just entering capitalism and is establishing new enterprising activities in many fields in- cluding 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 number m. The company runs an 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 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 included in the order and the price of their train tickets. The total earning is the sum of the earnings from all accepted orders.
Input
The input file is divided into blocks. The first line in each block contains three integers: passenger capacity n of the train, the number of the city B station and 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 be maximum 22 orders. The number of the city B station will be at most 7. The block where all three numbers in the first line are equal to zero denotes the end of the input file.
Output
The 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 Input
10 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 Output
19 34
题目分析:
题目大意是给出车站的train tickets,让你去确定在保证每站人数不超的情况下赚的钱最多是多少。类似于一颗二树,从头走到底看最后赚的钱最大,往左走还是往右走就看当前的订单选还是不选。
代码:
#include<iostream>
#include<algorithm>
using namespace std;
int n, numb, num_order;
struct Order {
int s; //start
int e; //end
int p; //people
};
Order sta[22];
int m = 0, maximum = 0;
int down[8]; //记录下车人数
void DFS(int i,int m,int sum) { //i是当前是第几个订单,m是前面订单方案得出的钱,sum是到当前站点时车上的人数
if (i == num_order) {
maximum = max(m, maximum);
return;
}
if (i > 0) { //求出车到达当前订单起始站点时车山的人数
for (int t = sta[i - 1].s + 1; t <= sta[i].s; t++) {
sum -= down[t];
}
}
//对于当前订单有两种情况,两种情况都要搜一下,因为不知到当前订单选还是不选使得赚的钱最多
//但是当当前车上的人数sum+当前订单的人数sta[i].p大于车上人的容量时一定不选。
//所以需要在选择当前订单前加if语句判断一下。
if (sum + sta[i].p <= n) {
down[sta[i].e] += sta[i].p;
sum += sta[i].p;
DFS(i + 1, m+(sta[i].e - sta[i].s)*sta[i].p,sum);
sum -= sta[i].p;
down[sta[i].e] -= sta[i].p;
}
DFS(i + 1, m, sum);
}
//将订单按起始站升序,若相等则按终止站升序
bool compare(const Order &a, const Order &b) {
if (a.s == b.s)return a.e < b.e;
return a.s < b.s;
}
int main() {
while (cin >> n >> numb >> num_order&&n) {
for (int i = 0; i < num_order; i++) {
cin >> sta[i].s >> sta[i].e >> sta[i].p;
}
memset(down, 0, sizeof(down));
sort(sta, sta + num_order, compare);
DFS(0,0,0);
cout << maximum << endl;
maximum = -1;
}
return 0;
}