Pagini recente » Cod sursa (job #454230) | Cod sursa (job #882602) | Cod sursa (job #845850) | Cod sursa (job #2469480) | Cod sursa (job #577505)
Cod sursa(job #577505)
#include<fstream>
#include<iostream>
using namespace std;
int n,W,e[1000],cost[1000],costmin[6000];
int uz[6000][1000];
void Rezolva()
{
int S,i,j,limita,nr,gata;
limita=W+1000;
for(S=1;S<=limita;S++)
costmin[S]=2000000000;
for(S=1;S<=W;S++)
{
for(i=1;i<=n;i++)
{
if(e[i]<=S && uz[S-e[i]][i]==0 && costmin[S-e[i]]!=2000000000)
{
if(costmin[S]>cost[i]+costmin[S-e[i]])
{
costmin[S]=cost[i]+costmin[S-e[i]];
nr=0;
for(j=1;j<=n;j++)
{
uz[S][j]=uz[S-e[i]][j];
if(uz[S][j]==1)
nr++;
}
uz[S][i]=1;
nr++;
uz[S][0]=nr;
}
}
}
}
ofstream fout("energii.out");
if(costmin[W]==2000000000)
{
int sum=0;
for(i=1;i<=n;i++)
sum+=e[i];
if(sum<W)
{
fout<<-1<<"\n";
fout.close();
}
else
{
gata=0;
for(S=W+1;!gata;S++)
{
for(i=1;i<=n;i++)
{
if(e[i]<=S && uz[S-e[i]][i]==0 && costmin[S-e[i]]!=2000000000)
{
if(costmin[S]>cost[i]+costmin[S-e[i]])
{
costmin[S]=cost[i]+costmin[S-e[i]];
nr=0;
for(j=1;j<=n;j++)
{
uz[S][j]=uz[S-e[i]][j];
if(uz[S][j]==1)
nr++;
}
uz[S][i]=1;
nr++;
uz[S][0]=nr;
}
}
}
if(costmin[S]!=2000000000)
gata=costmin[S];
}
fout<<gata<<"\n";
fout.close();
}
}
else
{
fout<<costmin[W]<<"\n";
fout.close();
}
}
int main()
{
int i;
ifstream fin("energii.in");
fin>>n>>W;
for(i=1;i<=n;i++)
fin>>e[i]>>cost[i];
fin.close();
Rezolva();
return 0;
}