Transportation
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5324 Accepted: 2229
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
Source
Regionals 1995 >> Europe - Central
问题链接:POJ1040 HDU1456 UVA301 UVALive5516 Transportation
问题简述:
城市A与B之间有m+1个火车站,第一站A站的编号为0,最后一站B站的编号为m,火车最多可以乘坐n人。火车票的票价为票上终点站的编号减去起点站的编号。输入数据的第1行包括3个数据,分别是火车容量、车站数量和订单数量,接着输入订单。每个订单包括3个数,分别是订单起始站、终点站和人数。铁路公司可以选择接受订单或全部不接受订单,但是每个订单必须整体处理不可拆分,计算铁路公司最多可以赚多少钱。
问题分析:
通过枚举接受订单或不接受订单来计算铁路公司可以赚多少钱,用DFS实现。订单需要按照车站顺序排队进行处理。
。
程序说明:(略)
参考链接:(略)
题记:(略)
AC的C++语言程序如下:
/* POJ1040 HDU1456 UVA301 UVALive5516 Transportation */
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const short N = 22; // 最大订票量(maximum 22 orders)
const short M = 7 + 1; // 最大车站数,包括首站(The number of the city B station will be at most 7.)
int c, s, order; // 火车乘客容量,车站数(不含首站)和订票量
int down[M]; // 第i站下车的人数,包括首站
int ans;
struct Ticket {
int start, dest, pnum;
bool operator < (const Ticket& t) const {
return start == t.start ? dest < t.dest : start < t.start;
}
} ticket[N];
void dfs(int orderNum, int pnum , int amount)
{
if (orderNum == order) // 订票检查完毕
ans = max(ans, amount);
else {
if (orderNum > 0)
for (int i = ticket[orderNum - 1].start + 1; i <= ticket[orderNum].start; i++)
pnum -= down[i]; // 下车后人数
if (pnum + ticket[orderNum].pnum <= c) { // 超载检查
down[ticket[orderNum].dest] += ticket[orderNum].pnum; // 不超载则接受订票
int a = amount + ticket[orderNum].pnum * (ticket[orderNum].dest - ticket[orderNum].start); // 金额
dfs(orderNum+1, pnum + ticket[orderNum].pnum, a);
down[ticket[orderNum].dest] -= ticket[orderNum].pnum; // 恢复
}
dfs(orderNum + 1, pnum , amount); // 不接受订票
}
}
int main()
{
while(~scanf("%d%d%d", &c, &s, &order) && (c || s || order)) {
for (int i = 0; i < order; i++)
scanf("%d%d%d", &ticket[i].start, &ticket[i].dest, &ticket[i].pnum); // 读入订票信息
sort(ticket, ticket + order);
ans = 0;
memset(down, 0, sizeof(down));
dfs(0, 0, 0);
printf("%d\n", ans);
}
return 0;
}