transportation -- DFS

来源:互联网 发布:淘宝刷收藏软件哪个好 编辑:程序博客网 时间:2024/06/03 18:25

题目:

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


题意分析:

一辆列车最多可以承载n个人,这辆列车从始发站到终点站一共m站,同时有d个订单;对于每个订单来说都有3个数据,分别是上车站、下车站、上车人数,就是说才第i站上车之后在第j站下车之前车上会增加x人。每个订单的收益是,站数乘以人数,而且每一个订单的人都必须全接或者一个都不接。我们要计算最大收益。

思路:

用DFS处理每一种情况,难点在于处理每一站车上的人数是否会超时。

先放dfs代码:

void dfs(int numEO, int money){    ans = max(ans, money);    if(numEO == order)        return ;    dfs(numEO + 1, money);    bool bul = 1;    for(int i = ar[numEO].bgn; i < ar[numEO].nd; i++)    {        sum[i] += ar[numEO].number;        if(sum[i] > maxpeople)            bul = 0;    }    if(bul)    {        dfs(numEO + 1, money + (ar[numEO].nd - ar[numEO].bgn) * ar[numEO].number);    }    for(int i = ar[numEO].bgn; i < ar[numEO].nd; i++)    {        sum[i] -= ar[numEO].number;    }}

这里有一个判断很重要:

如果接单是否会超载?一开始我用的sum没有用数组,就导致了一个问题循环把一个订单的人数加入了sum中,然后就出了bug。后来改用sum[i]的数组代表每一站上的人数,判断一下如果如果接了这一单,会不会出现在某一站上超载,然后再进行后续操作。

还有一项把每一站的人数再减去:

这一点可以理解为在每一个dfs算时都要在相应的站上加上这一单的人数,算完之后要返回上一层时sum[i]要还原为上一层的人数,因此就要减去当前订单的人数来进行之后的dfs操作。


代码如下:

#include <cstdio>#include <iostream>#include <cstring>using namespace std;struct EO{    int bgn;    int nd;    int number;} ar[205];int ans;int maxpeople, station, order;int sum[205];void dfs(int numEO, int money){    ans = max(ans, money);    if(numEO == order)        return ;    dfs(numEO + 1, money);    bool bul = 1;    for(int i = ar[numEO].bgn; i < ar[numEO].nd; i++)    {        sum[i] += ar[numEO].number;        if(sum[i] > maxpeople)            bul = 0;    }    if(bul)    {        dfs(numEO + 1, money + (ar[numEO].nd - ar[numEO].bgn) * ar[numEO].number);    }    for(int i = ar[numEO].bgn; i < ar[numEO].nd; i++)    {        sum[i] -= ar[numEO].number;    }}int main(){    while(cin >> maxpeople >> station >> order && maxpeople + station + order > 0)    {        ans = 0;        memset(sum, 0, sizeof(sum));        for(int i = 0; i < order; i++)        {            cin >> ar[i].bgn >> ar[i].nd >> ar[i].number;        }        dfs(0, 0);        cout << ans << endl;    }    return 0;}
原创粉丝点击