Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. vector<Cidade> Knapsackproblem(Viagem v, Path p, int tempoMax){
  2.  
  3. vector<Cidade> res;
  4.  
  5. p.getGraph().setA(new int *[v.getCidades().size()+1]);
  6.  
  7. for(size_t i=0; i < v.getCidades().size()+1; ++i){
  8. p.getGraph().getA()[i] = new int [tempoMax+1];
  9. }
  10.  
  11. for(size_t i=0; i < v.getCidades().size()+1; ++i){
  12. for(size_t j=0; j < tempoMax +1; ++j){
  13. p.getGraph().getA()[i][j] = 0;
  14. }
  15. }
  16.  
  17. int iMax = 0;
  18. int jMax = 0;
  19. vector<pair<int,int> > classificacoes;
  20.  
  21. for(size_t i =0; i < v.getCidades().size()+1; ++i){
  22. for(size_t j=0; j < tempoMax+1; ++j){
  23. if(i==0){
  24. p.getGraph().getA()[i][j] = 0;
  25. }
  26. else{
  27. if(p.getGraph().getVertexSet().at(i-1)->getInfo().getTempo() <= j){
  28. int dif_tempos = j - p.getGraph().getVertexSet().at(i-1)->getInfo().getTempo();
  29. if( p.getGraph().getVertexSet().at(i-1)->getInfo().getClassificacao() + p.getGraph().getA()[i-1][dif_tempos] > p.getGraph().getA()[i-1][j]){
  30. p.getGraph().getA()[i][j] = p.getGraph().getVertexSet().at(i-1)->getInfo().getClassificacao() + p.getGraph().getA()[i-1][dif_tempos];
  31. }
  32. else {
  33. p.getGraph().getA()[i][j] = p.getGraph().getA()[i-1][j];
  34. }
  35. }
  36. else
  37. {
  38. p.getGraph().getA()[i][j] = p.getGraph().getA()[i-1][j];
  39. }
  40. }
  41. pair<int,int> p;
  42. p.first = i;
  43. p.second = j;
  44. classificacoes.push_back(p);
  45. }
  46. }
  47.  
  48. //encontrar o maximo das classificações
  49.  
  50. sort(classificacoes.begin(), classificacoes.end());
  51.  
  52. iMax = classificacoes.at(classificacoes.size() -1).first;
  53. jMax = classificacoes.at(classificacoes.size() -1).second;
  54.  
  55.  
  56. //encontrar as cidades a serem usadas.
  57.  
  58. size_t j=jMax;
  59. size_t i=iMax;
  60.  
  61. while(i>0 && j>0){
  62. if(p.getGraph().getA()[i][j] != p.getGraph().getA()[i-1][j]){
  63. res.push_back(p.getGraph().getVertexSet().at(i-1)->getInfo());
  64. j -= p.getGraph().getVertexSet().at(i-1)->getInfo().getTempo();
  65. i--;
  66. }
  67. else i--;
  68. }
  69.  
  70.  
  71. return res;
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement