解题报告:有n个点,然后有m条可以添加的边,然后有一个k输入,表示一开始已经有k个集合的点,每个集合的点表示现在已经是连通的了。

还是用并查集加克鲁斯卡尔。只是在输入已经连通的集合的时候,通过并查集将该集合的点标记到一起,然后剩下的就可以当成是普通的最小生成树来做了题目代码如下:

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdlib>
using namespace std; struct node
{
int front ,rear,cost;
}rode[];
int pre[];
int find(int n)
{
return pre[n] == n? n:pre[n] = find(pre[n]);
}
int cmp(const void *a,const void *b)
{
return (*(node*)a).cost <= (*(node*)b).cost? -:;
}
int judge(int n)
{
for(int i = ;i <= n;++i)
if(find() != find(i))
return ;
return ;
} int main()
{
int T;
int ci,s,d;
scanf("%d",&T);
while(T--)
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
for(int i = ;i <= m;++i)
scanf("%d%d%d",&rode[i].front,&rode[i].rear,&rode[i].cost);
qsort(rode+,m,sizeof(node),cmp);
for(int i = ;i <= n;++i)
pre[i] = i;
while(k--)
{
scanf("%d%d",&ci,&s);
ci--;
while(ci--)
{
scanf("%d",&d);
pre[find(s)] = find(d);
}
}
int ans = ;
for(int i = ;i <= m;++i)
{
int temp1 = find(rode[i].front);
int temp2 = find(rode[i].rear);
if(temp1 != temp2)
{
ans += rode[i].cost;
pre[temp1] = temp2;
}
}
printf(judge(n)? "%d\n":"-1\n",ans);
}
return ;
}

不过,我过这道题的时候发现一个小问题,就是在排序的时候用sort总会T,但是发现改成qsort之后,比原来快了很多,然后就A了。经过一些测试发现,qsort在数据量比较大的时候要比sort更快,而且数据量越大,差别越大,下面再附上测试用的代码:

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<time.h>
using namespace std; const int maxn = ; int que[maxn]; int cmp(const void *a,const void *b)
{
return (*(int*)a) <= (*(int*)b)? -:;
}
bool comp(int a,int b)
{
return a <= b;
}
int main()
{
for(int i = maxn - ;i >=;--i)
que[i] = i;
int s = clock();
qsort(que,maxn,sizeof(int),cmp);
int e = clock();
printf("t_qsort = %d\n",e - s);
s = clock();
sort(que,que+maxn,comp);
e = clock();
printf("t_sort = %d\n",e - s);
return ;
}

HDU 3371 Connect the Cities 最小生成树(和关于sort和qsort的一些小发现)的更多相关文章

  1. hdu 3371 Connect the Cities(最小生成树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...

  2. hdu 3371 Connect the Cities (最小生成树Prim)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...

  3. hdu 3371 Connect the Cities

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Description In 2100, since th ...

  4. HDU 3371 Connect the Cities(prim算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3371 Problem Description In 2100, since the sea leve ...

  5. Hdu 3371 Connect the Cities(最小生成树)

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 其实就是最小生成树,但是这其中有值得注意的地方:就是重边.题目没有告诉你两个城市之间只有一条路可走, ...

  6. HDU 3371 Connect the Cities(并查集+Kruskal)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先 ...

  7. POJ:3371 Connect the Cities(最小生成树)

    http://acm.hdu.edu.cn/showproblem.php?pid=3371 AC代码: /** /*@author Victor /* C++ */ #include <bit ...

  8. hdu oj 3371 Connect the Cities (最小生成树)

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  9. HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑

    这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...

随机推荐

  1. PowerDesigner数据库设计实用技巧

    欢迎大家补充,谢谢! 1. 原始单据与实体之间的关系 可以是一对一.一对多.多对多的关系.在一般情况下,它们是一对一的关系:即一张原始单据对应且只对应一个实体.在特殊情况下,它们可能是一对多或多对一的 ...

  2. 互联网寒冬,Python 程序员如何准备面试

    虽说年年都在喊互联网寒冬,最难就业季,但是今年确实有点不一样,年前年后一波又一波互联网公司宣布『人员调整,结构优化』, 这是往年没发生过的. 是不是面试机会就少了很多呢?不是的. 搜索招聘网站我们可以 ...

  3. SQL Sever——远程过程调用失败(0x800706be)

    最近重装了系统,VS和SQL Sever莫名奇妙的不能用了.下面总结一下这个过程中遇到的问题,跟大家分享一下经验~~ 大概是以前的安装过程都十分顺利,这次,在尝试了数次登陆不上去之后,我仍然怀疑是自己 ...

  4. 计算机启动出现 Invalid Partition Table

    计算机启动出现 Invalid Partition Table 解决办法 使用大白菜启动盘进入临时系统,打开程序DiskGenius 如果系统盘(一般为 C 盘)非活动状态,先激活 如果装系统的硬盘不 ...

  5. 谈vs2013单元测试感想

    (1)安装篇:这个就不用多说啦,百度一个安装包进行安装. 之前下载过vs2013当时是抱着玩玩的心态,也没有安装成功,现在作为作业重新安装,并且进行单元测试.下面就是安装vs2013的具体过程以及我遇 ...

  6. mysql 存储过程中结尾分割符修改

    mysql中修改命令结束符delimiter的用法:mysql中的delimiter会告诉MySQL解释器,命令的结束符是什么,默认情况下MySQL的命令是以分号(;)结束的.在遇到(;)时,MySQ ...

  7. [转帖] k8s kubectl 命令行技巧

    https://jimmysong.io/posts/kubectl-cheatsheet/ Kubectl Cheatsheet kubectl命令技巧大全Posted on November 3, ...

  8. 2 引入jquery和boot

    vue引入bootstrap——webpack https://blog.csdn.net/wild46cat/article/details/77662555(copy) 想要在vue中引入boot ...

  9. Python的文件读写

    目录 读文件 操作文件 读取内容 面试题的例子 写文件 操作模式 指针操作 字符编码 读文件 操作文件 打开一个文件用open()方法(open()返回一个文件对象,它是可迭代的): 文件使用完毕后必 ...

  10. spring 事务传播 never 当一个业务方法设置为never时候表示 不会加入任何事务中