Pagini recente » Cod sursa (job #2071212) | Cod sursa (job #3004769) | Cod sursa (job #65084) | Cod sursa (job #1543646) | Cod sursa (job #956256)
Cod sursa(job #956256)
#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;
}