POJ1040【Transportation】(递归与回溯、dfs)

链接:POJ1040【Transportation】

题目描述:
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.

输入描述:
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.

输出描述:
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.

输入样例:

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

输出样例:

19
34

题意:
一些火车从A到B站,最大载客量为n人。
火车票价格等于始发站至目的站(含目的站)的站(站)数。
如果由于载客量限制,不能接受所有订单,其拒绝政策是完全接受或完全拒绝单个车站的单个订单。
求最大总收入,总收入是所有已接受订单的收入之和。

思路:
利用dfs从A站一直搜索到B站
分析递归:
每一次递归先计算当前车内人数
判断是否会超载
不超载则接受订单,并进行下一次递归,要记得回溯时恢复到未接受订单
超载则不接受订单,并进行下一次递归
全部订单检查完成则退出递归,并返回最大收入

代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxorder = 22; // 最大订票量
const int maxsta = 8; // 最大车站数,包括首站
int cap, sta, order; // 分别表示火车的容量,车站数(不包括首站)和订票量
int down[maxsta]; // 第i站下车的人数,包括首站
int ans; // 最终结果
struct ticket
{
    int start, des, pass;
    bool operator < (const ticket & p) const
	{
        if (start == p.start)
            return des < p.des;
        return start < p.start;
    } // 按此规则排序
}ord[maxorder];
void dfs(int ord_num,int passe,int money)
{
    if (ord_num == order) // 订票量全部检查完毕
    {
        ans = max(ans, money);
		return;
    }
    if (ord_num > 0)
        for (int i=ord[ord_num-1].start+1; i<=ord[ord_num].start; ++i)
            passe -= down[i]; // 减去下车人数,计算当前车内人数
    if (passe+ord[ord_num].pass <= cap) // 检查是否超载
    {
        down[ord[ord_num].des] += ord[ord_num].pass; // 不超载,接受订票
        dfs(ord_num+1, passe+ord[ord_num].pass, money+ord[ord_num].pass*(ord[ord_num].des-ord[ord_num].start));
        down[ord[ord_num].des] -= ord[ord_num].pass; // 恢复现场,以便后面回溯
    }
    dfs(ord_num+1, passe, money); // 不接受订票
}
int main()
{
    ios::sync_with_stdio(false);
    while (cin >> cap >> sta >> order && (cap || sta || order))
    {
        for (int i=0; i<order; i++)
            cin >> ord[i].start >> ord[i].des >> ord[i].pass; // 订票信息
        sort(ord, ord+order);
        ans = 0;
        dfs(0, 0, 0);
        cout << ans << endl;
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

温柔说给风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值