UVA_301_Transportation
2015-08-09 23:22
337 查看
Transportation |
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
这个题目其实直接dfs就可以了
因为请求最多才22
2^22即可
就是注意回溯的时候把该改的东西改全
#include <iostream> #include <stdio.h> using namespace std; const int M=25; struct ORD { int st,ed,nu; //始发站,终点站,人数 int mo; //收益 }ord[M]; int tra[10]; int tot; void dfs(int t,int now,int n,int cap) { if(now==n) { //cout<<t<<endl; tot=max(tot,t); return; } dfs(t,now+1,n,cap); int f=1; for(int i=ord[now].st+1;i<=ord[now].ed;i++) { if(tra[i]+ord[now].nu>cap) //当前状态旅客已经装不下了 { f=0; break; } } if(f) { for(int i=ord[now].st+1;i<=ord[now].ed;i++) tra[i]+=ord[now].nu; dfs(t+ord[now].mo,now+1,n,cap); for(int i=ord[now].st+1;i<=ord[now].ed;i++) tra[i]-=ord[now].nu; } } int main() { int cap,stb,od; while(1) { tot=0; scanf("%d%d%d",&cap,&stb,&od); if(!cap&&!stb&&!od) break; for(int i=0;i<od;i++) { scanf("%d%d%d",&ord[i].st,&ord[i].ed,&ord[i].nu); ord[i].mo=(ord[i].ed-ord[i].st)*ord[i].nu; } dfs(0,0,od,cap); printf("%d\n",tot); } return 0; }
相关文章推荐
- 【Android开源项目分析】TAB导航栏PagerSlidingTabStrip的使用和源码分析
- 智渔课堂官方免费教程六:Java基础之流程控制
- H3C FTP协议
- android动画(Animation)
- vs+mysql+ef配置方法
- 关于iOS多线程,你看我就够了(已更新)
- WebRTC VideoEngine超详细教程(一)——视频通话的基本流程
- Bresenham算法画填充圆及SDL代码实现
- I学霸官方免费教程六:Java基础之流程控制
- PDO实现学生管理系统
- maven 安装使用示例
- 贪心算法之Radar Installation
- HDU 4287 Intelligent IME(哈希)
- UVA 12526 Cellphone Typing (字典树)
- 修仙之路启程
- 使用laypage分页
- Maven入门教程
- PHP生成静态文件
- C语言函数参数压栈顺序为何是从右到左?
- Eclipse 配置格式