hud3371 Connect the Cities 简单最小生成树
//我看过Discuss说不能用克鲁斯卡尔因为有很多边
//但是只能用G++过,C++的确超时
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; struct node
{
int a, b, cost;
}c[];
int fa[]; void init(int n)
{
for (int i = ; i <= n; i++)
fa[i] = i;
} bool cmp(node x, node y)
{
return x.cost<y.cost;
} int find(int x)
{
if (fa[x] != x) fa[x] = find(fa[x]);
return fa[x];
} int main()
{
int n, k, m, ncase;
scanf("%d", &ncase);
while (ncase--)
{
scanf("%d %d %d", &n, &k, &m);
init(n); //初始化
for (int i = ; i<k; i++)
scanf("%d %d %d", &c[i].a, &c[i].b, &c[i].cost);
for (int i = ; i <= m; i++)
{
int x, pos, pos1;
scanf("%d %d", &x, &pos);
for (int j = ; j<x; j++)
{
scanf("%d", &pos1);
c[k].a = pos, c[k].b = pos1, c[k].cost = ;
pos = pos1;
k++;
}
} sort(c, c + k, cmp);
int sum = ;
for (int i = ; i<k; i++)
{
int x = find(c[i].a);
int y = find(c[i].b);
if (x != y)
sum += c[i].cost, fa[x] = y;
} int count = ;
for (int i = ; i <= n; i++)
if (fa[i] == i)
count++; if (count != )
printf("-1\n");
else
printf("%d\n", sum);
}
return ;
}
hud3371 Connect the Cities 简单最小生成树的更多相关文章
- hdu oj 3371 Connect the Cities (最小生成树)
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- Hdu 3371 Connect the Cities(最小生成树)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 其实就是最小生成树,但是这其中有值得注意的地方:就是重边.题目没有告诉你两个城市之间只有一条路可走, ...
- hdu 3371 Connect the Cities(最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...
- HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑
这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...
- hdu 3371 Connect the Cities
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Description In 2100, since th ...
- HDU3371--Connect the Cities(最小生成树)
Problem Description In 2100, since the sea level rise, most of the cities disappear. Though some sur ...
- HDU 3371 Connect the Cities(prim算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3371 Problem Description In 2100, since the sea leve ...
- Connect the Cities[HDU3371]
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- Connect the Cities(MST prim)
Connect the Cities Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
随机推荐
- Xammp修改端口
How can I get XAMPP working on port 80 under Windows 10? By default, Windows 10 starts Microsoft IIS ...
- Android图表AChartEngine
很多时候项目中我们需要对一些统计数据进行绘制表格,更多直观查看报表分析结果.基本有以下几种方法: 1:可以进行android api进行draw这样的话,效率比较低 2:使用开源绘表引擎,这样效率比较 ...
- fatal error C1083: 无法打开预编译头文件:“Debug\opencv.pch”: No such file or directory
stdafx.cpp右键——属性,预编译头选“创建”,其它cpp选“使用”. 调试不能优化.
- FLTK 简介
FLTK,如同其名字所表达的:The Fast Light Tool Kit,一个轻量级的GUI开发库.但这轻量级并不代表功能的羸弱,相反,FLTK在具有基本的GUI功能之外,还拥有一些特 ...
- CLI和CGI的区别
CGI :“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上.以CGI方式运行时,web s ...
- 提升自身的iOS编程水平 (转载)
阅读博客 在现在这个碎片化阅读流行的年代,博客的风头早已被微博盖过.而我却坚持写作博客,并且大量地阅读同行的iOS开发博客.博客的文章长度通常在3000字左右,许多iOS开发知识都至少需要这样的篇幅才 ...
- spring-jar包详解整理(大合集)
转:https://blog.csdn.net/weisong530624687/article/details/50888094 spring.jar 是包含有完整发布模块的单个jar 包.但是不包 ...
- hdu 1711 Number Sequence 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 题目意思:给出一条有n个数的序列a[1],a[2],......,a[n],和一条有m 个数的序 ...
- re(正则表达式)模块
一.最常用的匹配语法 re.match 从头开始匹配 re.search 匹配包含 re.findall 把所有匹配到的字符放到以列表中的元素返回 re.split 以匹配到的字符当做列表分隔符 r ...
- android项目 res/ 目录内支持的资源目录详解
表 1. 项目 res/ 目录内支持的资源目录 目录 资源类型 animator/ 用于定义属性动画的 XML 文件. anim/ 定义渐变动画的 XML 文件.(属性动画也可以保存在此目录中,但是为 ...