UVA - 301 - Transportation (暴力回溯)
来源:互联网 发布:地方财政支出数据 编辑:程序博客网 时间:2024/06/06 08:48
UVA - 301
Description
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 40 2 11 3 51 2 72 3 1010 5 43 5 102 4 90 2 52 5 80 0 0
Sample Output
1934
Source
Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 3. Brute Force :: Backtracking - Easy
思路:这题先给出三个数,分别代表火车的容量,有几个站和有几个订单,根据订单的取舍来决定最大利益
用dfs递归求解,枚举每一个订单取或不取的情况来求得最大值
AC代码:
#include <cstdio>#include <cstring>#include <algorithm> using namespace std;const int maxn = 30;struct node {int u, v, peo;}a[maxn];int A[maxn];//每个站最大的容量,这题里的容量也可以看做earning,递归结束时可以以之和表earning int ans;int cap, B, n;void dfs(int cur) {if(cur == n) {int sum = 0;for(int i = 0; i < B; i++) {sum += A[i];}ans = max(ans, sum);return;}int ok = 1;for(int i = a[cur].u; i < a[cur].v; i++)if(A[i] + a[cur].peo > cap) {ok = 0; break;}if(ok) {//判断可不可以接受第cur个订单 for(int i = a[cur].u; i < a[cur].v; i++) A[i] += a[cur].peo;dfs(cur + 1);//接受第cur个订单,然后往后走 for(int i = a[cur].u; i < a[cur].v; i++) A[i] -= a[cur].peo;}dfs(cur + 1);//不接受第cur个订单,然后往后走 }int main() {while(scanf("%d %d %d", &cap, &B, &n), cap || B || n) {ans = 0;memset(A, 0, sizeof(A));for(int i = 0; i < n; i++) {scanf("%d %d %d", &a[i].u, &a[i].v, &a[i].peo);}dfs(0);printf("%d\n", ans);}return 0;}
- UVA - 301 - Transportation (暴力回溯)
- UVA - 301 Transportation (回溯)
- uva 301 Transportation(回溯)
- uva 301 Transportation(回溯)
- UVa 301 - Transportation 回溯
- ( 回溯 )uva 301 - Transportation
- Transportation-301uva 回溯
- UVA 301 POJ 1040- Transportation DFS 回溯
- Transportation uva+递归+回溯
- uva 301 - Transportation 一切都是那么暴力
- UVA 301 Transportation 。。有点需要思考的回溯、、
- uva 301 Transportation 铁路公司的阳谋 纯dfs暴力
- uva 11218 KTV 暴力(回溯)
- UVa 301 - Transportation
- uva 301 - Transportation
- uva 301 - Transportation
- UVa 301 - Transportation
- Uva 301 Transportation
- Bitmap BitmapImage
- 如何清除xcode里面的mobileprovision文件
- JSTL标签 参考手册
- 原始套接字接收是多了4个字节,是怎么回事?
- iOS —— UITextView实现Placehodler属性
- UVA - 301 - Transportation (暴力回溯)
- 莫比乌斯小结
- Silverlight技术之Style和StaticResource
- WebViewJavascriptBridge-->实现JS和iOS之间的互调
- 【Android】数据库 sqLite
- 取消webView的弹窗
- HBase1.0.0版源码分析之HMaster启动代码分析(2)
- Java byte[] 转 String 陷阱
- 第三周 课后实践:项目三——程序的多文件组织