Advertisement
Ginger_samurai

Untitled

May 22nd, 2020
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.16 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.  
  38.  
  39. int main()
  40. {
  41.     xru();
  42.     while (true) {
  43.         ll vn = (ll)rand()  + 2, vm = (ll)rand() % (vn*(vn-1)/2);
  44.         vector<tp>vs(vm);
  45.         for (ll i = 0; i < vm; i++) {
  46.             vs[i].a = 1 + (ll)rand() % (vn);
  47.             vs[i].b = 1 + (ll)rand() % (vn);
  48.             vs[i].c = (ll)rand() % vn;
  49.         }
  50.         ll n, m;
  51.         //cin » n » m;
  52.         n = vn, m = vm;
  53.         cout << n << " " << m << endl;
  54.         vector<tp>id(m);
  55.         vector < vector < pll>>g(n), check(n, vector<pll>(n, { 0, 0 }));
  56.         for (ll i = 0; i < m; i++) {
  57.             ll x, y, cnt;
  58.             // cin » x » y » cnt;
  59.             x = vs[i].a;
  60.             y = vs[i].b;
  61.             cnt = vs[i].c;
  62.             cout << x << " " << y << " "<<  cnt << endl;
  63.             x--, y--;
  64.             id[i].a = x, id[i].b = y, id[i].c = cnt;
  65.             if (x == y) continue;
  66.             if (check[x][y].first == 0) {
  67.                 check[x][y] = { 1, cnt };
  68.                 check[y][x] = check[x][y];
  69.             }
  70.             else {
  71.                 check[x][y].second = min(check[x][y].second, cnt);
  72.                 check[y][x] = check[x][y];
  73.             }
  74.         }
  75.         for (ll i = 0; i < m; i++) {
  76.             if (id[i].a == id[i].b) {
  77.                 continue;
  78.             }
  79.             if (check[id[i].a][id[i].b].first == 1) {
  80.                 g[id[i].a].push_back({ id[i].b, check[id[i].a][id[i].b].second });
  81.                 g[id[i].b].push_back({ id[i].a, check[id[i].a][id[i].b].second });
  82.                 check[id[i].a][id[i].b].first = 0;
  83.                 check[id[i].b][id[i].a].first = 0;
  84.             }
  85.         }
  86.         queue<ll>q;
  87.         vector<pll>prev(n, { -1, -1 });
  88.         vector<ll>dist(n, inf);
  89.         vector<bool>used(n, false);
  90.         dist[n - 1] = 0;
  91.         used[n - 1] = true;
  92.         q.push(n - 1);
  93.         while (!q.empty()) {
  94.             ll x = q.front();
  95.             q.pop();
  96.             for (ll i = 0; i < g[x].size(); i++) {
  97.                 pll to = g[x][i];
  98.                 if (dist[x] + 1 < dist[to.first]) {
  99.                     prev[to.first] = { x, to.second };
  100.                     dist[to.first] = dist[x] + 1;
  101.                 }
  102.                 else if (dist[x] + 1 == dist[to.first]) {
  103.                     if (to.second < prev[to.first].second) {
  104.                         prev[to.first] = { x, to.second };
  105.                     }
  106.                 }
  107.                 if (!used[to.first]) {
  108.                     used[to.first] = true;
  109.                     q.push(to.first);
  110.                 }
  111.             }
  112.         }
  113.         vector<ll>ans;
  114.         ll x = 0;
  115.         ll i = 0;
  116.         while (prev[x].first != -1) {
  117.             // cout « x « " " « prev[x].first « " " « prev[x].second « endl;
  118.             ans.push_back(prev[x].second);
  119.             x = prev[x].first;
  120.             i++;
  121.         }
  122.  
  123.         cout << ans.size() << endl;
  124.         cout(ans);
  125.         cout << endl << endl;
  126.  
  127.  
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement