Kruskal HDOJ 1863 畅通工程
2024-09-21 22:05:39
/*
此题为:HDOJ 1233 + HDOJ 1232
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std; const int MAX_N = + ;
int pre[MAX_N];
int tot;
int sum;
struct NODE
{
int x, y;
int len;
}node[MAX_N]; int find(int root)
{
int son, tmp;
son = root; while (root != pre[root])
{
root = pre[root];
}
while (son != root)
{
tmp = pre[son];
pre[son] = root;
son = tmp;
} return root;
} bool cmp(NODE a, NODE b)
{
return a.len < b.len;
} int main(void) //HDOJ 1863 畅通工程
{
//freopen ("inC.txt", "r", stdin);
int n, m; //n路, m村庄 while (~scanf ("%d%d", &n, &m) && n)
{
tot = m - ;
for (int i=; i<=m; ++i)
{
pre[i] = i;
}
for (int i=; i<=n; ++i)
{
scanf ("%d%d%d", &node[i].x, &node[i].y, &node[i].len);
}
sort (node+, node++n, cmp);
sum = ;
for (int i=; i<=n; ++i)
{
int a, b;
a = find (node[i].x);
b = find (node[i].y);
if (a != b)
{
pre[a] = b;
sum += node[i].len;
--tot;
}
}
if (tot == )
printf ("%d\n", sum);
else
puts ("?");
}
}
最新文章
- flash开发几个问题
- Change Git Default Editor in Windows
- Tomcat 配置 HTTPS双向认证
- shutdown命令
- mysql高可用方案比较
- Linux(Centos)全自动异地备份数据(WEB+Mysql)
- 几个好用的截取字符串的php函数分享
- AttributeError: &#39;int&#39; object has no attribute &#39;log&#39;
- Android UI技巧(一)——Android中伸缩自如的9patch图片切法,没有美工自给自足
- 【TLV】非递归TLV数据解析
- CAS SSO单点登录框架学习
- 牛客多校第二场A run(基础DP)
- HDU1730 Northcott Game 尼姆博弈
- Selenium清空列数据
- 沉淀再出发:关于IntelliJ IDEA使用的一些总结
- 测试面试必会sql(1)
- HDU 4665 Mutiples on a circle (圆环DP)
- jcabanillas/yii2-inspinia-asset composert 安装失败
- 20162327实验一Java开发环境的熟悉实验报告
- vloatile总结与synchronized对比
热门文章
- scrollTo(String text) and scrollToExact(String text) method of Android Driver not working
- ajax的异步操作及页面重定向跳转
- VC断点失败的原因之中的一个
- Bootstrap progress-bar
- 织梦dedecms页面中增加二维码功能的实现方法
- String bulit-in function
- poj 1094 Sorting It All Out 解题报告
- Pycharm中如何安装python库
- [SDOI2016] 模式字符串 (BZOJ4598 &; VIJOS1995)
- 「BZOJ3438」小M的作物(最小割