Transportation
Transportation |
Ruratania is just entering capitalism and is establishing new enterprising activities in many 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 capacityn 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 capacityn 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
虽然注意到order最大有22,理论上有2^22,但是由于运载量的限制,加上这个限制是否接订单回溯,TLD,
然后注意到车站只有7个,先以起点对orde排序,再以当前车站为状态,选择起点为该站的进行回溯,TLD,然后注意到每次枚举一个新order的时候不因改从第一个开始,
至少每次选了一个orde之后枚举开始值就可以+1,TLD,笨了,因改直接从上一个order之后开始枚举,因为我们对order排序了,之前的order可以接受的话已经回溯过了,
终于0.86s,ac....
#include<stdio.h> #include<math.h> struct node {int start,end,num,earn; }a[30]; int max,n,m,k,station[10],accept[30]; int dfs(int now,int money,int begin) //now当前所在车站, begin 需要考虑得order起始范围 {int i,j,f; if (money>max) max=money; if (now>=m) return 0; for (i=begin;i<=k;i++) if ((a[i].start==now)&&(accept[i]==0)) {f=1; for (j=now;j<a[i].end;j++) if (station[j]<a[i].num) {f=0;break;} if (f) {for (j=now;j<a[i].end;j++) station[j]-=a[i].num; accept[i]=1; dfs(now,money+a[i].earn,i+1); //开始太2了写了,dfs(now,money+A[I].earn,begin+1); 直接可以从i+1开始;因为i之前的我们都处理过了,就改了这句话AC accept[i]=0; for (j=now;j<a[i].end;j++) station[j]+=a[i].num; } } else if (a[i].start>now) break; dfs(now+1,money,begin); //当前这站不接受order }; int main() {int i,j,t; while (scanf("%d%d%d",&n,&m,&k),n+m+k) { for (i=0;i<m;i++) station[i]=n; for (i=1;i<=k;i++) accept[i]=0; for (i=1;i<=k;i++) {scanf("%d%d%d",&a[i].start,&a[i].end,&a[i].num); a[i].earn=(a[i].end-a[i].start)*a[i].num; } for (i=1;i<k;i++) for (j=i+1;j<=k;j++) {if (a[i].start>a[j].start) {t=a[i].start; a[i].start=a[j].start; a[j].start=t; t=a[i].earn; a[i].earn=a[j].earn; a[j].earn=t; t=a[i].end; a[i].end=a[j].end; a[j].end=t; t=a[i].num; a[i].num=a[j].num; a[j].num=t; } } max=0; dfs(0,0,1); printf("%d\n",max); } return 0; }