301 – TransportationTime limit: 3.000 seconds |
Transportation |
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
1 2 3 4 5 6 7 8 9 10 11 |
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
1 2 |
19 34 |
关于题意的话要注意对于单张订单,只能全部接受所有乘客或者全部拒绝所有乘客,比如
1 2 |
1 2 7 |
这张订单,如果要接受的话就必须搭载7位乘客,而不能只搭载其中一部分,这样的话就比较好处理一点。
然后回溯暴力的时候不能太暴力了,从第一张订单开始一张一张往下DFS,每次DFS只用管当前订单以下的。
最后坑爹的是每个输入的开始三个数即火车最大载客量和B站个数以及订单个数是可能部分为零的,以前类似的情况都是习惯while(… a && b && c),搞笑的是之前应该是题目的数据也没太在意这个地方所以基本没出问题,这次乖乖吃了两个WA 囧。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
/******************************************************** * File Name: uvaoj301.cpp * Author: razrLeLe * Mail: razrlele@gmail.com * Homepage: https://yueyu.io * Created Time: Wed 04 Mar 2015 07:41:21 PM CST ******************************************************/ #include "vector" #include "set" #include "deque" #include "queue" #include "algorithm" #include "functional" #include "iostream" #include "cstdio" #include "cmath" #include "cstdlib" #include "string" #include "cstring" #include "string.h" #include "map" #include "cctype" #include "list" #include "stack" using namespace std; #define INF 0x3f3f3f3f #define LOCAL const int maxn = 30; int n, B, order; int start[maxn], desti[maxn], numbers[maxn]; int ans; int capa[10]; void DFS(int a, int sum) { ans = max(ans, sum); for(int i = a+1; i <= order; i++) { bool ok = true; for(int j = start[i] ; j < desti[i]; j++) if(capa[j] < numbers[i]) { ok = false; break; } if(!ok) continue; int tmp = (desti[i]-start[i])*numbers[i]; for(int j = start[i]; j < desti[i]; j++) capa[j] -= numbers[i]; DFS(i, sum+tmp); for(int j = start[i]; j < desti[i]; j++) capa[j] += numbers[i]; } return ; } int main() { #ifdef LOCAL freopen("/home/razrlele/build/data.txt", "r", stdin); freopen("/home/razrlele/build/out.txt", "w", stdout); #endif while(scanf("%d %d %d", &n, &B, &order) && (n || B || order)) { for(int i = 1; i <= order; i++) { scanf("%d %d %d", &start[i], &desti[i], &numbers[i]); } ans = 0; for(int i = 0; i <= B; i++) capa[i] = n; DFS(0, 0); printf("%d\n", ans); } return 0; } |