链接:http://poj.org/problem?id=1040

Transportation
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 4235 Accepted: 1725

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

Central Europe 1995



大意——从A到B有若干个车站,编号从0到m。列车的最大载客量是n。每次列车开车之前,会从各个车站收集订票信息。一条订票信息包含:起点站。终点站,人数。票价在数值上等于起点站与终点站之间的车站数(包含终点站,不包含起点站)。

因为列车的最大载客量是一定的。所以不一定能接受所有的订票。对于一条订票。仅仅能所有接受,或者是所有拒绝。如今给你最大载客量n。车站总数m,以及order条订票信息。要你计算出接受某些订票使之利润最大。而且输出这个最大利润。

思路——显然这是一个DFS的问题。但必须进行剪枝操作。

首先按起点站进行排序。假设起点站同样。则按终点站排。

然后按着顺序DFS:每接受一个订单,就把沿线所在站载客量所有加上这个订单中的人数。假设发现哪个站的人数超过了最大载客量n,就必须停下来,这个订单是不能接受的,就把所在站载客量中加上的人数所有减下来。

假设所有沿线所在站都没有超过n,就能够加上该订单的利润,開始測试下一个订单,測试完毕之后取消沿线所在站加的人数回溯。每当開始測试订单时,就把已经能获得的利润用參数的形式传递过去,保存这个最大值max。最后把所有的情况都遍历之后,输出max即得结果。



复杂度分析——时间复杂度:O(order*log(order))+O((n/passe)^order),空间复杂度:O(order)

附上AC代码:

#include <iostream>
#include <cstdio>
#include <iomanip>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
const double PI = acos(-1.0);
const double E = exp(1.0);
const short maxorder = 22; // 最大订票量
const short maxsta = 8; // 最大车站数,包含首站
short cap, sta, order; // 分别表示火车的容量,车站数(不包含首站)和订票量
short down[maxsta]; // 第i站下车的人数,包含首站
short ans; // 终于结果
struct ticket
{
short 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(short ord_num, short passe, short 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;
} void dfs(short ord_num, short passe, short 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); // 不接受订票
}

POJ 1040 Transportation的更多相关文章

  1. poj 1797Heavy Transportation(dijkstra变形)

    题目链接:http://poj.org/problem?id=1797 题意:有n个城市,m条道路,在每条道路上有一个承载量,现在要求从1到n城市最大承载量,而最大承载量就是从城市1到城市n所有通路上 ...

  2. POJ 3228Gold Transportation(二分+最大流)

    题目地址:POJ3288 这个题跟之前的一道题混了,感觉是一样的,所以连想都没怎么想就拆点然后求最短路然后二分求最大流了.结果连例子都只是,还一直以为又是哪里手残了..结果看了看例子,手算也确实不正确 ...

  3. POJ题目排序的Java程序

    POJ 排序的思想就是根据选取范围的题目的totalSubmittedNumber和totalAcceptedNumber计算一个avgAcceptRate. 每一道题都有一个value,value ...

  4. POJ 1797 Heavy Transportation (Dijkstra变形)

    F - Heavy Transportation Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & ...

  5. POJ 1797 Heavy Transportation

    题目链接:http://poj.org/problem?id=1797 Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K T ...

  6. poj 1797 Heavy Transportation(最大生成树)

    poj 1797 Heavy Transportation Description Background Hugo Heavy is happy. After the breakdown of the ...

  7. POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径)

    POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径) Description Background Hugo ...

  8. POJ.1797 Heavy Transportation (Dijkstra变形)

    POJ.1797 Heavy Transportation (Dijkstra变形) 题意分析 给出n个点,m条边的城市网络,其中 x y d 代表由x到y(或由y到x)的公路所能承受的最大重量为d, ...

  9. POJ 1797 ——Heavy Transportation——————【最短路、Dijkstra、最短边最大化】

    Heavy Transportation Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. 信息竞赛程序卡时_C++

    一.卡时简介 卡时是一个竞赛时常用的技巧 有些题目我们想不到完美算法就只能用暴力解决,但是此类方法一般时间复杂度较高,此时我们需要进行卡时 通俗来讲就是进行一个时间限制,让程序在达到这个时间后立马退出 ...

  2. 埃及分数问题_迭代加深搜索_C++

    一.题目背景 http://codevs.cn/problem/1288/ 给出一个真分数,求用最少的1/a形式的分数表示出这个真分数,在数量相同的情况下保证最小的分数最大,且每个分数不同. 如 19 ...

  3. "贪心"的考试

    先来落实比较简单的T3,T4 T3  团结的队伍 team 题目描述 众所周知,长郡信息组是一个团结友爱的优秀团队.为了弘扬团队精神,践行社会主义核心价值观,秉承朴实沉毅的校训,信息组决定外出游玩(. ...

  4. 第20章 HOOK和数据库访问

    转自: https://blog.csdn.net/u014162133/article/details/46573873 通过安装Hook过程,可以用来屏蔽消息队列中某些消息 The SetWind ...

  5. system call hooking 系统调用增加或劫持

    1. 引言:这篇文章提供了一种增加自定义系统调用或劫持原有的系统调用的实现方法,只针对 linux 系统.主要思路是获取系统调用表 sys_call_table 地址,然后用新函数地址覆盖系统调用表某 ...

  6. Android应用开发之使用Socket进行大文件断点上传续传

    http://www.linuxidc.com/Linux/2012-03/55567.htm http://blog.csdn.net/shimiso/article/details/8529633 ...

  7. 使用Android Studio开发调用.NET Webservice 之初体验

    本人是.NET出身 但苦于领导要让研究Android 外壳然后准备套html5  ,当试验兵真坑啊 但也没办法 咱还得研究啊,索性 不辜负领导的期望(也研究好两三天了)总算弄明白了 app本地存储 和 ...

  8. 牛客网 牛客练习赛7 A.骰子的游戏

    A.骰⼦的游戏 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 32768K,其他语言65536K64bit IO Format: %lld 题目描述 在Alice和Bob面前的是两个骰 ...

  9. 树链剖分【p1505】[国家集训队]旅游

    Description Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间 ...

  10. 多模态检索之CCA算法

    多模态检索主要是实现不同模态下的数据能相互检索,例如文本模态数据,和图像模态数据.要能实现他们之间的相互检索,首先要是它们相互关联起来.CCA·算法用于多模态检索步骤:      1)首先提取文本,图 ...