-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
6-yuyu0830
- Loading branch information
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#include <iostream> | ||
#include <queue> | ||
|
||
#define fastio cin.tie(NULL); cin.sync_with_stdio(false); | ||
|
||
using namespace std; | ||
|
||
struct edge { | ||
int vertex1, vertex2, value; | ||
|
||
edge(int v1, int v2, int _value) : vertex1(v1), vertex2(v2), value(_value) {}; | ||
}; | ||
|
||
struct cmp { | ||
bool operator()(edge a, edge b) { return a.value > b.value; } | ||
}; | ||
|
||
bool manOnly[1001] = {0, }; | ||
|
||
// Union-Find Function | ||
int parent[1001] = {0, }; | ||
|
||
int find(int v) { | ||
if (parent[v] == v) return v; | ||
return parent[v] = find(parent[v]); | ||
} | ||
|
||
void merge(int x, int y) { parent[find(x)] = find(y); } | ||
|
||
bool isUnion(int x, int y) { return find(x) == find(y); } | ||
|
||
int main() { | ||
fastio | ||
int n, m; cin >> n >> m; | ||
|
||
for (int i = 1; i <= n; i++) { | ||
// Union Initialize | ||
parent[i] = i; | ||
|
||
// is University Man only? | ||
char c; cin >> c; | ||
if (c == 'M') manOnly[i] = 1; | ||
} | ||
|
||
priority_queue<edge, vector<edge>, cmp> q; | ||
|
||
for (int i = 0; i < m; i++) { | ||
int a, b, c; cin >> a >> b >> c; | ||
|
||
// No same gender... | ||
if (manOnly[a] == manOnly[b]) continue; | ||
|
||
q.push(edge(a, b, c)); | ||
} | ||
|
||
int cnt = 1, ans = 0; | ||
|
||
while (!q.empty()) { | ||
edge tmp = q.top(); | ||
q.pop(); | ||
|
||
int v1 = tmp.vertex1; | ||
int v2 = tmp.vertex2; | ||
|
||
// is cycle created? Then pass this edge | ||
if (isUnion(v1, v2)) continue; | ||
|
||
// add this edge | ||
merge(v1, v2); | ||
cnt++; | ||
ans += tmp.value; | ||
|
||
// if all Vertex visited | ||
if (cnt == n) { | ||
printf("%d\n", ans); | ||
return 0; | ||
} | ||
} | ||
|
||
printf("-1\n"); | ||
} |