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

hdu1879 继续畅通工程 基础最小生成树的更多相关文章

  1. hdu1863 畅通工程 基础最小生成树

    #include <iostream> #include <cstdio> #include <algorithm> #define N 110 #define M ...

  2. hdu1233 还是畅通工程 基础最小生成树

    //克鲁斯卡尔 #include<iostream> #include<algorithm> using namespace std; ; struct node { int ...

  3. HDU1875——畅通工程再续(最小生成树:Kruskal算法)

    畅通工程再续 Description相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖,发展首先要解决的问题当 ...

  4. HDU1879--继续畅通工程(最小生成树)

    Problem Description 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).现得到城镇道路统计 ...

  5. HDU1233--还是畅通工程(最小生成树)

    Problem Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直 ...

  6. 还是畅通工程,最小生成树kruskal

    题目描述:     某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可 ...

  7. HDU1879 继续畅通工程 2017-04-12 19:12 50人阅读 评论(0) 收藏

    继续畅通工程 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submis ...

  8. A - 还是畅通工程(最小生成树)

    点击打开链接 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并 ...

  9. hdu 1233 还是畅通工程 (最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1233 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)    ...

随机推荐

  1. DirectX11 学习笔记5 - D3DXMath 库

    directx11 特别推出了一个数学库 用于向量 矩阵的计算.并且使用128位 同一时候计算4条32位数据 (SIMD) 之前基本是用的directx10math.h这个头文件.  这个数学库既然是 ...

  2. SpringMVC框架下使用jfreechart绘制折线图,柱状图,饼状图

    java代码 @Controller public class CityAction { @Autowired private CityBiz cityBiz; //柱状图 @RequestMappi ...

  3. java:BufferedImage推断图像通道顺序并转RGB/BGR

    一般来说java ImageIO处理读取图像时.通常是RGB或ARGB格式,可是有的时候.我们须要图像是BGR格式. 比方通过JNI将图像矩阵传递给动态库,动态库里用OpenCV来处理矩阵,而用Ope ...

  4. Material-Animations

    https://github.com/eltld/Material-Animations

  5. Mac使用小结

    1.修改pip的镜像地址及更新pip https://www.cnblogs.com/techroad4ca/p/5922389.html 2.更新python的库,比如更新six sudo pip ...

  6. debian var目录

    1 /usr和/var /usr,只读数据. /var,可变数据. 2 /var/lib和/var/cache /var/lib,保存应用或者系统可变的状态信息,真的只是状态信息,比如/var/lib ...

  7. C++11 std::function、std::bind和lambda表达式

    参考博客: C++可调用对象详解-https://www.cnblogs.com/Philip-Tell-Truth/p/5814213.html 一.关于std::function与std::bin ...

  8. Codeforces Beta Round #25 (Div. 2 Only)E. Test

    E. Test time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

  9. React引入,运行

    1.引入 <script src="https://cdn.bootcss.com/react/15.5.4/react.min.js"></script> ...

  10. poj 3368 Frequent values 解题报告

    题目链接:http://poj.org/problem?id=3368 题目意思:给出一段 n 个数的序列你,对于区间 [l, r] 的询问,找出 出现频率最高的数的次数.考虑到序列中的数是非递减的, ...