Pagini recente » Cod sursa (job #772694) | Cod sursa (job #671731) | Cod sursa (job #2794851) | Cod sursa (job #577847) | Cod sursa (job #598864)
Cod sursa(job #598864)
#include <fstream>
#include <cstring>
using namespace std;
int n, w, cost[5010], gen[1010][3];
bool fol[5010][1010];
int main()
{
int i, j;
ifstream f("energii.in");
ofstream g("energii.out");
f >> n >> w;
for(i = 1; i <= n; ++i)
f >> gen[i][1] >> gen[i][2];
memset(cost, -1, sizeof(cost));
cost[0] = 0;
for(i = 1; i <= w; ++i)
for(j = 1; j <= n; ++j)
if(gen[j][1] <= i && cost[i - gen[j][1]] != -1 && !fol[i - gen[j][1]][j] && ((cost[i - gen[j][1]] + gen[j][2] < cost[i]) || cost[i] == -1))
{
memcpy(fol[i], fol[i - gen[j][1]], sizeof(fol[i]));
fol[i][j] = 1;
cost[i] = cost[i - gen[j][1]] + gen[j][2];
}
g << cost[w] << '\n';
g.close();
return 0;
}