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 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
题意:第一行输入w,n,m,其中w代表车的最大载客数,n代表有n个站牌,
然后有m组数据。一辆公交车从起点0出发至终点站牌n,每个人交的乘车的价钱是他的乘车的站牌数(例如自站牌1做到站牌2需要1元钱,自站牌1做到站牌3需要3元钱,以此类推)。需要注意的是如果到达某一个站牌时车上还剩5个座位,但是在这个站牌到达下个站牌的人数大于5个,则这辆车将不会让这群人上车(意思是要上车就全都上,否则全都不让上。),求出在这样的规则下,公交车的最大获益金额。
代码:
#include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> #include<stdlib.h> using namespace std; struct node { int x; int y; int z; int num; int ss; }q[10010]; int n,m,w; int t; int a[10010]; int maxx; void add(int x,int y,int z,int num) { q[t].x = x; q[t].y = y; q[t].z = z; q[t].num = num; q[t++].ss = num; } int cmp(const void *a,const void *b) { struct node *aa,*bb; aa = (struct node*)a; bb = (struct node*)b; if(aa->num!=bb->num) { return bb->num - aa->num; } else { return aa->x - bb->x; } } void DFS(int sum,int cnt) { if(maxx < sum) { maxx = sum; } for(;cnt<t;cnt++) { if(sum + q[cnt].ss < maxx) { return ; } int i; for(i=q[cnt].x;i<q[cnt].y;i++) { a[i] += q[cnt].z; if(a[i]>w) { break; } } if(i == q[cnt].y) { DFS(sum+q[cnt].num,cnt+1); i--; } for(;i>=q[cnt].x;i--) { a[i] -= q[cnt].z; } } } int main() { while(scanf("%d%d%d",&w,&n,&m)!=EOF) { if(w == 0 && n == 0 && m == 0) { break; } t = 0; maxx = 0; memset(a,0,sizeof(a)); int x,y,z,pnum; for(int i=0;i<m;i++) { scanf("%d%d%d",&x,&y,&z); if(z<=w) { pnum = (y-x)*z; add(x,y,z,pnum); } } qsort(q,t,sizeof(q[0]),cmp); for(int i=t-2;i>=0;i--) { q[i].ss = q[i].ss + q[i+1].ss; } DFS(0,0); printf("%d\n",maxx); } return 0; }