Advertisement
Ginger_samurai

Untitled

May 24th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.23 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 = 1e18 + 123, 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. void dfsa(vector<vector<pll>>& g, vector<ll>& dist) {
  31.     dist[0] = 0;
  32.     vector<bool>used(g.size(), false);
  33.     queue<ll>q;
  34.     q.push(0);
  35.     used[0] = true;
  36.     while (!q.empty()) {
  37.         ll x = q.front();
  38.         q.pop();
  39.         for (ll i = 0; i < g[x].size(); i++) {
  40.             if (!used[g[x][i].first]) {
  41.                 dist[g[x][i].first] = dist[x] + 1;
  42.                 used[g[x][i].first] = true;
  43.                 q.push(g[x][i].first);
  44.             }
  45.         }
  46.     }
  47. }
  48.  
  49. void dfsb(vector<vector<pll>>& g, vector<ll>& dist) {
  50.     dist[g.size() - 1] = 0;
  51.     vector<bool>used(g.size(), false);
  52.     queue<ll>q;
  53.     q.push(g.size() - 1);
  54.     used[g.size() - 1] = true;
  55.     while (!q.empty()) {
  56.         ll x = q.front();
  57.         q.pop();
  58.         for (ll i = 0; i < g[x].size(); i++) {
  59.             if (!used[g[x][i].first]) {
  60.                 dist[g[x][i].first] = dist[x] + 1;
  61.                 used[g[x][i].first] = true;
  62.                 q.push(g[x][i].first);
  63.             }
  64.         }
  65.     }
  66. }
  67.  
  68.  
  69.  
  70. void bfs(vector<vector<pll>>& g, vector<ll>& mindist) {
  71.     ll n = g.size();
  72.     vector<ll>dist(n, inf);
  73.     vector<bool>flag(n, false), used(n, false), pushed(n, false);
  74.     queue<ll>q;
  75.     q.push(0);
  76.     used[0] = true;
  77.     dist[0] = 0;
  78.     pushed[0] = true;
  79.     while (!q.empty()) {
  80.         ll x = q.front();
  81.         q.pop();
  82.         //cout << x;
  83.         if (!flag[x]) {
  84.             for (ll i = 0; i < g[x].size(); i++) {
  85.                 pll to = g[x][i];
  86.                 if (!used[to.first] || dist[to.first] == dist[x] + 1) {
  87.                     mindist[dist[x] + 1] = min(mindist[dist[x] + 1], to.second);
  88.                 }
  89.  
  90.             }
  91.             flag[x] = true;
  92.             q.push(x);
  93.         }
  94.         else {
  95.             for (ll i = 0; i < g[x].size(); i++) {
  96.                 pll to = g[x][i];
  97.                 if (!used[to.first]) {
  98.                     dist[to.first] = dist[x] + 1;
  99.                     used[to.first] = true;
  100.                     if (to.second == mindist[dist[to.first]]) {
  101.                         q.push(to.first);
  102.                         pushed[to.first] = true;
  103.                     }
  104.                 }
  105.                 else {
  106.                     if (dist[to.first] == dist[x] + 1 && !pushed[to.first]) {
  107.                         if (to.second == mindist[dist[to.first]]) {
  108.                             q.push(to.first);
  109.                             pushed[to.first] = true;
  110.                         }
  111.                     }
  112.                 }
  113.             }
  114.         }
  115.     }
  116. }
  117.  
  118. bool comp(vector<ll>& a, vector<ll>& b) {
  119.     if (a.size() != b.size()) {
  120.         return false;
  121.     }
  122.     for (ll i = 0; i < a.size(); i++) {
  123.         if (a[i] > b[i]) {
  124.             return false;
  125.         }
  126.         if (a[i] < b[i]) {
  127.             return true;
  128.         }
  129.     }
  130.     return true;
  131. }
  132.  
  133. void A(vector<vector<pll>>& g, ll& sz, vector<ll>& ans) {
  134.  
  135.     ll n = g.size();
  136.     queue<pll>q;
  137.     vector<ll>dist(n, inf);
  138.     vector<bool>used(n, false);
  139.     dist[n - 1] = 0;
  140.     vector<pll>prev(n, { -1, -1 });
  141.     prev[n - 1] = { -1, -1 };
  142.     q.push({ n - 1, 0 });
  143.     while (!q.empty()) {
  144.         pll x = q.front();
  145.         q.pop();
  146.         for (ll i = 0; i < g[x.first].size(); i++) {
  147.             pll to = g[x.first][i];
  148.             if (dist[x.first] + 1 < dist[to.first]) {
  149.                 prev[to.first].first = x.first;
  150.                 prev[to.first].second = to.second;
  151.                 dist[to.first] = dist[x.first] + 1;
  152.             }
  153.             else if (dist[x.first] + 1 == dist[to.first]) {
  154.                 if (to.second < prev[to.first].second) {
  155.                     prev[to.first].first = x.first;
  156.                     prev[to.first].second = to.second;
  157.                 }
  158.             }
  159.             if (!used[to.first]) {
  160.                 used[to.first] = true;
  161.                 q.push(to);
  162.             }
  163.         }
  164.  
  165.  
  166.     }
  167.     ll x = 0;
  168.     while (prev[x].first != -1) {
  169.         ans.push_back(prev[x].second);
  170.         x = prev[x].first;
  171.  
  172.     }
  173. }
  174.  
  175. int main()
  176. {
  177.     xru();
  178.     while (true) {
  179.  
  180.         ll n = (ll)rand() % 13 + 2, m =1 + (ll)rand() % 7;
  181.         //cin >> n >> m;
  182.         vector<vector<ll>>check(n, vector<ll>(n, llinf));
  183.         vector<ll>x(m), y(m), cnt(m);
  184.         for (ll i = 0; i < m; i++) {
  185.             // cin >> x[i] >> y[i] >> cnt[i];
  186.             x[i] = 1 + (ll)rand() % n, y[i] = 1 + (ll)rand() % n, cnt[i] = (ll)rand() % 8;
  187.             x[i]--, y[i]--;
  188.             if (x[i] == y[i]) continue;
  189.             check[x[i]][y[i]] = min(check[x[i]][y[i]], cnt[i]);
  190.             check[y[i]][x[i]] = check[x[i]][y[i]];
  191.         }
  192.         vector < vector<pll>>g(n), lg(n);
  193.         for (ll i = 0; i < m; i++) {
  194.             if (x[i] == y[i] || check[x[i]][y[i]] == llinf) continue;
  195.             g[x[i]].push_back({ y[i], check[x[i]][y[i]] });
  196.             g[y[i]].push_back({ x[i], check[x[i]][y[i]] });
  197.             check[x[i]][y[i]] = llinf;
  198.             check[y[i]][x[i]] = llinf;
  199.  
  200.         }
  201.  
  202.         vector<ll>ans2;
  203.         ll sz = 0;
  204.         A(g, sz, ans2);
  205.  
  206.  
  207.         vector<ll> dista(n, inf), distb(n, inf);
  208.         dfsa(g, dista);
  209.         dfsb(g, distb);
  210.  
  211.  
  212.         vector<ll>dist(n, inf);
  213.         vector<bool>used(n, false);
  214.         queue<ll>q;
  215.         dist[0] = 0;
  216.         used[0] = true;
  217.         q.push(0);
  218.         while (!q.empty()) {
  219.             ll x = q.front();
  220.             q.pop();
  221.             for (ll i = 0; i < g[x].size(); i++) {
  222.                 pll to = g[x][i];
  223.                 if (!used[to.first]) {
  224.                     dist[to.first] = dist[x] + 1;
  225.                     used[to.first] = true;
  226.                     q.push(to.first);
  227.                     if (dista[to.first] + distb[to.first] == distb[0]) {
  228.                         //cout <<11<<" "<< x <<" "<< to.first << endl;
  229.                         lg[x].push_back(to);
  230.                         lg[to.first].push_back({ x, to.second });
  231.                     }
  232.                 }
  233.                 else {
  234.                     if (dist[x] + 1 == dist[to.first]) {
  235.                         if (dista[to.first] + distb[to.first] == distb[0]) {
  236.                             // cout <<22<<" "<< x << " " << to.first << endl;
  237.                             lg[x].push_back(to);
  238.                             lg[to.first].push_back({ x, to.second });
  239.                         }
  240.                     }
  241.                 }
  242.             }
  243.         }
  244.         /*
  245.         for (ll i = 0; i < n; i++) {
  246.             cout << i + 1 << "  ";
  247.             for (ll j = 0; j < lg[i].size(); j++) {
  248.                 cout << lg[i][j].first + 1 << "," << lg[i][j].second << " ";
  249.             }
  250.             cout << endl;
  251.         }
  252.  
  253.         */
  254.         vector<ll>ans;
  255.         if (dist[n - 1] != inf) {
  256.  
  257.  
  258.             vector<ll>mindist(dist[n - 1] + 1, inf);
  259.             bfs(lg, mindist);
  260.             // cout << dist[n - 1] << endl;
  261.             for (ll i = 1; i < mindist.size(); i++) {
  262.                 ans.push_back(mindist[i]);
  263.             }
  264.         }
  265.         if (!comp(ans, ans2)) {
  266.             cout << n << " " << m << endl;
  267.             for (ll i = 0; i < m; i++) {
  268.                 cout << x[i] + 1 << " " << y[i] + 1 << " " << cnt[i] << endl;
  269.             }
  270.             cout << endl;
  271.             cout(ans);
  272.             cout << endl;
  273.             cout(ans2);
  274.             break;
  275.         }
  276.     }
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement