Cod sursa(job #355061)

Utilizator daniel.dumitranDaniel Dumitran daniel.dumitran Data 10 octombrie 2009 09:32:14
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
#include <stdio.h>

#define MAXG 1000
#define MAXW 5000
#define MAXE 10000

#define FISIN     "energii.in"
#define FISOUT    "energii.out"

#define INF       (MAXE * MAXG + 1)

FILE *fin, *fout;

int nr_g, w;
int cost[MAXG], pow[MAXG];
int d[MAXG + MAXE + 1];

int main() {
  fin = fopen(FISIN, "rt");
  fout = fopen(FISOUT, "wt");

  fscanf(fin, "%d %d", & nr_g, &w);
  for (int i = 0; i < nr_g; ++i)
    fscanf(fin, "%d %d", pow + i, cost + i);

  for (int i = 0; i <= MAXG + MAXE; ++i) d[i] = INF;
  d[0] = 0;
  int max = 0, best = -1;

  for (int i = 0; i < nr_g; ++i)
    for (int j = max; j >= 0; --j) {
      if (d[j] == INF) continue;
      int new_pow = j + pow[i];
      int new_cost = d[j] + cost[i];
      if (new_cost < d[new_pow]) {
        d[new_pow] = new_cost;
        if (new_pow >= w && ((new_cost < best) || (best == -1)))
          best = new_cost;
        if (new_pow <= w && new_pow > max) max = new_pow;
      }
    }

  fprintf(fout, "%d\n", best);

  fclose(fout);
  fclose(fin);
  return 0;
}