-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOutput_Mochila.cpp
159 lines (108 loc) · 4.1 KB
/
Output_Mochila.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
Trabalho da disciplina de Projeto e analise de algoritmos 2023/1
Alunos:
Bruno Belo Comachio,
Luccas Souza Di Oliveira,
Matheus Mello De Azevedo.
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
#include <string.h>
#include <cstdlib>
#include <time.h>
#include <chrono>
#include <iomanip>
#define INF INT_MAX/2
using namespace std;
int memo[10001][10001];
int n;
int mochila(int i, int c,vector<vector<int>>& l){
if(c < 0) return -INF; //se chegar em capacidade negativa na mochila
if(i == n) return 0; //se chegar no ultimo item
if(memo[i][c]!=-1) return memo[i][c]; //tem no memo
//não pegar o item
int r1 = mochila(i+1, c, l);
//pegar o item
int r2 = l[i][2] + mochila(i+1, c-l[i][1], l);
return memo[i][c] = max(r1,r2);
}
bool customComparador_Valor(const vector<int>& a, const vector<int>& b){
return a[2] < b[2];
}
bool customComparador_Peso(const vector<int>& a, const vector<int>& b){
return a[1] < b[1];
}
int porValor(int c, vector<vector<int>>& l){
sort(l.begin(),l.end(), customComparador_Valor);
int lucro = 0;
for(int i = n-1; i >= 0; i--){
if(l[i][1] <= c){
lucro += l[i][2];
c -= l[i][1];
}
}
return lucro;
}
int porPeso(int c, vector<vector<int>>& l){
sort(l.begin(),l.end(), customComparador_Peso);
int lucro = 0;
for(int i = 0; i < n; i++){
if(l[i][1] <=c)
{
lucro += l[i][2];
c -= l[i][1];
}
}
return lucro;
}
float desempenho(int re, int ro){
float valor = (100.0f * ((re - static_cast<float>(ro))/re));
return valor;
}
int main(){
memset(memo, -1, sizeof memo);
int N, C; //Numero de itens, Capacidade Mochila
cin >> N >> C;
//Lista com todos os itens: [ID, Peso, Valor]
vector<vector<int>> item_list(N, vector<int>(3));
n = N;
//Lendo todos os itens
for(int i = 0; i < N; i++) cin >> item_list[i][0] >> item_list[i][1] >> item_list[i][2];
//Começando a medir o tempo para a solução
//Usando Programação dinamica
auto t1_comeco = chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(false);
int max_profit = mochila(0, C, item_list);
auto t1_fim = chrono::high_resolution_clock::now();
//Calculando o valor, do fim da execução menos o começo
//assim obtemos o tempo necessario para a execução
auto t1_ms = chrono::duration_cast<chrono::milliseconds>(t1_fim - t1_comeco);
double t1_seg = chrono::duration_cast<chrono::nanoseconds>(t1_fim - t1_comeco).count();
t1_seg *= 1e-9;
//Começando a medir o tempo para a solução
//Usando uma Heuristica Gulosa colocando sempre o item de maior valor
auto t2_comeco = chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(false);
int porValor_profit = porValor(C, item_list);
auto t2_fim = chrono::high_resolution_clock::now();
auto t2_ms = chrono::duration_cast<chrono::milliseconds>(t2_fim - t2_comeco);
double t2_seg = chrono::duration_cast<chrono::nanoseconds>(t2_fim - t2_comeco).count();
t2_seg *= 1e-9;
//Começando a medir o tempo para a solução
//Usando uma outra Heuristica Gulosa colocando sempre o item de menor peso
auto t3_comeco = chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(false);
int porPeso_profit = porPeso(C, item_list);
auto t3_fim = chrono::high_resolution_clock::now();
auto t3_ms = chrono::duration_cast<chrono::milliseconds>(t3_fim - t3_comeco);
double t3_seg = chrono::duration_cast<chrono::nanoseconds>(t3_fim - t3_comeco).count();
t3_seg *= 1e-9;
float valor1 = desempenho(max_profit,porValor_profit);
float valor2 = desempenho(max_profit,porPeso_profit);
cout << t1_seg << ';' << max_profit << ';';
cout << t2_seg << ';' << porValor_profit << ';' << fixed << setprecision(2) << valor1 << ';';
cout << fixed << setprecision(6)<< t3_seg << ';' << porPeso_profit << ';' << fixed << setprecision(2) << valor2 << endl;
return 0;
}