Advertisement
Ginger_samurai

Untitled

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