uva 301

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
using namespace std;
int station[10],max_person,orders_,station_n;
int orders[25][3],ans,max_num;
void dps(int n){
    if(n == orders_){
            if(ans>max_num){max_num = ans;}
            return;
    }
    for(int i=0;i<2;i++){
        if(i ==0 ){
            // 如果i为0,就不取
            dps(n+1);
        }
        else{
            //orders[n][0] orders[n][1]
            bool flag = true;
            for(int j = orders[n][0];j<orders[n][1];j++){
                if(station[j] - orders[n][2]<0){flag = false;}
            }
            if(flag){
                for(int j = orders[n][0];j<orders[n][1];j++){
                    station[j] -=orders[n][2];
                }
                ans += (orders[n][1]-orders[n][0])*orders[n][2];
                //cout<<"now is "<<n<<" rd cen"<<endl;
                dps(n+1);
                for(int j = orders[n][0];j<orders[n][1];j++){
                    station[j] +=orders[n][2];
                }
                ans -= (orders[n][1]-orders[n][0])*orders[n][2];
            }
        }
    }
}
int main() {
    //ifstream cin("ha.txt");
    while(cin>>max_person>>station_n>>orders_&&max_person&&station_n){
        for(int i=0;i<orders_;i++){
            cin>>orders[i][0]>>orders[i][1]>>orders[i][2];
        }
        ans = 0;
        max_num = 0;
        for(int i=0;i<=station_n+1;i++){
            station[i] = max_person;
        }
        dps(0);
        cout<<max_num<<endl;
    }
}

你可能感兴趣的:(uva 301)