题目来源:http://poj.org/problem?id=1040

题目大意:

  某运输公司要做一个测试。从A城市到B城市的一条运输线路中有若干个站,将所有站包括A和B在内按顺序编号为0到m。该路线上的一趟列车,乘客最大容量为cap。车开出之前,该趟车会收到来自各个车站的车票订单请求。一个请求包括起点站编号,终点站编号和乘客人数。每张车票的价格为起点到终点之间相隔的站数。在不超载的情况下,求一趟列车能获得的最大收益。对于某一个订单请求,要么接受要么拒绝,不能只接受其中的一部分乘客。

输入:每个测试用例一个数据块。每块的第一行有3个整数:cap:(车的容量), m:(终点城市的编号, 最大为7),n:(订单数,最大为22).接下来的n行每行三个数表示一个订单,三个数分别为起点、终点、人数。

输出:没个测试用例单独一行,输出一个整数表示该趟列车能获得的最大收益。


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

由于数据量不算太大,直接用搜索就可以解决。搜索前先对所有订单按起点的顺序进行排序。

不加剪枝的dfs代码,耗时391ms:

 //////////////////////////////////////////////////////////////////////////
// POJ1040 Transportation
// Memory: 264K Time: 391MS
// Language: C++ Result: Accepted
////////////////////////////////////////////////////////////////////////// #include <iostream>
#include <algorithm> using namespace std; int n, m, cap; class Order {
public:
int start;
int destination;
int cnt;
}; Order orders[];
int maxE;
//记录每站有多少个乘客
int pasCnt[]; //比较函数,按start升序,destination降序,cnt降序
bool cmp(const Order &a, const Order &b) {
return a.start == b.start
? (a.destination == b.destination ? (b.cnt < a.cnt) : b.destination < a.destination)
: a.start < b.start;
} void dfs(int i, int now) {
if (now > maxE) {
maxE = now;
}
for (int t = i; t < n; ++t) {
if (pasCnt[orders[t].start] + orders[t].cnt > cap) continue;//reject
for (int j = orders[t].start; j < orders[t].destination; ++j)
//get in
pasCnt[j] += orders[t].cnt;
dfs(t + , now + orders[t].cnt * (orders[t].destination - orders[t].start));
for (int j = orders[t].start; j < orders[t].destination; ++j)
pasCnt[j] -= orders[t].cnt;
}
} int main(void) {
while (true) {
cin >> cap >> m >> n;
if (cap == && m == && n == ) break;
for (int i = ; i < n; ++i)
cin >> orders[i].start >> orders[i].destination >> orders[i].cnt;
//按起点顺序将订单排序
sort(orders, orders + n, cmp);
maxE = ;
memset(pasCnt, , sizeof(pasCnt));
dfs(, );
cout << maxE << endl;
}
system("pause");
return ;
}

(不剪枝DFS)

看了别人剪枝的方法,加上之后,时间瞬间降到16ms.

剪枝方法如下:

每一个order添加两个项v和r。v表示该张订单的收益值。r表示如果选择该选项最多还能获得多少收益,这个值只是一个上界而不是上确界。计算方法是,计算排序后该订单和排在该订单之后的所有订单的收益值之和。因为dfs是按订单顺序依次搜索的,所以当当前收益值加上该订单的r值小于等于已搜到的最大收益,则不必再考虑该订单及以后的订单,因为不可能再得到比已知最大收益更大的收益。

 //////////////////////////////////////////////////////////////////////////
// POJ1040 Transportation
// Memory: 236K Time: 16MS
// Language: C++ Result: Accepted
////////////////////////////////////////////////////////////////////////// #include <iostream>
#include <algorithm> using namespace std; int n, m, cap; class Order {
public:
int start;
int destination;
int cnt;
int v; //该订单能获得的收益
int r; //选取该订单之后最多还能获得多少收益
}; Order orders[];
int maxE;
//记录每站有多少个乘客
int pasCnt[]; //比较函数,按start升序,destination降序,cnt降序
bool cmp(const Order &a, const Order &b) {
return a.start == b.start
? (a.destination == b.destination ? (b.cnt < a.cnt) : b.destination < a.destination)
: a.start < b.start;
} void dfs(int i, int now) {
if (now > maxE) {
maxE = now;
}
for (int t = i; t < n; ++t) {
if (orders[t].r + now <= maxE) return;
if (pasCnt[orders[t].start] + orders[t].cnt > cap) continue;//reject
for (int j = orders[t].start; j < orders[t].destination; ++j)
//get in
pasCnt[j] += orders[t].cnt;
dfs(t + , now + orders[t].v);
for (int j = orders[t].start; j < orders[t].destination; ++j)
pasCnt[j] -= orders[t].cnt;
}
} int main(void) {
while (true) {
cin >> cap >> m >> n;
if (cap == && m == && n == ) break;
for (int i = ; i < n; ++i) {
cin >> orders[i].start >> orders[i].destination >> orders[i].cnt;
orders[i].v = orders[i].cnt * (orders[i].destination - orders[i].start);
}
//按起点顺序将订单排序
sort(orders, orders + n, cmp);
for (int i = n - , sum = ; i >= ; --i) {
sum += orders[i].v;
orders[i].r = sum;
}
maxE = ;
memset(pasCnt, , sizeof(pasCnt));
dfs(, );
cout << maxE << endl;
}
return ;
}

(剪枝)

附上测试数据:


Test Input


Test Output

POJ1040 Transportation的更多相关文章

  1. poj1040 Transportation(DFS)

    题目链接 http://poj.org/problem?id=1040 题意 城市A,B之间有m+1个火车站,第一站A站的编号为0,最后一站B站的编号为m,火车最多可以乘坐n人.火车票的票价为票上终点 ...

  2. Transportation poj1040

    Ruratania is just entering capitalism and is establishing new enterprising activities in many fields ...

  3. POJ 1797 Heavy Transportation(最大生成树/最短路变形)

    传送门 Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 31882   Accept ...

  4. 【HDU 4940】Destroy Transportation system(无源无汇带上下界可行流)

    Description Tom is a commander, his task is destroying his enemy’s transportation system. Let’s repr ...

  5. Heavy Transportation(最短路 + dp)

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

  6. POJ 1797 Heavy Transportation (Dijkstra变形)

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

  7. poj 1797 Heavy Transportation(最短路径Dijkdtra)

    Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 26968   Accepted: ...

  8. POJ 1797 Heavy Transportation

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

  9. uva301 - Transportation

      Transportation Ruratania is just entering capitalism and is establishing new enterprising activiti ...

随机推荐

  1. Agc_006 E Rotate 3x3

    题目大意 给定一个$3\times N$的方阵,每个位置的数恰好是每一个$[1,3\times N]$中的数. 初始时,每个位置$[x,y]$填的是$3(y-1)+x,(1\leq x\leq N,1 ...

  2. Qt Quick中的信号与槽

    在QML中,在Qt Quick中,要想妥善地处理各种事件,肯定离不开信号与槽,本博的主要内容就是整理Qt 中的信号与槽的内容. 1. 链接QML类型的已知信号 QML中已有类型定义的信号分为两类:一类 ...

  3. 使用 Anthem.NET 框架的一个调试经历

    简介:Anthem 是一个很好用的 Ajax 框架,支持 ASP.NET 1.1, 2.0.由于该框架的所有控件都继承自 ASP.NET 自身的服务器控件,保留了几乎所有这些控件的属性和行为(除了把它 ...

  4. 理解Promise

    一.Propmise基本用法 Promise用于发送一个异步完成的结果,是替代回调函数的另一种选择.可以把Promise理解为一种异步函数. 以下函数通过一个Promise来异步地返回一个结果 fun ...

  5. 添加一个用户并且让用户获得root权限

    1.创建一般用户: 完全参考默认值创建一个用户, 一般账号UID应该是500以后的. 默认会创建用户家目录和账号一模一样的群组名.创建使用账号且给予口令才算完成了用户的创建流程. useradd us ...

  6. Scala总结

    Scala总结 ===概述 scala是一门以Java虚拟机(JVM)为目标运行环境并将面向对象和函数式编程的最佳特性结合在一起的静态类型编程语言. scala是纯粹的面向对象的语言.java虽然是面 ...

  7. smbpasswd和pdbedit

    samba用户管理: smbpasswd :smbpasswd命令属于samba套件,能够实现添加或删除samba用户和为用户修改密码. smbpasswd [options] USERNAME -a ...

  8. Linq to Object之非延迟标准查询操作符

    非延时标准查询操作符是指不具备延时查询特性的标准查询操作符,这些操作符一般用于辅助延时标准查询操作符使用. 1.ToArray操作符 ToArray操作符用于将一个输入序列转换成一个数组. 方法原型: ...

  9. DES加密/解密

    /// <summary> /// DES加密/解密类. /// </summary> public class DESEncrypt { #region ========加密 ...

  10. CentOS7 搭建LNMP

    一.        安装依赖文件 1.  sudo yum install gcc gcc-c++ zlib zlib-devel libxml2 libxml2-devel openssl open ...