Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. #include <iomanip>
  2. #include <iostream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <cstdio>
  6.  
  7. using namespace std;
  8.  
  9. struct struc1{
  10. int64_t right, nowflow, link;
  11. bool used;
  12. };
  13.  
  14. struct struc2{
  15. int64_t l, r, cost;
  16. };
  17.  
  18. int64_t big_num = 1000000000, n, m, posque = 0;
  19. bool flg;
  20. vector<vector<struc1>> List;
  21. vector<int64_t> dist, parent;
  22. vector<struc2> edge;
  23. vector<bool> used;
  24. vector<vector<int64_t>> answer;
  25. vector<int64_t> que;
  26.  
  27. int64_t min(int64_t o1, int64_t o2) {
  28. if (o1 < o2)
  29. return o1;
  30. return o2;
  31. }
  32.  
  33.  
  34. void dfs(){
  35. int vertex = que[posque++];
  36. //cout << vertex << "\n";
  37. for (int i = 0; i < List[vertex].size(); i++)
  38. if (!List[vertex][i].used)
  39. if (dist[List[vertex][i].right] > dist[vertex] + 1) {
  40. parent[List[vertex][i].right] = vertex;
  41. dist[List[vertex][i].right] = dist[vertex] + 1;
  42. que.push_back(List[vertex][i].right);
  43. }
  44. }
  45.  
  46. void bfs() {
  47. while (posque < que.size())
  48. dfs();
  49. }
  50.  
  51.  
  52. int main() {
  53. int64_t k, numto, numfrom;
  54. cin >> n >> m >> numfrom >> numto;
  55. List.resize(n + 1);
  56. dist.resize(n + 1);
  57. parent.resize(n + 1);
  58. edge.resize(2 * m);
  59. used.resize(2 * m);
  60. for (int i = 0; i < m; i++) {
  61. int a, b;
  62. struc1 per1;
  63. cin >> a >> b;
  64. per1.nowflow = 0;
  65. per1.right = b;
  66. per1.link = -1;
  67. per1.used = false;
  68. List[a].push_back(per1);
  69. }
  70. /* for (int i = 1; i <= n; i++)
  71. for (int j = 0; j < List[i].size(); j++) {
  72. if (List[i][j].link < 0) {
  73. struc1 per1;
  74. per1.nowflow = 0;
  75. per1.right = i;
  76. per1.used = false;
  77. per1.link = j;
  78. List[i][j].link = List[List[i][j].right].size();
  79. List[List[i][j].right].push_back(per1);
  80. }
  81. }*/
  82. int64_t total_cost = 0;
  83. answer.resize(2);
  84. for (int i = 1; i <= 2; i++) {
  85. for (int j = 1; j <= n; j++)
  86. dist[j] = big_num;
  87. dist[numfrom] = 0;
  88. que.resize(0);
  89. que.push_back(numfrom);
  90. posque = 0;
  91. bfs();
  92. if (dist[n] == big_num) {
  93. cout << "NO";
  94. exit(0);
  95. }
  96. /*for (int ii = 1; ii <= n; ii++)
  97. cout << dist[ii] << " ";
  98. cout << "\n";
  99. for (int ii = 1; ii <= n; ii++)
  100. cout << parent[ii] << " ";
  101. cout << "\n";*/
  102. int64_t nowpos = numto;
  103. while (nowpos != numfrom) {
  104. int64_t l = parent[nowpos];
  105. for (int g = 0; g < List[l].size(); g++)
  106. if (List[l][g].right == nowpos && !List[l][g].used) {
  107. List[l][g].used = true;
  108. List[l][g].nowflow++;
  109. //
  110.  
  111. if (List[l][g].link < 0) {
  112. struc1 per1;
  113. per1.nowflow = 0;
  114. per1.right = l;
  115. per1.used = false;
  116. per1.link = g;
  117. List[l][g].link = List[nowpos].size();
  118. List[nowpos].push_back(per1);
  119. }
  120.  
  121. //
  122. List[List[l][g].right][List[l][g].link].nowflow--;
  123. //cout << "!" << l << " " << nowpos << " " << g << "\n";
  124. break;
  125. }
  126. //answer[i - 1].push_back(nowpos);
  127. nowpos = l;
  128. //cout << nowpos <<"\n";
  129. }
  130. }
  131. for (int64_t i = 0; i < 2; i++) {
  132. int64_t nowpos = numfrom;
  133. while (nowpos != numto) {
  134. for (int j = 0; j < List[nowpos].size(); j++) {
  135. if (List[nowpos][j].nowflow > 0) {
  136. //cout << nowpos << "\n";
  137. List[nowpos][j].nowflow--;
  138. nowpos = List[nowpos][j].right;
  139. answer[i].push_back(nowpos);
  140. //cout << nowpos << " " << j << "\n";
  141. break;
  142. }
  143. }
  144. }
  145. }
  146. cout << "YES\n";
  147. for (int j = 0; j < 2; j++) {
  148. cout << numfrom << " ";
  149. for (int i = 0; i < answer[j].size(); i++)
  150. cout << answer[j][i] << " ";
  151. cout << "\n";
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement