码迷,mamicode.com
首页 > 其他好文 > 详细

UVA - 301

时间:2014-11-28 20:10:08      阅读:362      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   color   os   sp   

 Transportation 

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 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

bubuko.com,布布扣
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <vector>
 5 #include <algorithm>
 6 
 7 using namespace std;
 8 
 9 class Node {
10 public:
11     int x,y;
12     int value;
13     Node (int xx,int yy,int v) {
14         x = xx;
15         y = yy;
16         value = v;
17     }
18     bool operator < (const Node& rhs) const {
19         return (x == rhs.x ? x < rhs.x : y < rhs.y);
20     }
21 };
22 
23 vector <Node > v;
24 
25 int visit[20];
26 int maxn;
27 int MaxNum,NumStation,NumBook;
28 
29 int check(int cur) {
30     int ok = 1;
31     for (int i = v[cur].x;i < v[cur].y;i++) {
32         if (visit[i] + v[cur].value > MaxNum) {
33             ok = 0;
34             break;
35         }
36     }
37     return ok;
38 }
39 
40 void dfs(int cur,int sum) {
41     if (cur == v.size()) {
42         if (sum > maxn) maxn = sum;
43     } else 
44     if (check(cur)) {
45         for (int i = v[cur].x;i < v[cur].y;i++) {
46             visit[i] += v[cur].value;
47         }
48         dfs(cur + 1,sum + (v[cur].y - v[cur].x) * v[cur].value);
49         for (int i = v[cur].x;i < v[cur].y;i++) {
50             visit[i] -= v[cur].value;
51         }
52         dfs(cur + 1,sum);
53     }
54     else dfs(cur + 1,sum);
55 }
56 
57 int main () {
58     // freopen("1.in","r",stdin);
59     while (cin >> MaxNum >> NumStation >> NumBook) {
60         if (MaxNum == 0) break;
61         v.clear();
62         memset(visit,0,sizeof(visit));
63         for (int i = 0;i < NumBook;i++) {
64             int x,y,value;
65             cin >> x >> y >> value;
66             v.push_back(Node(x,y,value));
67         }
68         sort(v.begin(), v.end());
69         maxn = 0;
70         dfs(0,0);
71         cout << maxn << endl;
72     }
73 }
View Code

 

UVA - 301

标签:des   style   blog   http   io   ar   color   os   sp   

原文地址:http://www.cnblogs.com/xiaoshanshan/p/4129248.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!