#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <cmath>
using namespace std;
int pre[105];
struct unit{
int u,v,w;
int k;
}village[10005];
bool compare(unit a,unit b){
return a.w<b.w;
}
int find(int x){
return pre[x]==x?x:pre[x]=find(pre[x]);
}
void join(int a,int b){
pre[b]=a;
}
int main(){
int i,m,n;
while(~scanf("%d",&n)&&n){
scanf("%d",&m);
for(i=1;i<=m;i++){
pre[i]=i;
}
for(i=1;i<=n;i++){
scanf("%d%d%d",&village[i].u,&village[i].v,&village[i].w);
}
sort(village+1,village+1+n,compare);
int result=0,k=0;
for(i=1;i<=n;i++){
int x=find(village[i].u),y=find(village[i].v);
if(x!=y){
join(x,y);
result=result+village[i].w;
k++;
}
}
//cout<<"k "<<k<<endl;
if(k==m-1){
printf("%d\n",result);
}else{
printf("?\n");
}
}
return 0;
}
hdoj 1863 畅通工程
最新推荐文章于 2021-08-29 11:59:12 发布