hdu1863
2024-08-29 14:26:36
#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;
}
最新文章
- jquery在线五子棋
- forward内部跳转 和redirect重定向跳转的区别
- Cucumber
- 如何使用Jlink
- CentOS最小化安装后,增加GNOME桌面
- 工作流(Workflow)学习---基础知识整理
- g++ error: extra qualification on member [-fpermissive]
- springmvc 接收对象 滴灌摘要
- Python3基础 set() 集合 创建集合与特点:自动将重复合并掉 不支持索引
- Latex 表格内文字过长自动换行
- iOS开发基础-UITableView基本属性
- Springboot配置多数据源(Mysql和Orcale)--(Idea Maven JDBCTemplate支持下的)
- 扁平数组构建DOM树
- 创建Android Apps的30个经验教训
- C#删除WebBrowser控件Session
- 线程---local数据隔离
- python基础学习Day11 函数名的应用、闭包、迭代器
- Vue笔记:使用 VS Code 断点调试
- font-awesome图标
- django(五):cookie和session
热门文章
- sehlle脚本获取linux服务器基本信息
- Less用法注意事项
- python note of decorator
- 51nod 1057 n的阶乘 (压位优化)
- HDU - 2612 Find a way(BFS搜索)
- linux ping-测试主机之间网络的连通性
- span-wise drag/lift forces of cylinder
- Spring Boot 2 (二):Spring Boot 2 尝鲜-动态 Banner
- 集训第六周 数学概念与方法 计数 排列 L题
- h-ui.admin.pro.iframe头部和标签Tab修改CSS