hdu1879 继续畅通工程 基础最小生成树
#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 继续畅通工程 基础最小生成树的更多相关文章
- hdu1863 畅通工程 基础最小生成树
#include <iostream> #include <cstdio> #include <algorithm> #define N 110 #define M ...
- hdu1233 还是畅通工程 基础最小生成树
//克鲁斯卡尔 #include<iostream> #include<algorithm> using namespace std; ; struct node { int ...
- HDU1875——畅通工程再续(最小生成树:Kruskal算法)
畅通工程再续 Description相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖,发展首先要解决的问题当 ...
- HDU1879--继续畅通工程(最小生成树)
Problem Description 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).现得到城镇道路统计 ...
- HDU1233--还是畅通工程(最小生成树)
Problem Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直 ...
- 还是畅通工程,最小生成树kruskal
题目描述: 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可 ...
- HDU1879 继续畅通工程 2017-04-12 19:12 50人阅读 评论(0) 收藏
继续畅通工程 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submis ...
- A - 还是畅通工程(最小生成树)
点击打开链接 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并 ...
- hdu 1233 还是畅通工程 (最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1233 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others) ...
随机推荐
- (转)gcc学习笔记
1.gcc -Wall hello.c -o hello //编译源文件,显示警告信息 2../a.out //运行程序 3.gcc -Wall calc.c /usr/lib/libm.a -o ...
- android编译遇到问题修改
(注意要确定安装了jdk) 第一步: cd lichee; ./build.sh -p sun5i_elite -k 3.0 (apt-get install uboot-mkimage需要安装 ...
- Creating a .bash_profile on your mac
A typical install of OS X won't create a .bash_profile for you. When you want to run functions from ...
- 模式识别之聚类算法k-均值---k-均值聚类算法c实现
//写个简单的先练习一下,测试通过 //k-均值聚类算法C语言版 #include <stdlib.h> #include <stdio.h> #inc ...
- socketIO原理图
- Python之如果添加扩展包
1.首先下载好你需要的扩展包 下载地址是http://www.lfd.uci.edu/~gohlke/pythonlibs/ 2.将你下载好的.whl文件放在你的python文件夹中的Lib\site ...
- 一步一步学Silverlight 2系列(20):如何在Silverlight中与HTML DOM交互(下)
述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...
- skynet源码阅读<1>--lua与c的基本交互
阅读skynet的lua-c交互部分代码时,可以看到如下处理: struct skynet_context * context = lua_touserdata(L, lua_upvalueindex ...
- Python机器视觉编程常用数据结构与示例
本文总结了使用Python进行机器视觉(图像处理)编程时常用的数据结构,主要包括以下内容: 数据结构 通用序列操作:索引(indexing).分片(slicing).加(adding).乘(multi ...
- 【POJ 2411】 Mondriaan's Dream
[题目链接] 点击打开链接 [算法] 很明显,我们可以用状态压缩动态规划解决此题 f[n][m]表示n-1行已经放满,第n行状态为m的合法的方案数 状态转移方程很好推 注意这题时限较紧,注意加一些小优 ...