Advertisement
he_obviously

Untitled

May 18th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <queue>
  6.  
  7. using namespace std;
  8.  
  9. vector <vector <pair <int, int>>> v;
  10. vector <int> d;
  11. vector <int> p;
  12. vector <vector <int>> roads;
  13.  
  14. bool operator>(vector <int>& a, vector <int>& b) {
  15.     int i = 0;
  16.     while (i < (int)a.size()) {
  17.         if (a[i] != b[i]) {
  18.             return a[i] > b[i];
  19.         }
  20.         ++i;
  21.     }
  22.     return false;
  23. }
  24.  
  25. int main() {
  26.     ios_base::sync_with_stdio(0);
  27.     cin.tie(0); cout.tie(0);
  28.     int n, m;
  29.     cin >> n >> m;
  30.     v.resize(n + 1, {});
  31.     d.resize(n + 1, -1);
  32.     p.resize(n + 1, -1);
  33.     roads.resize(n + 1, {});
  34.     for (int i = 0; i < m; ++i) {
  35.         int a, b, c;
  36.         cin >> a >> b >> c;
  37.         if (a != b) {
  38.             v[a].push_back({ b, c });
  39.             v[b].push_back({ a, c });
  40.         }
  41.     }
  42.     queue <pair <int, int>> q;
  43.     q.push({1, 0});
  44.     d[1] = 0;
  45.     while (!q.empty()) {
  46.         pair <int, int> s = q.front();
  47.         int start = s.first;
  48.         q.pop();
  49.         if (start == n) {
  50.             break;
  51.         }
  52.         for (auto x : v[start]) {
  53.             if (x.first == s.second) continue;
  54.             if (d[x.first] == -1) {
  55.                 d[x.first] = d[start] + 1;
  56.                 p[x.first] = start;
  57.                 roads[x.first] = roads[start];
  58.                 roads[x.first].push_back(x.second);
  59.                 q.push({ x.first, start });
  60.             }
  61.             else {
  62.                 if (d[x.first] == d[start] + 1) {
  63.                     roads[start].push_back(x.second);
  64.                     if (roads[x.first] > roads[start]) {
  65.                         roads[x.first] = roads[start];
  66.                     }
  67.                     roads[start].pop_back();
  68.                 }
  69.             }
  70.         }
  71.         roads[start].clear();
  72.     }
  73.     cout << roads[n].size() << "\n";
  74.     for (int x : roads[n]) cout << x << " ";
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement