poj-1040-Transportation
来源:互联网 发布:中兴软件开发技术面 编辑:程序博客网 时间:2024/05/22 10:24
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.
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 40 2 11 3 51 2 72 3 1010 5 43 5 102 4 90 2 52 5 80 0 0
Sample Output
1934
首先将订单排好序,然后dfs去找,在dfs的途中,要记录下,每一站下车的人数。
在dfs中,总共只有两种状态,要么接收dfs(i + 1, p + order[i].p, money + order[i].p * (order[i].e - order[i].s));要么不接收dfs(i + 1, p, money);
然后算钱就好。
#include <cstdio>#include <algorithm>#include <iostream>#include <cstring>using namespace std;const int maxm = 23;const int maxn = 8;int n,m,t;int ans;int down[maxn];struct Order{ int s,e,p; bool operator < (const Order &a) const{ if(a.s == s) return e < a.e; return s < a.s; }}order[maxm];void dfs(int i, int p, int money){ if(i == t){ if(ans < money) ans = money; return; } if(i > 0) for (int j = order[i - 1].s + 1;j<=order[i].s; j++) p -= down[j]; if(p + order[i].p <= n){ down[order[i].e] += order[i].p; dfs(i + 1, p + order[i].p, money + order[i].p * (order[i].e - order[i].s)); down[order[i].e] -= order[i].p; } dfs(i + 1, p, money);}int main(){ while(scanf("%d %d %d",& n,& m,& t),t || n || m){ for(int i = 0; i < t; i++) scanf("%d%d%d",& order[i].s,& order[i].e,& order[i].p); sort(order,order+t); ans = 0; memset(down,0,sizeof(down)); dfs(0,0,0); printf("%d\n",ans); } return 0;}
0 0
- poj 1040 Transportation
- poj 1040 Transportation
- POJ-1040-Transportation
- Transportation-POJ 1040
- POJ 1040 Transportation DFS
- POJ 1040 Transportation
- poj-1040-Transportation
- POJ 1040 Transportation 笔记
- poj 1040 Transportation DFS剪枝
- UVA 301 POJ 1040- Transportation DFS 回溯
- UVa 301 & HDU 1456 & POJ 1040 - Transportation
- poj 1040 Transportation 深搜剪枝
- POJ 1797 Heavy Transportation
- POJ 1797 Heavy Transportation
- poj 1797Heavy Transportation
- poj 3228 Gold Transportation
- poj 1797 Heavy Transportation
- POJ 1797 Heavy Transportation
- php array访问
- 刷新加载
- 2016SDAU课程练习三1015
- 赋值/关系/逻辑运算以及条件语句
- C#冒泡排序实例1
- poj-1040-Transportation
- Android中的四大数据存储方式解析
- http://blog.sina.com.cn/rss/2790628754.xml
- VS2013下安装&编译Windows Caffe
- IDEA使用教程
- ListView分页加载
- DP与回文串
- PAT (Basic Level) Practise (中文)1049. 数列的片段和(20)
- ZedBoard上移植OpenCV