Advertisement
Ginger_samurai

Untitled

May 23rd, 2020
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.73 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.  
  32.  
  33.  
  34.  
  35. int main()
  36. {
  37.  xru();
  38.  
  39.  ll n, m;
  40.  cin >> n >> m;
  41.  vector<vector<ll>>check(n, vector<ll>(n, llinf));
  42.  vector<ll>x(m), y(m), cnt(m);
  43.  for (ll i = 0; i < m; i++) {
  44.      cin >> x[i] >> y[i] >> cnt[i];
  45.      x[i]--, y[i]--;
  46.      if (x[i] == y[i]) continue;
  47.      check[x[i]][y[i]] = min(check[ x[i] ][ y[i] ], cnt[i]);
  48.      check[y[i]][x[i]] = check[x[i]][y[i]];
  49.  }
  50.  vector < vector<pll>>g(n);
  51.  for (ll i = 0; i < m; i++) {
  52.      if (x[i] == y[i] || check[x[i]][y[i]] == llinf) continue;
  53.      g[x[i]].push_back({ y[i], check[x[i]][y[i]] });
  54.      g[y[i]].push_back({ x[i], check[x[i]][y[i]] });
  55.      check[x[i]][y[i]] = llinf;
  56.      check[y[i]][x[i]] = llinf;
  57.  
  58.  }
  59.  vector<pll>prev(n, { -1, inf });
  60.  vector<ll>dist(n, inf);
  61.  vector<bool>used(n, false);
  62.  queue<ll>q;
  63.  vector<vector<pll>> lg(n);
  64.  q.push(n - 1);
  65.  dist[n - 1] = 0;
  66.  used[n - 1] = true;
  67.  while (!q.empty()) {
  68.      ll v = q.front();
  69.      q.pop();
  70.      for (ll i = 0; i < g[v].size(); i++) {
  71.          pll to = g[v][i];
  72.          if (!used[to.first]) {
  73.              dist[to.first] = dist[v] + 1;
  74.              lg[to.first].push_back({ v, to.second });
  75.              lg[v].push_back(to);
  76.              used[to.first] = true;
  77.              q.push(to.first);
  78.          }
  79.          else {
  80.              if (dist[v] + 1 == dist[to.first]) {
  81.                  lg[to.first].push_back({ v, to.second });
  82.                  lg[v].push_back(to);
  83.              }
  84.          }
  85.      }
  86.  }
  87.  queue<ll>lq;
  88.  vector<ll>minLdist(dist[0] + 1, inf);
  89.  vector<ll>ldist(n, inf);
  90.  vector<bool>lused(n, false), vlused(n, false);
  91.  vector<ll>ans;
  92.  ldist[0] = 0;
  93.  lused[0] = true;
  94.  lq.push(0);
  95.  while (!lq.empty()) {
  96.      ll lv = lq.front();
  97.     // cout << lv << endl;
  98.      lq.pop();
  99.      if (!vlused[lv]) {
  100.          //cout << lv << " ";
  101.          for (ll i = 0; i < lg[lv].size(); i++) {
  102.              pll lto = lg[lv][i];
  103.              if (!lused[lto.first]) {
  104.                  minLdist[ ldist[lv] + 1 ] = min( minLdist[ldist[lv] + 1] , lto.second );
  105.  
  106.              }
  107.              else {
  108.                  if (ldist[lv] + 1 == ldist[lto.first]) {
  109.                      minLdist[ldist[lv] + 1] = min(minLdist[ldist[lv] + 1], lto.second);
  110.  
  111.                  }
  112.  
  113.              }
  114.          }
  115.          vlused[lv] = true;
  116.          lq.push(lv);
  117.      }
  118.      else {
  119.          for (ll i = 0; i < lg[lv].size(); i++) {
  120.              pll lto = lg[lv][i];
  121.              if (!lused[lto.first]) {
  122.                  ldist[lto.first] = ldist[lv] + 1;
  123.                  lused[lto.first] = true;
  124.                  if (lto.second == minLdist[ldist[lto.first]]) {
  125.                      lq.push(lto.first);
  126.                  }
  127.              }
  128.          }
  129.      }
  130.  
  131.  }
  132.  
  133.  cout << minLdist.size() - 1<<endl;
  134.  for (ll i = 1; i < minLdist.size(); i++) {
  135.      cout << minLdist[i] << " ";
  136.  }
  137.  
  138.  
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement