uva-301 - Transportation

来源:互联网 发布:口算生成器 软件下载 编辑:程序博客网 时间:2024/05/29 13:34



 Transportation 

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

简单的回溯题,用每个order订单是否accept作为状态搜索。

刚开始用了个错误的方法判断是否超过列车容量导致一直挖 --- 直接保存已接受的订单,把当前订单的人数加上(已接受的)和当前订单有交集的订单人数,然后判断是否超过容量 --- 不能这么算,应该算的是交集最多的地方是否超过容量。。。。

改了个判断方法就ac了,用int status[]保存每个车站已经接受的人数,判断每个车站再加上当前订单的人数是否超过容量来决定能否接受。

#include <iostream>using namespace std;struct Order{    int s;    int e;    int num;};int n;   //容量int stations; //车站数int numOrder; //订单数int ans;Order allOrder[22];  //保存所有的orderint status[8];     //保存车站人数状态void search(int cur, int earns){    if(cur == numOrder){              ans = max(earns, ans);  //更新最大值        return;    }    search(cur+1, earns);    int accept = 1;    for(int i = allOrder[cur].s; i < allOrder[cur].e; i++) //判断加上当前order的人数后是否超过容量     if( status[i]+allOrder[cur].num > n){                      accept = 0;        break;    }    if(accept){        for(int i = allOrder[cur].s; i < allOrder[cur].e; i++)  //更新车站人数        status[i] += allOrder[cur].num;                                   search(cur+1, earns+allOrder[cur].num*(allOrder[cur].e - allOrder[cur].s));        for(int i = allOrder[cur].s; i < allOrder[cur].e; i++)   //还原状态,返回递归上一层        status[i] -= allOrder[cur].num;    }}int main(){    while(cin >> n >> stations >> numOrder){        if(!n && !stations && !numOrder) break;        for(int i = 0; i < numOrder; i++)            cin >> allOrder[i].s >> allOrder[i].e >> allOrder[i].num;        ans = 0;        for(int i = 0; i <= stations; i++) status[i] = 0;        search(0, 0);        cout << ans << endl;    }    return 0;}





 Transportation 

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
0 0
原创粉丝点击