您的位置:首页 > 其它

uva 301 Transportation (DFS)

2013-08-11 14:08 387 查看


 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

题目大意:给出car限载人数,站数,和一些订单,订单内容有起始站,终点站,和人数, 赚取金额= (终点 - 起点) * 人数。求最大利润。
解题思路:dfs,层数为当前订单号,无非就是接与不接。

#include <stdio.h>
#include <string.h>

const int N = 25;
int n, star, m, max, car
, order
[3];

bool judge(int cur) {
for (int i = order[cur][0]; i < order[cur][1]; i++) {
car[i] += order[cur][2];
if (car[i] > n)
return false;
}
return true;
}

void find(int cur, int sum) {
if (cur == m) {
if (max < sum)
max = sum;
return ;
}

int rec
;
memcpy(rec, car, sizeof(car));
find(cur + 1, sum);
memcpy(car, rec, sizeof(rec));
if (judge(cur))
find(cur + 1, sum + order[cur][2] * (order[cur][1] - order[cur][0]));
memcpy(car, rec, sizeof(rec));
}

int main() {
while (scanf("%d%d%d", &n, &star, &m), n + m + star) {
// Init;
memset(car, 0, sizeof(car));
memset(order, 0, sizeof(order));
max = 0;

// Read;
for (int i = 0; i < m; i++)
for (int j = 0; j < 3; j++ )
scanf("%d", &order[i][j]);

find(0, 0);

printf("%d\n", max);
}
return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: