Advertisement
Ginger_samurai

Untitled

May 21st, 2020
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. #include<vector>
  2. #include <string>
  3. #include<algorithm>
  4. #include <iostream>
  5. #include <queue>
  6. #include<set>
  7. #include<cmath>
  8. #include<math.h>
  9. using namespace std;
  10. #define ll long long
  11. #define pll pair<long long, long long>
  12. #define mp make_pair
  13. #define pb push_back
  14. #define all(a) a.begin(), a.end()
  15. #define rall(a) a.rbegin(), a.rend()
  16. #define cin(a) for(ll ghgh = 0; ghgh<a.size(); ghgh++) cin»a[ghgh];
  17. #define pcin(a) for(ll ghgh = 0; ghgh<a.size(); ghgh++) cin»a[ghgh].first»a[ghgh].second;
  18. #define cout(a) for(ll ghgha = 0; ghgha<a.size(); ghgha++) cout«a[ghgha]«" ";
  19. #define pcout(a) for(ll ghgha = 0; ghgha<a.size(); ghgha++) cout«a[ghgha].first«" "«a[ghgha].second«endl;
  20. const ll inf = 1e9 + 7, llinf = 1e18 + 123;
  21. void xru() {
  22. setlocale(LC_ALL, "rus");
  23. /*freopen(".in", "r", stdin);
  24. freopen(".out", "w", stdout);*/
  25. ios_base::sync_with_stdio(false);
  26. cin.tie(NULL);
  27. cout.tie(NULL);
  28. }
  29.  
  30.  
  31. struct tp {
  32. ll a, b, c;
  33. };
  34.  
  35.  
  36.  
  37. int main()
  38. {
  39. xru();
  40.  
  41. ll n, m;
  42. cin » n » m;
  43. vector<vector<pll»g(n), check(n, vector<pll>(n, { 0, 0 }));
  44. vector<tp>id(m);
  45. for (ll i = 0; i < m; i++) {
  46. ll x, y, cnt;
  47. cin » x » y » cnt;
  48. x--, y--;
  49. id[i].a = x;
  50. id[i].b = y;
  51. id[i].c = cnt;
  52. if (x == y) {
  53. continue;
  54. }
  55. if (check[x][y].first != 0) {
  56. check[x][y].second = min(check[x][y].second, cnt);
  57. check[y][x].second = check[x][y].second;
  58. }
  59. else {
  60. check[x][y] = { 1, cnt };
  61. check[y][x] = check[x][y];
  62. }
  63. }
  64. for (ll i = 0; i < m; i++) {
  65. if (id[i].a == id[i].b) {
  66. continue;
  67. }
  68. if (check[ id[i].a ][ id[i].b ].first == 1) {
  69. g[id[i].a].push_back({ id[i].b, check[id[i].a][id[i].b].second });
  70. g[id[i].b].push_back({ id[i].a, check[id[i].a][id[i].b].second });
  71. check[id[i].a][id[i].b].first--;
  72. check[id[i].b][id[i].a].first--;
  73.  
  74. }
  75. }
  76.  
  77. queue<pll>q;
  78. vector<ll>dist(n, inf);
  79. vector<bool>used(n, false);
  80. dist[n - 1] = 0;
  81. vector<pll>prev(n);
  82. prev[n-1] = { -1, -1 };
  83. q.push({ n - 1, 0 });
  84. while (!q.empty()) {
  85. pll x = q.front();
  86. q.pop();
  87. for (ll i = 0; i < g[x.first].size(); i++) {
  88. pll to = g[x.first][i];
  89. if (dist[x.first] + 1 < dist[to.first]) {
  90. prev[to.first].first = x.first;
  91. prev[to.first].second = to.second;
  92. dist[to.first] = dist[x.first] + 1;
  93. }
  94. else if (dist[x.first] + 1 == dist[to.first]) {
  95. if (to.second < prev[to.first].second) {
  96. prev[to.first].first = x.first;
  97. prev[to.first].second = to.second;
  98. }
  99. }
  100. if (!used[to.first]) {
  101. used[to.first] = true;
  102. q.push(to);
  103. }
  104. }
  105.  
  106.  
  107. }
  108. ll x = 0;
  109. vector<ll>ans;
  110. while (prev[x].first != -1) {
  111. ans.push_back(prev[x].second);
  112. x = prev[x].first;
  113.  
  114. }
  115. cout « ans.size()«endl;
  116. cout(ans);
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement