uva 301 Transportation

来源:互联网 发布:unity3d特效 编辑:程序博客网 时间:2024/04/27 13:24

回溯法加子集枚举

#include <stdio.h>struct node{int start;int end;int num;};struct node all_order[100];struct node cur_order[100];int max_profit;int passenger[20];void dfs(int cur, int last_profit, int capacity, int max_pos, int last_index){int i, j;bool f;int cur_profit;if(cur > max_pos)return;for(i=last_index+1; i<=max_pos; i++){f = true;for(j=all_order[i].start; j<all_order[i].end; j++){if(passenger[j]+all_order[i].num > capacity){f = false;break;}}if(f){cur_order[cur].start = all_order[i].start;cur_order[cur].end = all_order[i].end;cur_order[cur].num = all_order[i].num;for(j=all_order[i].start; j<all_order[i].end; j++)passenger[j] += all_order[i].num;cur_profit = last_profit + all_order[i].num*(all_order[i].end-all_order[i].start);if(max_profit < cur_profit)max_profit = cur_profit;dfs(cur+1, cur_profit, capacity, max_pos, i);for(j=all_order[i].start; j<all_order[i].end; j++)passenger[j] -= all_order[i].num;} }}void func(int capacity, int last_station, int order_num){max_profit = 0;for(int i=0; i<=last_station; i++) {passenger[i] = 0; }dfs(1, 0, capacity, order_num, 0);printf("%d\n", max_profit);}int main(void){int capacity, last_station, order_num;int i;int start, end, num;//freopen("input.dat", "r", stdin);while(1){scanf("%d %d %d", &capacity, &last_station, &order_num);if(!capacity && !last_station && !order_num)break;for(i=1; i<=order_num; i++){scanf("%d %d %d", &start, &end, &num);all_order[i].start = start;all_order[i].end = end;all_order[i].num = num;}func(capacity, last_station, order_num);}}