#include<cstdio>
#include<algorithm>
using namespace std;
int N,M;
struct edge { int u,v,cost;
bool operator < (const edge& rhs) const
{
return cost < rhs.cost;
} };
edge es[110];
int sett[110];
int find2(int x)
{
if(sett[x] == x ) return x;
else return sett[x] = find2(sett[x]);
}
void unite(int x,int y)
{
x =find2(x);
y =find2(y);
if(x<y) sett[y]=x;
else sett[x]=y;
}
bool same(int x,int y)
{
return find2(x)==find2(y);
}
int kruskal()
{
sort(es,es+N);
for(int i=0;i<=M;i++) sett[i]=i;
int res=0;
int num=0;
for(int i=0;i<N;i++){
edge e = es[i];
if( !same(e.u, e.v) )
{
unite(e.u,e.v);
res+=e.cost;
num++;
}
}
// printf("%d%d",num,M);
if(num < M-1) printf("?\n");
else printf("%d\n",res);
}
int main()
{
while(scanf("%d%d",&N,&M)&&N )
{
for(int i=0;i<N;i++)
scanf("%d%d%d",&es[i].u,&es[i].v,&es[i].cost);
kruskal();
}
return 0;
}

最新文章

  1. jquery在线五子棋
  2. forward内部跳转 和redirect重定向跳转的区别
  3. Cucumber
  4. 如何使用Jlink
  5. CentOS最小化安装后,增加GNOME桌面
  6. 工作流(Workflow)学习---基础知识整理
  7. g++ error: extra qualification on member [-fpermissive]
  8. springmvc 接收对象 滴灌摘要
  9. Python3基础 set() 集合 创建集合与特点:自动将重复合并掉 不支持索引
  10. Latex 表格内文字过长自动换行
  11. iOS开发基础-UITableView基本属性
  12. Springboot配置多数据源(Mysql和Orcale)--(Idea Maven JDBCTemplate支持下的)
  13. 扁平数组构建DOM树
  14. 创建Android Apps的30个经验教训
  15. C#删除WebBrowser控件Session
  16. 线程---local数据隔离
  17. python基础学习Day11 函数名的应用、闭包、迭代器
  18. Vue笔记:使用 VS Code 断点调试
  19. font-awesome图标
  20. django(五):cookie和session

热门文章

  1. sehlle脚本获取linux服务器基本信息
  2. Less用法注意事项
  3. python note of decorator
  4. 51nod 1057 n的阶乘 (压位优化)
  5. HDU - 2612 Find a way(BFS搜索)
  6. linux ping-测试主机之间网络的连通性
  7. span-wise drag/lift forces of cylinder
  8. Spring Boot 2 (二):Spring Boot 2 尝鲜-动态 Banner
  9. 集训第六周 数学概念与方法 计数 排列 L题
  10. h-ui.admin.pro.iframe头部和标签Tab修改CSS