Transportation

Transportation
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3090   Accepted: 1283

Description

Ruratania is just entering capitalism and is establishing new enterprising activities in many fields in- cluding 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 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

Source

Central Europe 1995

//Source Code:
//Memory: 164K		Time: 407MS
//Language: C++		Result: Accepted

#include <iostream>
#include <cstdio>
#include <memory.h>
#include <algorithm>
using namespace std;

struct Order
{
    int s;
    int e;
    int p;
}ord[23];
int n, NumB, NumOrd;
int MaxEarn, CurEarn, off[8];//off记录每一站的应下车人数

int cmp (const void *aa, const void *bb)
{
    struct Order *a=(Order*) aa;
    struct Order *b=(Order*) bb;
    if(a -> s != b -> s) return a -> s - b -> s;
    else return a -> e - a -> e;
}

//参数num是order的编号,tolp是车上总人数
void DFS (int num,int tolp)
{
    //order由零开始编号,故此时已搜索所有order,回溯
    if (num == NumOrd)
    {
        if (MaxEarn < CurEarn) MaxEarn = CurEarn;
        return ;
    }
    else
    {
        //减去下车人数,注意无论当前order是否accepted,都要执行
        if (num > 0)
            for (int i = ord[num - 1].s + 1; i <= ord[num].s; i++)
            {
                tolp -= off[i];
            }
        //加上当前order的上车人数不超载就搜索下一张order
        tolp += ord[num].p;
        if (tolp <= n)
        {
            CurEarn += (ord[num].e - ord[num].s) * ord[num].p;
            off[ord[num].e] += ord[num].p;
            DFS(num + 1, tolp);
            //以下两条指令必须在当前的if中,因为上面的“CurEarn+= ...”和“off+=”
            //是满足if 的条件时才执行的。
            CurEarn -= (ord[num].e - ord[num].s) * ord[num].p;
            off[ord[num].e] -= ord[num].p;
        }
        tolp -= ord[num].p; //还原现场
        DFS (num + 1, tolp);
    }
    return ;
}

int main()
{
    while (scanf("%d %d %d", &n, &NumB, &NumOrd) && (n || NumB || NumOrd))
    {
        memset(off, 0, sizeof(off));
        MaxEarn = 0; CurEarn = 0;
        for (int i = 0; i < NumOrd; i++)
        {
            scanf("%d %d %d", &ord[i].s, &ord[i].e, &ord[i].p);
        }
        qsort (ord, NumOrd, sizeof(ord[0]), cmp);
        DFS(0,0);
        printf("%d\n", MaxEarn);
    }
    return 0;
}


你可能感兴趣的:(struct,express,input,each,output,Numbers)