UVA - 301 Transportation
来源:互联网 发布:算法相同软件著作权 编辑:程序博客网 时间:2024/05/16 01:39
题目大意: 有一辆车从A城市开往B城市,途中有m个站,车上最多的载客人数为n人,每一个站的价格就是终点和起点的差值,现在有k分订单,要求找到这辆车的最大利润。
解题思路:递归订单, 分为两种情况讨论,能接和不能接,判断后在进行递归
#include<cstdio>struct order {int s;int e;int p;};//存储起点,终点,乘客数量order o[30];//记录有多少订单int ans;//记录最终答案int cnt[30];//记录每站的人int cap, num, ord;bool judge() {for(int i = 0; i < num ; i++)if(cnt[i] > cap) return false;return true;}void dfs(int d, int sum) {if(sum > ans) ans = sum;if(d >= ord)return ;//循环终止条件,等于订单数量或大于时for(int i = o[d].s; i < o[d].e; i++) cnt[i] = cnt[i] + o[d].p;//先假设可以接收订单if(judge()) {//判断能否接收,能接收的话,就继续递归下去int temp = o[d].p*(o[d].e-o[d].s)+sum;dfs(d+1,temp);}for(int i = o[d].s; i < o[d].e; i++)cnt[i] = cnt[i] - o[d].p;//不能接收的情况,和回溯的情况dfs(d+1,sum);}int main() {while(scanf("%d%d%d", &cap, &num, &ord) != EOF && (cap || num || ord)) {for(int i = 0; i < num; i++)cnt[i] = 0;for(int i = 0; i < ord; i++) {scanf("%d%d%d",&(o[i].s), &(o[i].e), &(o[i].p));}if(!cap || !num || !ord) {printf("0\n");continue;}ans = 0;dfs(0,0);printf("%d\n",ans);}return 0;}
0 0
- UVa 301 - Transportation
- uva 301 - Transportation
- uva 301 - Transportation
- UVa 301 - Transportation
- Uva 301 Transportation
- UVa 301 - Transportation 回溯
- uva 301 Transportation
- UVa 301 - Transportation
- UVA 301 Transportation
- UVA 301 - Transportation
- uva 301 Transportation (DFS)
- uva 301 Transportation
- uva 301 Transportation
- UVa:301 Transportation
- UVa 301 - Transportation
- uva 301 - Transportation
- UVa 301 Transportation
- ( 回溯 )uva 301 - Transportation
- js php 数组比较
- elasticsearch与mongodb分布式集群环境下数据同步
- 记录一下
- shell002
- Doing Homework again HDU 1789 贪心?DP
- UVA - 301 Transportation
- Block的循环引用
- 黑马程序员——集合类(二)
- 服务器 数据库设计技巧
- Macmini 2012Late硬盘异响和Mac下设置apm
- Ubuntu14.04使用su切换到root时提示:Authentication failure
- HTTP Client 的类
- Intent匹配规则以及解析框架深入分析
- 小平台编程