uva 301 Transportation

来源:互联网 发布:java断点续传上传框架 编辑:程序博客网 时间:2024/06/05 16:37

原题:
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 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

中文:
首先给你3个数,表示这辆车的容量,车站数和订单数。
然后给你下面一堆订单,每个订单包含3个数,分别是起始站点,目的地站点,乘客数。
每个订单要取,要么不取,在保证车上人数不超过上限的情况下,最多能赚多少钱?每个乘客坐1站1元钱。

#include<bits/stdc++.h>using namespace std;typedef long long ll;//fstream in,out;struct node{    int s,e,num;};node no[26];int mark[100];int cap,stat,ord,ans;void update(int x){    for(int i=no[x].s;i<no[x].e;i++)        mark[i]+=no[x].num;}void recall(int x){    for(int i=no[x].s;i<no[x].e;i++)        mark[i]-=no[x].num;}bool judge(int x){    for(int i=no[x].s;i<no[x].e;i++)    {        if(mark[i]+no[x].num>cap)            return false;    }    return true;}void dfs(int x){    if(x==ord)    {        int tmp=accumulate(mark,mark+stat+1,0);        ans=max(ans,tmp);    }    else    {        if(judge(x))        {            update(x);            dfs(x+1);            recall(x);        }        dfs(x+1);    }}int main(){    ios::sync_with_stdio(false);    while(cin>>cap>>stat>>ord)    {        if(cap+stat+ord==0)            return 0;        ans=0;        memset(mark,0,sizeof(mark));        for(int i=0;i<ord;i++)            cin>>no[i].s>>no[i].e>>no[i].num;        dfs(0);        cout<<ans<<endl;    }    return 0;}

解答:
直接用暴力dfs即可,由于车站数最多只有7个,所以每次更新的时候不需要使用区间数据结构,直接更新即可。

0 0
原创粉丝点击