Guest User

f.cpp

a guest
Jun 26th, 2015
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. //#define __GLIBCXX_DEBUG
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. #define rep(i, n) for(int i = 0; i < (int)(n); ++i)
  6. #define pb push_back
  7. #define ALL(a) (a.begin()), (a.end())
  8. #define mp make_pair
  9. #define F first
  10. #define S second
  11. #define SZ(a) ((int)(a.size()))
  12.  
  13. typedef long long ll;
  14. typedef pair<int, int> PI;
  15.  
  16. struct UF{
  17.   vector<int> ap;
  18.   UF(int n): ap(n){
  19.       rep(i, n) ap[i] = i;
  20.   }
  21.  
  22.   int find(int x){
  23.     if(ap[x] == x)
  24.       return x;
  25.     return ap[x] = find(ap[x]);
  26.   }
  27.  
  28.   void unit(int a, int b){
  29.     ap[find(a)] = find(b);
  30.   }
  31. };
  32.  
  33. int n, m, k;
  34.  
  35. vector<PI> aG[300], bG[300];
  36.  
  37. void solve(){
  38.   rep(i, n) aG[i].clear();
  39.   rep(i, n) bG[i].clear();
  40.   vector<pair<int, PI> > aed, bed;
  41.   rep(i, m){
  42.     int a, b, c;
  43.     char ch;
  44.     cin >> a >> b >> c >> ch;
  45.     --a, --b;
  46.     if(ch == 'A'){
  47.       aed.pb(mp(c, mp(a, b)));
  48.       aG[a].pb(mp(b, c));
  49.       aG[b].pb(mp(a, c));
  50.     }else{
  51.       bed.pb(mp(c, mp(a, b)));
  52.       bG[b].pb(mp(a, c));
  53.       bG[a].pb(mp(b, c));
  54.     }
  55.   }
  56.  
  57.   vector<bool> usea(SZ(aed));
  58.   vector<bool> useb(SZ(bed));
  59.   sort(ALL(bed));
  60.   sort(ALL(aed));
  61.   UF uf(n);
  62.   int cost = 0;
  63.   rep(i, SZ(bed)){
  64.     int a = bed[i].S.F;
  65.     int b = bed[i].S.S;
  66.     if(uf.find(a) == uf.find(b)) continue;
  67.     uf.unit(a, b);
  68.     useb[i] = 1;
  69.     cost += bed[i].F;
  70.   }
  71.  
  72.   int useaed = 0;
  73.   rep(i, SZ(aed)){
  74.     int a = aed[i].S.F;
  75.     int b = aed[i].S.S;
  76.     if(uf.find(a) == uf.find(b)) continue;
  77.     uf.unit(a, b);
  78.     usea[i] = 1;
  79.     cost += aed[i].F;
  80.     useaed += 1;
  81.   }
  82.  
  83.   {
  84.     bool ok = true;
  85.     rep(i, n) ok &= uf.find(i) == uf.find(0);
  86.     if(!ok || useaed > k){
  87.       cout << -1 << endl;
  88.       return;
  89.     }
  90.   }
  91.  
  92.   while(useaed < k){
  93.     vector<pair<int, PI> > ap;
  94.     rep(i, SZ(bed)){
  95.       if(!useb[i]) continue;
  96.       UF uf(n);
  97.       rep(j, SZ(bed)){
  98.         if(i == j || !useb[j]) continue;
  99.         int a = bed[j].S.F;
  100.         int b = bed[j].S.S;
  101.         uf.unit(a, b);
  102.       }
  103.       rep(j, SZ(aed)){
  104.         if(!usea[j]) continue;
  105.         int a = aed[j].S.F;
  106.         int b = aed[j].S.S;
  107.         uf.unit(a, b);
  108.       }
  109.      
  110.       rep(j, SZ(aed)){
  111.         if(usea[j]) continue;
  112.         int a = aed[j].S.F;
  113.         int b = aed[j].S.S;
  114.         if(uf.find(a) == uf.find(b)) continue;
  115.         ap.pb(mp(aed[j].F - bed[i].F, mp(i, j)));
  116.         break;
  117.       }
  118.     }
  119.     if(ap.empty()) break;
  120.     sort(ALL(ap));
  121.     ++useaed;
  122.     useb[ap[0].S.F] = false;
  123.     usea[ap[0].S.S] = true;
  124.     cost += ap[0].F;
  125.   }
  126.  
  127.   if(useaed == k)
  128.     cout << cost << endl;
  129.   else
  130.     cout << -1 << endl;
  131. }
  132.  
  133. int main(int argc, char *argv[])
  134. {
  135.   while(cin >> n >> m >> k && n) solve();
  136.   return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment