Guest User

Untitled

a guest
May 20th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.36 KB | None | 0 0
  1.     #include <algorithm>
  2.     #include <iostream>
  3.     #include <sstream>
  4.     #include <string>
  5.     #include <vector>
  6.     #include <queue>
  7.     #include <set>
  8.     #include <map>
  9.     #include <cstdio>
  10.     #include <cstdlib>
  11.     #include <cctype>
  12.     #include <cmath>
  13.      
  14.     using namespace std;
  15.      
  16.     int size[100000];
  17.     int parent [100000];
  18.     bool cafe[100000];
  19.     long long ans;
  20.     pair<pair<int, int>, int> arr[400000];
  21.     int n, m, t, a, b, c, p;
  22.     string str;
  23.      
  24.      
  25.      
  26.     bool cmp(const pair<pair<int, int>, int> &a, const pair<pair<int, int>, int> &b){
  27.          if (a.second < b.second)
  28.              return true;
  29.          if (a.second > b.second)
  30.              return false;
  31.          return a.first < b.first;
  32.     }
  33.      
  34.     int find_set(int v) {
  35.                     if (v == parent[v])
  36.                             return v;
  37.                     return find_set(parent[v]);
  38.     }
  39.      
  40.     bool union_sets(int a, int b) {
  41.                     a = find_set(a);
  42.                     b = find_set(b);
  43.                     if (a == b)
  44.                             return false;
  45.                     if (size[a] < size[b]) {
  46.                             int temp = a;
  47.                             a = b;
  48.                             b = temp;
  49.                     }
  50.  
  51.                     parent[b] = a;
  52.                     if (size[a] == size[b])
  53.                         size[a]++;
  54.                     return true;
  55.     }
  56.      
  57.     void MST() {
  58.                     for (int i = 0; i < n; i++) {
  59.                             if (cafe[i]) {
  60.                                     parent[i] = p;
  61.                                     size[p]++;
  62.                             }
  63.                             else {
  64.                             parent[i] = i;
  65.                                 size[i] = 1;
  66.                   }
  67.                     }
  68.      
  69.                     for (int i = 0; i < m; i++) {
  70.                             if (union_sets(arr[i].first.first, arr[i].first.second)) {
  71.                                     ans += arr[i].second;
  72.                     if (size[arr[i].first.first] == n || size[arr[i].first.second] == n)
  73.                                     return;
  74.                             }
  75.                            
  76.                     }
  77.     }
  78.      
  79.     int main()
  80.     {
  81.         //freopen("input.txt","r",stdin);
  82.                     cin >> t;
  83.                     while(t--) {
  84.                        ans = 0;            
  85.                             scanf("%d%d",&n,&m);
  86.            
  87.             getline(cin, str);
  88.             getline(cin, str);
  89.            for (int j = 0; j < n; j++)
  90.            {
  91.                  cafe[j] = (str[j] == 'R');
  92.                  if(cafe[j]) p = j;
  93.                  size[j] = 0;
  94.            }
  95.                
  96.                             for (int j = 0; j < m; j++)
  97.                             {
  98.                                 scanf ("%d%d%d", &a, &b, &c);
  99.                                 a--;
  100.                                 b--;
  101.                                 if (c < 0) {
  102.                                        ans += c;
  103.                                        c = 0;
  104.                                     }
  105.                               arr[j] = make_pair(make_pair(a, b), c);
  106.                             }
  107.                    
  108.                             sort(arr, arr + m, cmp);
  109.                             MST();
  110.                             cout << ans << endl;          
  111.             }
  112.             //      system("pause");
  113.                    
  114.           return 0;
  115.     }
Add Comment
Please, Sign In to add comment