TOJ1003 Transportation(DFS)

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 on-route 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 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.

Output

The output consists of lines corresponding to the blocks of the input 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

深搜

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;

int ans,m,rode,num;
int down[9];//表示第i站下车的人数
struct Node{
    int from,to,num;
    bool operator < (const Node& b) const{//运算符重载
        if (from == b.from) //如果起点一样,比较终点
            return to < b.to;
        return from < b.from;//比较起点
    }
};
Node ticket[25];

void input(){
    for (int i = 0;i < num;i++){
        scanf("%d%d%d",&ticket[i].from,&ticket[i].to,&ticket[i].num);
    }
    ans = 0;
    memset(down,0,sizeof(down));
    sort(ticket,ticket+num);
}

void DFS(int station,int V,int val);

int main(){
    while(scanf("%d%d%d",&m,&rode,&num) && (m || rode || num)){
        input();
        DFS(0,0,0);
        printf("%d\n",ans);
    }
    return 0;
}

void DFS(int k,int V,int money){//V表示当前车上人数,k表示第k个订单,money表示收益
    if (k == num){//访问过所有订单
        ans = max(ans,money);
        return;
    }
    if (k > 0){//进行下车操作
        for (int i = ticket[k-1].from + 1;i <= ticket[k].from;i++)
            V -= down[i];
    }
    if (V + ticket[k].num <= m){//选第k份订单
        down[ticket[k].to] += ticket[k].num;//down[i]表示第i站下车的人数
        DFS(k+1,V + ticket[k].num,money + ticket[k].num * (ticket[k].to - ticket[k].from));
        down[ticket[k].to] -= ticket[k].num;//恢复
    }
    DFS(k+1,V,money);//不选第k份订单
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值