Cod sursa(job #956256)

Utilizator otto1Palaga Vicentiu-Octavian otto1 Data 2 iunie 2013 17:28:01
Problema Zebughil Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.31 kb
#include<fstream>
using namespace std;
#define StatesMax (1<<NMax)
#define NMax (17+1)

int BestWeight[StatesMax],Loads[NMax],N,WeightMax,States;

ifstream in("zebughil.in");
ofstream out("zebughil.out");

int solve()
{ int currentTruck,currentLoad,currentState;
    for(currentState=1;currentState<States;currentState++)
            BestWeight[currentState]=-1;
    BestWeight[0]=WeightMax;


    for(currentTruck=1;currentTruck<=N;currentTruck++)
    {
      for(currentState=1;currentState<States;currentState++)

        if(BestWeight[currentState]!=-1) BestWeight[currentState]=WeightMax;
        else
        for(currentLoad=0;(1<<currentLoad)<=currentState; currentLoad++)
         if((currentState&(1<<currentLoad))&&BestWeight[currentState&(~(1<<currentLoad))]-Loads[currentLoad]>BestWeight[currentState])
         BestWeight[currentState]=BestWeight[currentState&(~(1<<currentLoad))]-Loads[currentLoad];


    if(BestWeight[States-1]!=-1) return currentTruck;


    }
}

void read()
{
     in>>N>>WeightMax;

     States=(1<<N);

     for(int i=0;i<N;i++)
     in>>Loads[i];

     }

int main()
{
 for(int i=1;i<=3;i++)
         {
         read();
         out<<solve()<<'\n';
                     }


         in.close();
         out.close();

         return 0;
    }