Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problema 1 #20

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions submissions/tomazgomes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Requerimentos

- GNU's `g++`

# Como executar a solução (apenas Linux)

Compile o programa com `g++ problema-1.cpp -O2 -o problema-1`. Em seguida, execute o binário com `./problema-1 <caminho-para-arquivo-de-entrada>`. A resposta para a instância especificada será imprimida na saída padrão.
45 changes: 45 additions & 0 deletions submissions/tomazgomes/problema-1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <iostream>
#include <fstream>

const int NMAX = 1024;
const int WMAX = 3000;

int w[NMAX], v[NMAX];
int n, W;
int dp[NMAX][WMAX];

int main(int argc, char * argv[])
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);

if (argc != 2) {
std::cout << "[ERRO]: Esse programa requer exatamente 1 argumento - o caminho para o arquivo de entrada.\n";
std::exit(1);
}

std::ifstream fs(argv[1]);
fs >> n >> W;

for (int i = 0; i < n; ++i) {
fs >> w[i] >> v[i];
}

for (int weight = 0; weight <= W; weight++) {
if (weight >= w[0])
dp[0][weight] = v[0];
else
dp[0][weight] = 0;
}
for (int i = 1; i < n; i++) {
for (int weight = 0; weight <= W; weight++) {
if (weight >= w[i])
dp[i][weight] = std::max(dp[i - 1][weight - w[i]] + v[i], dp[i - 1][weight]);
else
dp[i][weight] = dp[i - 1][weight];
}
}

std::cout << dp[n - 1][W] << "\n";
return 0;
}