#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 ;
}

最新文章

  1. iOS App禁止横屏
  2. 如何使用sysdba身份通过jdbc连接oracle?
  3. CMD命令之 :修改windows的CMD窗口输出编码格式为UTF-8
  4. linux中用shell获取昨天、明天或多天前的日期
  5. 七、context command
  6. AngularJs在单击提交后显示验证信息.
  7. 自己实现的库函数1(strlen,strcpy,strcmp,strcat)
  8. Compass被墙后如何安装安装
  9. Android ServiceConnection
  10. OC基础-day02
  11. 导出含有图片的Java项目,图片不显示
  12. 自反ACL(第三组)
  13. [PHP] defunct僵尸进程
  14. Python Installing Jupyter
  15. 记事本:CSS
  16. Alpha、伪Beta 发布个人感想与体会
  17. 慎用 apt-get autoremove !
  18. DM8168 PWM驱动与測试程序
  19. erlang转化中文为url
  20. 转:vs无法调试解决方案

热门文章

  1. C3P0连接池配置和实现详解(转)
  2. ViewPagerTransforms
  3. for in 与for of
  4. mac系统不同java版本切换
  5. SpringInAction4笔记——web
  6. 备忘录模式-Memento
  7. CH 5302 金字塔(区间DP)
  8. 【POJ 1159】Palindrome
  9. 关于animate的一些属性
  10. Spring Boot2.0之 整合Redis事务