Cod sursa(job #457405)

Utilizator danni_1107Sengher Daniel danni_1107 Data 19 mai 2010 16:24:33
Problema Energii Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.66 kb
#include <fstream.h> 
ifstream f("energii.in"); 
ofstream h("energii.out"); 
struct generator{int e; 
          
int c; } g[1010]; 
int c[5010],i,n,w,j,wi,ci,wmax,cc; 
const int inf=5010*1010; 
int main(){ 
f>>n>>w;     wmax=0; 
for(i=1;i<=w;i++) 
c[i]=inf; 
for(i=1;i<=n;i++) 
f>>g[i].e>>g[i].c; 
c[0]=0; 
for(i=1;i<=n;i++) 
{for(j=wmax;j>=0;j--) 
   
if(c[j]!=inf)       
{wi=j+g[i].e;       
cc=c[j]+g[j].c;        
if(wi>w)       
wi=w;       
if(wmax<wi)       
wmax=wi;       
if(c[wi]>cc)       
c[wi]=cc;       
}       
}      
if(c[w]!=inf) h<<c[w];       
else h<<-1;       
f.close();       
h.close();       
return 0; 
      
}