fork download
  1. // ~~ icebear love attttttt ~~
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef pair<int, int> ii;
  7. typedef pair<int, ii> iii;
  8.  
  9. template<class T>
  10. bool minimize(T &a, const T &b) {
  11. if (a > b) return a = b, true;
  12. return false;
  13. }
  14.  
  15. template<class T>
  16. bool maximize(T &a, const T &b) {
  17. if (a < b) return a = b, true;
  18. return false;
  19. }
  20.  
  21. #define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
  22. #define FORR(i,a,b) for(int i=(a); i>=(b); --i)
  23. #define REP(i, n) for(int i=0; i<(n); ++i)
  24. #define RED(i, n) for(int i=(n)-1; i>=0; --i)
  25. #define MASK(i) (1LL << (i))
  26. #define mp make_pair
  27. #define pb push_back
  28. #define fi first
  29. #define se second
  30. #define all(x) x.begin(), x.end()
  31. #define task "icebearat"
  32.  
  33. const int MOD = 1e9 + 7;
  34. const int inf = 1e9 + 27092008;
  35. const ll INF = 1e18 + 27092008;
  36. const int N = 1e5 + 5;
  37. int n, m, k;
  38. int c[N], lab[N];
  39. array<int, 3> edge[N];
  40.  
  41. void init(void) {
  42. cin >> n >> m >> k;
  43. FOR(i, 1, n) cin >> c[i];
  44. FOR(i, 1, m) {
  45. int u, v, w;
  46. cin >> u >> v >> w;
  47. edge[i] = {w, u, v};
  48. }
  49. }
  50.  
  51. int root(int v) {
  52. return (lab[v] < 0 ? v : lab[v] = root(lab[v]));
  53. }
  54.  
  55. bool unite(int u, int v) {
  56. u = root(u); v = root(v);
  57. if (u == v) return false;
  58. if (lab[u] > lab[v]) swap(u, v);
  59. lab[u] += lab[v];
  60. lab[v] = u;
  61. return true;
  62. }
  63.  
  64. void process(void) {
  65. FOR(i, 1, n) if (!c[i])
  66. c[i] = i + k;
  67. FOR(i, 1, m) {
  68. int u = edge[i][1], v = edge[i][2], w = edge[i][0];
  69. edge[i] = {w, c[u], c[v]};
  70. }
  71.  
  72. sort(edge + 1, edge + m + 1);
  73. memset(lab, -1, sizeof lab);
  74. ll mst = 0;
  75. int numEdge = 0;
  76. FOR(i, 1, m)
  77. if (unite(edge[i][1], edge[i][2])) mst += edge[i][0], numEdge++;
  78. cout << (numEdge == k - 1 ? mst : -1);
  79. }
  80.  
  81. int main() {
  82. ios_base::sync_with_stdio(0);
  83. cin.tie(0); cout.tie(0);
  84. if (fopen(task".inp", "r")) {
  85. freopen(task".inp", "r", stdin);
  86. freopen(task".out", "w", stdout);
  87. }
  88. int tc = 1;
  89. // cin >> tc;
  90. while(tc--) {
  91. init();
  92. process();
  93. }
  94. return 0;
  95. }
  96.  
  97.  
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
-1