-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoj1024.cpp
62 lines (56 loc) · 996 Bytes
/
oj1024.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
57
58
59
60
61
62
#include <stdio.h>
#include <algorithm>
using namespace std;
int tree[110];
int findRoot(int x){
if(tree[x]==-1)
return x;
int tmp=findRoot(tree[x]);
tree[x]=tmp;
return tmp;
}
struct Edge{
int a,b;
int cost;
bool operator < (const Edge &A) const{
return cost<A.cost;
}
} edge[110];
void init(){
for(int i=0;i<100;i++){
tree[i]=-1;
edge[i].a=-1;
edge[i].b=-1;
edge[i].cost=10000;
}
return;
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m)&&n){
init();
for(int i=0;i<n;i++)
scanf("%d%d%d",&edge[i].a,&edge[i].b,&edge[i].cost);
sort(edge,edge+n);
int ans=0;
for(int i=0;i<n;i++){
int a=findRoot(edge[i].a);
int b=findRoot(edge[i].b);
if(a!=b){
ans+=edge[i].cost;
tree[a]=b;
}
}
bool link=true;
for(int i=1,root=findRoot(1);i<=m;i++)
if(findRoot(i)!=root){
link=false;
break;
}
if(link)
printf("%d\n",ans);
else
printf("?\n");
}
return 0;
}