Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <vector>
  5. #include <string>
  6. using namespace std;
  7.  
  8. void print_vec(vector<int> &v)
  9. {
  10. for (int i = 0; i < v.size(); i++)
  11. cout << v[i] << " ";
  12. cout << endl;
  13. }
  14.  
  15. void set_up_costs(vector<vector <double> > &costs)
  16. {
  17. ifstream file;
  18. file.open("matrice.txt.txt");
  19. if (!file.is_open())
  20. {
  21. cout << "nu s-a deschis fisierul \n";
  22. return;
  23. }
  24. int N;
  25. file >> N;
  26. double cost;
  27. for (int i = 0; i < N; i++)
  28. {
  29. vector <double> cost_lin;
  30. for (int j = 0; j < N; j++)
  31. {
  32. file >> cost;
  33. cost_lin.push_back(cost);
  34. }
  35. costs.push_back(cost_lin);
  36. }
  37. file.close();
  38. }
  39.  
  40. vector<int> get_random_sol(vector<vector <double> > &costs)
  41. {
  42. vector <int> sol;
  43. vector<int> used;
  44. for (int i = 0; i < costs.size(); i++)
  45. {
  46. used.push_back(0);
  47. }
  48. for (int i = costs.size() - 1; i >= 0; i--)
  49. {
  50. int poz = (rand() + 1) % (i + 1);
  51. int nr_fol = 0;
  52. for (int j = 0; j <= poz; j++)
  53. if (used[j] == 1) nr_fol++;
  54. poz += nr_fol;
  55. while (used[poz] == 1)
  56. poz++;
  57. sol.push_back(poz);
  58. used[poz] = 1;
  59. }
  60. return sol;
  61. }
  62.  
  63. double get_sol_val(vector<int> &sol, vector<vector <double> > &costs)
  64. {
  65. int val = 0;
  66. for (int i = 0; i < sol.size() - 1; i++)
  67. val += costs[sol[i]][sol[i + 1]];
  68. return val;
  69. }
  70.  
  71. int main()
  72. {
  73.  
  74. vector <vector<double> > costs;
  75. set_up_costs(costs);
  76. int N = costs.size();
  77. cout << "matricea are " << N << " lin/col \n";
  78.  
  79. for (int i = 0; i < costs.size(); i++)
  80. {
  81. for (int j = 0; j < costs[i].size(); j++)
  82. cout << costs[i][j] << " ";
  83. cout << endl;
  84. }
  85. vector<int> sol=get_random_sol(costs);
  86. print_vec(sol);
  87. cout << get_sol_val(sol, costs);
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement