Guest User

Untitled

a guest
May 20th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.37 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 parent[v] = 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.                     parent[b] = a;
  51.                     size[a] += size[b];
  52.                     return true;
  53.     }
  54.      
  55.     void MST() {
  56.                     for (int i = 0; i < n; i++) {
  57.                             if (cafe[i]) {
  58.                                     parent[i] = p;
  59.                                     size[p]++;
  60.                             }
  61.                             else {
  62.                             parent[i] = i;
  63.                                 size[i] = 1;
  64.                   }
  65.                     }
  66.      
  67.                     for (int i = 0; i < m; i++) {
  68.                             if (union_sets(arr[i].first.first, arr[i].first.second)) {
  69.                                     ans += arr[i].second;
  70.                     if (size[arr[i].first.first] == n || size[arr[i].first.second] == n)
  71.                                     return;
  72.                             }
  73.                            
  74.                     }
  75.     }
  76.      
  77.     int main()
  78.     {
  79.         //freopen("input.txt","r",stdin);
  80.                     cin >> t;
  81.                     while(t--) {
  82.                        ans = 0;            
  83.                             scanf("%d%d",&n,&m);
  84.            
  85.             getline(cin, str);
  86.             getline(cin, str);
  87.            for (int j = 0; j < n; j++)
  88.            {
  89.                  cafe[j] = (str[j] == 'R');
  90.                  if(cafe[j]) p = j;
  91.                  size[j] = 0;
  92.                 }
  93.                
  94.                             for (int j = 0; j < m; j++)
  95.                             {
  96.                                 scanf ("%d%d%d", &a, &b, &c);
  97.                                 a--;
  98.                                 b--;
  99.                                 if (c < 0) {
  100.                                        ans += c;
  101.                                        c = 0;
  102.                                     }
  103.                               arr[j] = make_pair(make_pair(a, b), c);
  104.                             }
  105.                    
  106.                             sort(arr, arr + m, cmp);
  107.                             MST();
  108.                             cout << ans << endl;          
  109.             }
  110.             //      system("pause");
  111.                    
  112.           return 0;
  113.     }
Add Comment
Please, Sign In to add comment