Pagini recente » Cod sursa (job #2731329) | Cod sursa (job #306667) | Cod sursa (job #388147) | Cod sursa (job #2399366) | Cod sursa (job #108009)
Cod sursa(job #108009)
#include <stdio.h>
#include <stdlib.h>
const int maxsize = 10001;
int ee[maxsize];
int cg[maxsize];
int a[maxsize];
int poz[maxsize];
bool in(int k, int s)
{
if(s <= 0 )
return false;
int m = s;
int ss = poz[m];
while(ss != -1)
{
if(ss == k)
return true;
m = m-ee[poz[m]];
ss = poz[m];
}
return false;
}
int main(void)
{
FILE *fin;
FILE* fout;
fin = fopen("energii.in", "r");
fout = fopen("energii.out", "w");
int g;
int w;
fscanf(fin, "%d\n", &g);
fscanf(fin, "%d\n", &w);
int i=0;
int total = 0;
for(;i<g;++i)
{
int ee1, cg1;
fscanf(fin, "%d %d\n", &ee1, &cg1);
ee[i] = ee1;
cg[i] = cg1;
total += ee1;
}
fclose(fin);
bool found = false;
a[0] = 0;
for(int k=0;k<g;++k)
{
if(cg[k] == 0)
a[0] += ee[k];
}
if(a[0] >= w)
{
fprintf(fout, "0\n");
fclose(fout);
return 0;
}
if(total < w)
{
fprintf(fout, "-1\n");
fclose(fout);
return 0;
}
a[0] = 0;
poz[0] = -1;
int min = 1001*10002;
for(i=1;i<=maxsize;++i)
{
int pozm = -1;
a[i] = 1001*10002;
for(int k=0;k<g;++k)
{
if(ee[k] <= i && !in(k, i-ee[k]))
{
if(cg[k] + a[i-ee[k]] < a[i])
{
a[i] = cg[k] + a[i-ee[k]];
pozm = k;
}
}
}
poz[i] = pozm;
if(i >= w && min > a[i])
min = a[i];
}
for(int k=0;k<g;++k)
{
if(ee[k] > w && ee[k] < 100002)
{
if(cg[k] < min)
min = cg[k];
}
}
fprintf(fout, "%d\n", min);
fclose(fout);
return 0;
}