-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprim.cpp
More file actions
67 lines (57 loc) · 1.34 KB
/
prim.cpp
File metadata and controls
67 lines (57 loc) · 1.34 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
/* Usando matriz de adjacencia */
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int N = 1005; //numero maximo de vertices
const int inf = 1e9; //nao ha perigo de overflow
int g[N][N]; //grafo
int pai[N]; //para reconstruir o caminho
int dist[N]; //distancia minima de cada vertice a arvore
bool mstSet[N];
long long prim(int ss, int nn){
priority_queue<pii> pq;
for(int i = 0; i < nn; i++){
mstSet[i] = 0; dist[i] = inf; // AGMaxima -> troca por dist[i] = -1
}
int edges = 0;
long long cst = 0;
dist[ss] = 0;
pai[ss] = -1;
pq.push(make_pair(0, -ss));
while(!pq.empty() && edges < nn){
int uu = -pq.top().second;
pq.pop();
if(mstSet[uu]) continue;
cst += dist[uu];
mstSet[uu] = 1;
edges++;
for(int vv = 0; vv < nn; vv++){
if(g[uu][vv] != 0){
if(!mstSet[vv] && g[uu][vv] < dist[vv]){ //AGMaxima -> troca por g[uu][vv] > dist[vv]
pai[vv] = uu;
dist[vv] = g[uu][vv];
pq.push(make_pair(-g[uu][vv], -vv));
}
}
}
}
return cst;
}
int main()
{
int nn, mm;
while(scanf("%d %d", &nn, &mm), nn){
for(int i = 0; i < nn; i++){
for(int j = 0; j < nn; j++){
g[i][j] = 0;
}
}
for(int aa, bb, cst, i = 0; i < mm; i++){
scanf("%d %d %d", &aa, &bb, &cst);
g[aa][bb] = g[bb][aa] = cst;
}
long long ans = prim(0, nn);
printf("%Ld\n", ans);
}
return 0;
}