-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path1287Networking.cpp
More file actions
54 lines (54 loc) · 1.14 KB
/
1287Networking.cpp
File metadata and controls
54 lines (54 loc) · 1.14 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
#include<iostream>
#include<vector>
#include<string.h>
#include<stdio.h>
#include<stack>
#include<algorithm>
#include<stdlib.h>
#include<queue>
#include<utility>
#include<float.h>
#include<math.h>
#include<sstream>
#include<stdlib.h>
#include<limits.h>
#include<string>
#include<assert.h>
#include<numeric>
#include<map>
using namespace std;
const int N = 105;
const int INF = 0x3f3f3f3f;
int n,m;
int g[N][N];
int vis[N];
int dist[N];
int prim(){
memset(vis,0,sizeof(vis));
memset(dist,0x3f,sizeof(dist));
dist[1] = 0;
int cost = 0;
int id,best;
for(int k=0;k<n;k++){
best = INF;
for(int i=1;i<=n;i++) if(!vis[i] && dist[i]<best) best = dist[i],id=i;
cost += dist[id];
vis[id] = 1;
for(int i=1;i<=n;i++) if(!vis[i] && g[id][i]<dist[i]) dist[i]=g[id][i];
}
return cost;
}
int main(){
int u,v,w;
while(scanf("%d",&n),n){
scanf("%d",&m);
memset(g,0x3f,sizeof(g));
for(int i=0;i<m;i++){
scanf("%d%d%d",&u,&v,&w);
g[u][v] = min(g[u][v],w);
g[v][u] = min(g[v][u],w);
}
printf("%d\n",prim());
}
return 0;
}