hdu1879 继续畅通工程 基础最小生成树
2024-08-30 13:48:05
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
using namespace std; #define MAXN 10005
int fa[MAXN];
struct node
{
int from, to, len;
}arr[MAXN]; bool cmp(node a, node b)
{
return a.len < b.len;
} int m; int find(int x)
{
if (x == fa[x])
return x;
else
return fa[x] = find(fa[x]);
} int main()
{
int n;
while (scanf("%d",&n)&&n)
{
for (int i = ; i <= n; i++)
fa[i] = i;
m = ;
int s = n*(n - ) / ;
for (int i = ; i < s; i++)
{
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
if (d)
{
int x = find(a);
int y = find(b);
if (x != y)
fa[y] = x;
}
else
{
arr[m].from = a;
arr[m].to = b;
arr[m].len = c;
m++;
}
} sort(arr, arr + m, cmp);
int ans = ;
for (int i = ; i < m; i++)
{
int a = find(arr[i].from);
int b = find(arr[i].to);
if (a != b)
{
ans += arr[i].len;
fa[b] = a;
}
}
printf("%d\n", ans);
}
//system("pause");
return ;
}
最新文章
- iOS App禁止横屏
- 如何使用sysdba身份通过jdbc连接oracle?
- CMD命令之 :修改windows的CMD窗口输出编码格式为UTF-8
- linux中用shell获取昨天、明天或多天前的日期
- 七、context command
- AngularJs在单击提交后显示验证信息.
- 自己实现的库函数1(strlen,strcpy,strcmp,strcat)
- Compass被墙后如何安装安装
- Android ServiceConnection
- OC基础-day02
- 导出含有图片的Java项目,图片不显示
- 自反ACL(第三组)
- [PHP] defunct僵尸进程
- Python Installing Jupyter
- 记事本:CSS
- Alpha、伪Beta 发布个人感想与体会
- 慎用 apt-get autoremove !
- DM8168 PWM驱动与測试程序
- erlang转化中文为url
- 转:vs无法调试解决方案