题目来源: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:

 1 //////////////////////////////////////////////////////////////////////////
 2 //        POJ1040 Transportation
 3 //        Memory: 264K        Time: 391MS
 4 //        Language: C++        Result: Accepted
 5 //////////////////////////////////////////////////////////////////////////
 6 
 7 #include <iostream>
 8 #include <algorithm>
 9 
10 using namespace std;
11 
12 int n, m, cap;
13 
14 class Order {
15 public:
16     int start;
17     int destination;
18     int cnt;
19 };
20 
21 Order orders[22];
22 int maxE;
23 //记录每站有多少个乘客
24 int pasCnt[8];
25 
26 //比较函数,按start升序,destination降序,cnt降序
27 bool cmp(const Order &a, const Order &b) {
28     return a.start == b.start 
29         ? (a.destination == b.destination ? (b.cnt < a.cnt) : b.destination < a.destination) 
30         : a.start < b.start;
31 }
32 
33 void dfs(int i, int now) {
34     if (now > maxE) {
35         maxE = now;
36     }
37     for (int t = i; t < n; ++t) {
38         if (pasCnt[orders[t].start] + orders[t].cnt > cap) continue;//reject
39         for (int j = orders[t].start; j < orders[t].destination; ++j) 
40             //get in
41             pasCnt[j] += orders[t].cnt;
42         dfs(t + 1, now + orders[t].cnt * (orders[t].destination - orders[t].start));
43         for (int j = orders[t].start; j < orders[t].destination; ++j)
44             pasCnt[j] -= orders[t].cnt;
45     }
46 }
47 
48 int main(void) {
49     while (true) {
50         cin >> cap >> m >> n;
51         if (cap == 0 && m == 0 && n == 0) break;
52         for (int i = 0; i < n; ++i) 
53             cin >> orders[i].start >> orders[i].destination >> orders[i].cnt;
54         //按起点顺序将订单排序
55         sort(orders, orders + n, cmp);
56         maxE = 0;
57         memset(pasCnt, 0, sizeof(pasCnt));
58         dfs(0, 0);
59         cout << maxE << endl;
60     }
61     system("pause");
62     return 0;
63 }
View Code (不剪枝DFS)

相关文章:

  • 2021-06-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-15
  • 2021-12-29
猜你喜欢
  • 2022-12-23
  • 2021-12-07
  • 2022-12-23
  • 2021-08-11
  • 2021-06-15
  • 2021-08-31
相关资源
相似解决方案