Advertisement
Guest User

Untitled

a guest
May 27th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <cmath>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <climits>
  5. #include <vector>
  6. using namespace std;
  7.  
  8. int n, m;
  9. int b[200][200];
  10.  
  11. int main()
  12. {
  13.     freopen("input.txt", "r", stdin);
  14.     freopen("output.txt", "w", stdout);
  15.  
  16.     cin >> n >> m;
  17.  
  18.     vector <pair<int, pair<int, int>>> a(m + 1);
  19.  
  20.     for (int i = 0; i < m; i++)
  21.     {
  22.         int t1, t2, t3;
  23.         cin >> t1 >> t2 >> t3;
  24.         a[i].first = t3;
  25.         a[i].second.first = t1 - 1;
  26.         a[i].second.second = t2 - 1;
  27.     }
  28.    
  29.     sort(a.begin(), a.end());
  30.  
  31.     vector <int> tr(n + 1);
  32.     vector < pair<int, int> > res;
  33.  
  34.     for (int i = 0; i < n; i++)
  35.         tr[i] = i;
  36.  
  37.     int c = 0;
  38.  
  39.     for (int i = 0; i < m; i++)
  40.     {
  41.         int s = a[i].second.first;
  42.         int e = a[i].second.second;
  43.         int w = a[i].first;
  44.  
  45.         if (tr[s] != tr[e])
  46.         {
  47.             b[s][e] = w;
  48.             b[e][s] = w;
  49.             res.push_back(make_pair(s, e));
  50.             int to = tr[e];
  51.             int tn = tr[s];
  52.             c += w;
  53.  
  54.             for (int j = 0; j < n; j++)
  55.             {
  56.                 if (tr[j] == to)
  57.                     tr[j] = tn;
  58.             }
  59.         }
  60.     }
  61.  
  62.     cout << c << endl;
  63.  
  64.     for (int i = 0; i < n; i++)
  65.     {
  66.         for (int j = 0; j < n; j++)
  67.             cout << b[i][j] << " ";
  68.         cout << endl;
  69.     }
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement