Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <climits>
  5. #include <queue>
  6. #include <cmath>
  7. #define N 26
  8. using namespace std;
  9. typedef struct Priority{
  10. vector<int> resultat;
  11. double prior;
  12. double var_evristic;
  13. } Priority;
  14.  
  15. bool operator < (const Priority &com_ver_1, const Priority &com_ver_2){
  16. return com_ver_1.prior > com_ver_2.prior;
  17. }
  18.  
  19. class Graf{
  20. private:
  21. double **graf;
  22. int d;
  23. vector<int> str;
  24. double weight_from[N];
  25. public:
  26. Graf(int i, int start): d(i){
  27. str.push_back(start);
  28. graf = new double *[d];
  29. for(int i = 0; i < d; i++)
  30. graf[i] = new double[d];
  31. for(int i = 0; i < d; i++){
  32. for(int j = 0; j < d; j++)
  33. graf[i][j] = 0;
  34. }
  35. }
  36. ~Graf(){
  37. for (int i = 0; i < d; i++)
  38. delete[] graf[i];
  39. delete[] graf;
  40. delete[] weight_from;
  41. str.clear();
  42. }
  43. void matrix(char i, char j, double weight){
  44. graf[i][j] = weight;
  45. }
  46. void Display(){
  47. for(int i = 0; i < d; i++){
  48. for(int j = 0; j < d; j++){
  49. cout << graf[i][j] << " ";
  50. }
  51. cout << endl;
  52. }
  53. }
  54. void print_path(int first, int finish){
  55. bool admissible = 1;
  56. bool monotonic = 1;
  57. int i = 0;
  58. if(str.size() > 1){
  59. while(str[i] != finish)
  60. {
  61. admissible = is_admissible(finish, str[i]) && admissible ? 1 : 0;
  62. i++;
  63. monotonic = is_monotonic(finish, str[i-1], str[i]) && monotonic ? 1 : 0;
  64. }
  65. admissible = is_admissible(finish, str[i]) && admissible ? 1 : 0;
  66. for(i = 0; i < str.size() ; i++)
  67. cout << char(str[i] + 97);
  68. if(admissible)
  69. cout<<endl<<"Heuristic is admissible"<<endl;
  70. else
  71. cout<<endl<<"Heuristic is not admissible"<<endl;
  72. if(monotonic)
  73. cout<<"Heuristic is monotonic"<<endl;
  74. else
  75. cout<<"Heuristic is not monotonic"<<endl;
  76. }
  77. else cout << "нет пути";
  78. }
  79. double evristic(int i, int j){
  80. return abs(i - j);
  81. }
  82. bool is_admissible( int finish, int ver){
  83. return weight_from[ver] >= evristic(finish, ver);
  84. }
  85. bool is_monotonic(int finish, int ver1, int ver2){
  86. return weight_from[ver1] - weight_from[ver2] >= evristic(finish, ver1) - evristic(finish, ver2);
  87. }
  88. void min_way_A_star(int first, int finish, priority_queue <Priority> &queue, double way){
  89. vector<int> str1;
  90. while(true){
  91. for(int j = 0; j < d; j++)
  92. if(graf[first][j] != 0){
  93. Priority elem;
  94. elem.var_evristic = evristic(finish, j);
  95. elem.prior = graf[first][j] + way + elem.var_evristic;
  96. weight_from[first] = graf[first][j] + way;
  97. for(auto i: str1)
  98. elem.resultat.push_back(i);
  99. elem.resultat.push_back(j);
  100. queue.push(elem);
  101. }
  102. if(queue.empty())
  103. break;
  104. if(!queue.empty()){
  105. Priority popped;
  106. popped = queue.top();
  107. queue.pop();
  108. first = popped.resultat[popped.resultat.size()-1];
  109. str1 = popped.resultat;
  110. way = popped.prior - popped.var_evristic;
  111. }
  112. if(str1[str1.size()-1] == finish){
  113. for(auto i: str1)
  114. str.push_back(i);
  115. break;
  116. }
  117. }
  118. str1.clear();
  119. }
  120. };
  121.  
  122. int main() {
  123. char start, the_end, from, to;
  124. double weight;
  125. cin>> start >> the_end;
  126. priority_queue <Priority> queue;
  127. Graf matr(N, start - 'a');
  128. while(cin >> from >> to >> weight){
  129. matr.matrix(from - 'a', to - 'a', weight);
  130. }
  131. // matr.Display();
  132. matr.min_way_A_star(start - 'a', the_end - 'a', queue, 0);
  133. matr.print_path(start - 'a', the_end - 'a');
  134. return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement