-
Notifications
You must be signed in to change notification settings - Fork 2
/
1152.cpp
56 lines (53 loc) · 1.07 KB
/
1152.cpp
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
#include <bits/stdc++.h>
#define MAX 200000
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector< ii > vii;
vii g[MAX + 1];
bool visitado[MAX+1];
int n,m;
ll prim(int s){
ll cost = 0;
priority_queue< ii, vector<ii>, greater<ii> > pq;
for(int i = 0; i <=n ;i++) visitado[i] = false;
for(int i = 0; i< g[s].size(); i++){
pq.push(ii(g[s][i].second,g[s][i].first));
visitado[s] = true;
}
while(!pq.empty()){
ii aux = pq.top();
pq.pop();
int u,d;
u = aux.first;
d = aux.second;
if(!visitado[d]){
cost += u;
visitado[d] = true;
for(int i = 0;i < g[d].size(); i++){
if(!visitado[g[d][i].first])
pq.push(ii(g[d][i].second,g[d][i].first));
}
}
}
return cost;
}
int main(){
cin >> m >> n;
while(n!=0 || m!=0){
for(int i = 0;i <=m ;i++) g[i].clear();
ll total = 0;
for(int i = 0; i<n;i++){
int x,y,z;
cin >> x >> y >> z;
g[x].push_back(ii(y,z));
g[y].push_back(ii(x,z));
total += z;
}
ll resp = total - prim(0);
cout << resp << endl;
cin >> m >> n;
}
return 0;
}