-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11631 Dark roads.cpp
More file actions
82 lines (73 loc) · 2.05 KB
/
Copy path11631 Dark roads.cpp
File metadata and controls
82 lines (73 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define Set(v,d) memset(v, d, sizeof(v))
#define oo 10000000000000007 //infinity
#define F first
#define S second
#define pb push_back
#define sz(x) (int)(x.size())
#define all(x) (x.begin()), (x.end())
#define rall(x) (x.rbegin()), (x.rend())
typedef vector<int> vi;
struct UnionFind{
vi parent;
UnionFind(int n){
parent = vi(n);
for(int i=0;i<n;i++) parent[i] = i;
}
int findSet(int x){
if(x == parent[x]) return x;
return parent[x] = findSet(parent[x]);
}
void link(int x, int y){
x = findSet(x), y = findSet(y);
parent[x] = y;
}
};
struct edge{
int f, t, w;
edge(int f, int t, int w) : f(f), t(t), w(w) {}
bool operator <(const edge& e) const{
return w > e.w;
}
};
const int MAX = 200005;
int n, m;
vector<edge> adjList;
int MST(){
UnionFind u(n);
int mstCost = 0;
priority_queue<edge>q;
for(int i=0;i<sz(adjList);i++)
q.push(adjList[i]);
while(!q.empty()){
edge e = q.top(); q.pop();
if(u.findSet(e.f) != u.findSet(e.t)){
mstCost += e.w;
u.link(e.f, e.t);
}
}
return mstCost;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
scanf("%d%d", &n, &m);
while(m){
int total = 0;
adjList.clear();
for(int i=0;i<m;i++){
int f, t, w;
scanf("%d%d%d", &f, &t, &w);
adjList.pb(edge(f, t, w));
total += w;
}
cout<<total - MST()<<endl;
scanf("%d%d", &n, &m);
}
return 0;
}