Cod sursa(job #1752640)

Utilizator ducu34Albastroiu Radu Gabriel ducu34 Data 4 septembrie 2016 18:34:54
Problema Energii Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.88 kb
//
//  main.cpp
//  Energii
//
//  Created by Albastroiu Radu on 9/4/16.
//  Copyright © 2016 Albastroiu Radu. All rights reserved.
//

#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#define MULTFOARTEMULT (1<<30)-1
using namespace std;

ifstream fin("energii.in");
ofstream fout("energii.out");

int i,j,nr_of_gen, min_power, power, cost, cost_total[10001], Min_cost;
// first power, second cost;
vector<pair<int,int>> powers;

int main()
{
    fin>>nr_of_gen>>min_power;
    
    for(i=1;i<=nr_of_gen;i++)
    {
        fin>>power>>cost;
        
        for(auto &el : powers)
        {
            if( cost_total[el.first + power] == 0 )
                cost_total[el.first + power] = MULTFOARTEMULT;
                
            if( cost_total[el.first + power] > el.second + cost )
            {
                cost_total[el.first + power] = el.second + cost;
                    
                if(el.first + power < min_power)
                    powers.push_back(make_pair(el.first + power, el.second + cost));
            }
            
            if( cost_total[el.first + power] == MULTFOARTEMULT )
                cost_total[el.first + power] = 0;
        }
        
        if( cost_total[power] == 0 )
            cost_total[power] = MULTFOARTEMULT;
        
        
        if( cost_total[power] > cost )
        {
            cost_total[power] = cost;
            
            if(power < min_power)
                powers.push_back(make_pair(power, cost));
        }
        
        if( cost_total[power] == MULTFOARTEMULT )
            cost_total[power] = 0;

        
    }
    
    Min_cost = MULTFOARTEMULT-1;
    
    for(i=min_power;i<=10000;i++)
    {
        if(cost_total[i])
        {
            Min_cost = min(Min_cost, cost_total[i]);
        }
    }
    
    if(Min_cost!=MULTFOARTEMULT-1)
        fout<<Min_cost;
    else
        fout<<-1;
    
    return 0;
}