a53

chromosome

a53
Aug 14th, 2020 (edited)
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. /// Silion Liviu
  2. /// Se bazeaza pe ideea asta rezolvarea: https://prnt.sc/tzpbeg.
  3. /// Se face un vector pentru fiecare traseu pentru a verifica sa nu fie niciun loop si pentru a putea reconstitui traseul.
  4.  
  5. #include <fstream>
  6. #include <vector>
  7. #include <string>
  8. #include <queue>
  9. #include <utility>
  10. #include <functional>
  11. #include <algorithm>
  12. #include <iostream>
  13.  
  14. using Ip = std::pair<int, int>;
  15. using Vp = std::pair<int, std::vector<int>>;
  16. using Vi = std::vector<int>;
  17. using Vb = std::vector<bool>;
  18.  
  19. namespace std
  20. {
  21. class InParser
  22. {
  23. public:
  24. InParser(const char *file_name)
  25. {
  26. input_file.open(file_name, std::ios::in);
  27. input_file.sync_with_stdio(false);
  28. index = 0;
  29. input_file.read(buffer, SIZE);
  30. }
  31. InParser &operator>>(int &n)
  32. {
  33. for (; !std::isdigit(buffer[index]); increment())
  34. ;
  35. n = 0;
  36. for (; std::isdigit(buffer[index]); increment())
  37. n = n * 10 + (buffer[index] - '0');
  38. return *this;
  39. }
  40. ~InParser()
  41. {
  42. input_file.close();
  43. }
  44.  
  45. private:
  46. std::fstream input_file;
  47. static const int SIZE = 0x1000;
  48. int index;
  49. char buffer[SIZE];
  50. void increment()
  51. {
  52. if (++index == SIZE)
  53. index = 0, input_file.read(buffer, SIZE);
  54. }
  55. };
  56. class OutParser
  57. {
  58. public:
  59. OutParser(const char *file_name)
  60. {
  61. output_file.open(file_name, std::ios::out);
  62. output_file.sync_with_stdio(false);
  63. index = 0;
  64. }
  65. OutParser &operator<<(int64_t target)
  66. {
  67. if (target < 10)
  68. increment(target + '0');
  69. else
  70. {
  71. (*this) << (target / 10);
  72. increment(target % 10 + '0');
  73. }
  74. return *this;
  75. }
  76. OutParser &operator<<(int target)
  77. {
  78. if (target < 10)
  79. increment(target + '0');
  80. else
  81. {
  82. (*this) << (target / 10);
  83. increment(target % 10 + '0');
  84. }
  85. return *this;
  86. }
  87. OutParser &operator<<(const char *target)
  88. {
  89. int aux = 0;
  90. while (target[aux])
  91. increment(target[aux++]);
  92. return *this;
  93. }
  94. ~OutParser()
  95. {
  96. output_file.write(buffer, index);
  97. output_file.close();
  98. }
  99.  
  100. private:
  101. std::fstream output_file;
  102. static const int SIZE = 0x1000;
  103. int index;
  104. char buffer[SIZE];
  105. void increment(char ch)
  106. {
  107. if (index == SIZE)
  108. index = 0, output_file.write(buffer, SIZE), buffer[index++] = ch;
  109. else
  110. buffer[index++] = ch;
  111. }
  112. };
  113. OutParser &operator<<(OutParser &flux, const Vp &p)
  114. {
  115. flux << /*"weight " <<*/ p.first << " " << (int)p.second.size() << "\n" /*" using path: "*/;
  116. for (const auto &it : p.second)
  117. flux << it << " ";
  118. flux << "\n";
  119. return flux;
  120. }
  121. ofstream &operator<<(ofstream &flux, const Vp &p)
  122. {
  123. flux << /*"weight " <<*/ p.first << " " << p.second.size() << "\n" /*" using path: "*/;
  124. for (const auto &it : p.second)
  125. flux << it << " ";
  126. flux << "\n";
  127. return flux;
  128. }
  129. ostream &operator<<(ostream &flux, const Vi &p)
  130. {
  131. for (const auto &it : p)
  132. flux << it << " ";
  133. return flux;
  134. }
  135. ostream &operator<<(ostream &flux, const Vp &p)
  136. {
  137. flux << "weight " << p.first << " using path: ";
  138. for (const auto &it : p.second)
  139. flux << it << " ";
  140. flux << "\n";
  141. return flux;
  142. }
  143. template <>
  144. struct greater<Vp> : binary_function<Vp, Vp, bool>
  145. {
  146. bool operator()(const Vp &p1, const Vp &p2) const
  147. {
  148. if (p1.first != p2.first)
  149. return p1.first > p2.first;
  150. vector<int> v1 = p1.second, v2 = p2.second;
  151. reverse(v1.begin(), v1.end()), reverse(v2.begin(), v2.end());
  152. return v1 > v2;
  153. }
  154. };
  155. }; // namespace std
  156.  
  157. std::InParser fin("chromosome.in");
  158. std::OutParser fout("chromosome.out");
  159.  
  160. int V, E, K, u, v, w, src, dest;
  161. std::vector<std::vector<Ip>> Graph;
  162. std::vector<Vp> Path;
  163. Vi Count, Curr, New;
  164. Vb Seen;
  165. std::priority_queue<Vp, std::vector<Vp>, std::greater<Vp>> PQ;
  166.  
  167. int main()
  168. {
  169. fin >> V >> E >> K >> src >> dest;
  170. Graph.resize(V + 1), Count.assign(V + 1, 0);
  171. for (int i = 1; i <= E; ++i)
  172. fin >> u >> v >> w,
  173. Graph[u].emplace_back(std::make_pair(v, w));
  174. //Graph[v].emplace_back (std::make_pair (u, w));
  175.  
  176. Curr.push_back(src);
  177. PQ.push(std::make_pair(0, Curr));
  178. while (!PQ.empty() && Count[dest] < K)
  179. {
  180. Curr = PQ.top().second;
  181. //std::cout << PQ.top ();
  182. int cost = PQ.top().first, u = Curr.back();
  183. PQ.pop(), ++Count[u];
  184. if (u == dest)
  185. Path.emplace_back(std::make_pair(cost, Curr));
  186. if (Count[u] <= K)
  187. {
  188. for (const auto &it : Graph[u])
  189. {
  190. New = Curr;
  191. Seen.assign(V + 1, false);
  192. for (const auto &it : New)
  193. Seen[it] = true;
  194. if (!Seen[it.first])
  195. New.emplace_back(it.first),
  196. PQ.push(std::make_pair(cost + it.second, New));
  197. }
  198. }
  199. }
  200.  
  201. fout << (int)Path.size() << "\n";
  202. for (const auto &it : Path)
  203. fout << it;
  204.  
  205. //fin.close (), fout.close ();
  206. return 0;
  207. }
Add Comment
Please, Sign In to add comment