Advertisement
PedalaVasile

modern

Oct 16th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream fin("modern.in");
  6. ofstream fout("modern.out");
  7.  
  8. int parent[110], rnk[110], dist[110], c[110], p[110];
  9.  
  10. struct date
  11. {
  12.     int t, cost;
  13. };
  14.  
  15. struct da
  16. {
  17.     int f, t, cost;
  18. };
  19.  
  20. bool comp(da a, da b)
  21. {
  22.     return a.cost < b.cost;
  23. }
  24.  
  25. vector < date > l[110];
  26. vector < da > v;
  27.  
  28. int n, m, cT, maxim, loc;
  29.  
  30. queue < int > q;
  31.  
  32. int ufind(int nod)
  33. {
  34.     while (parent[nod] != nod)
  35.     {
  36.         parent[nod] = parent[parent[nod]];
  37.         nod = parent[nod];
  38.     }
  39.  
  40.     return nod;
  41. }
  42.  
  43. void unite(int a, int b)
  44. {
  45.     if (rnk[a] > rnk[b])
  46.         parent[b] = a;
  47.     else
  48.         parent[a] = b;
  49.  
  50.     if (rnk[a] == rnk[b])
  51.         rnk[b]++;
  52. }
  53.  
  54. void Kruskal()
  55. {
  56.     for (da el : v)
  57.     {
  58.         int rootF = ufind(el.f), rootT = ufind(el.t);
  59.  
  60.         if (rootF != rootT)
  61.         {
  62.             l[el.f].push_back({el.t, el.cost});
  63.  
  64.             cT += el.cost;
  65.             unite(rootT, rootF);
  66.         }
  67.     }
  68. }
  69.  
  70. void Bfs(int nod)
  71. {
  72.     dist[nod] = 1;
  73.     c[nod] = 1;
  74.     q.push(nod);
  75.  
  76.     while (!q.empty())
  77.     {
  78.         nod = q.front();
  79.         q.pop();
  80.  
  81.         for (date el : l[nod])
  82.         {
  83.             q.push(el.t);
  84.  
  85.             p[el.t] = nod;
  86.  
  87.             if (!dist[el.t])
  88.             {
  89.                 c[el.t] = c[nod] + 1;
  90.                 dist[el.t] = dist[nod] + el.cost;
  91.             }
  92.  
  93.             if (dist[el.t] > maxim)
  94.             {
  95.                 maxim = dist[el.t];
  96.                 loc = el.t;
  97.             }
  98.  
  99.         }
  100.     }
  101. }
  102.  
  103. void Drum(int nod)
  104. {
  105.     if(p[nod])
  106.         Drum(p[nod]);
  107.  
  108.     fout << nod << ' ';
  109. }
  110.  
  111. int main()
  112. {
  113.     fin >> n >> m;
  114.  
  115.     for (int i = 1; i <= m; i++)
  116.     {
  117.         int a, b, c;
  118.  
  119.         fin >> a >> b >> c;
  120.  
  121.         v.push_back({a, b, c});
  122.     }
  123.  
  124.     sort(v.begin(), v.end(), comp);
  125.  
  126.     for (int i = 1; i <= n; i++)
  127.         parent[i] = i, rnk[i] = 1;
  128.  
  129.  
  130.     Kruskal();
  131.  
  132.     for (int i = 1; i <= n; i++)
  133.         for (date el : l[i])
  134.             fout << i << ' ' << el.t << '\n';
  135.  
  136.  
  137.     fout << cT * 100 << '\n';
  138.  
  139.     Bfs(1);
  140.  
  141.     fout << loc << ' ' << maxim - 1 << ' ' << c[loc] << '\n';
  142.  
  143.     Drum(loc);
  144.  
  145.     fin.close();
  146.     fout.close();
  147.     return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement