Pagini recente » Cod sursa (job #86376) | Cod sursa (job #3266931) | Cod sursa (job #1897087) | Cod sursa (job #2387533) | Cod sursa (job #950444)
Cod sursa(job #950444)
#include<fstream>
using namespace std;
#define StatesMax (1<<NMax)
#define NMax (17+1)
int BestWeight[StatesMax],Loads[NMax],N,WeightMax,States;
fstream 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;
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;
}