解题报告:有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. JAVA笔试准备

    建立时间:2019.4.19 修改时间: 腾讯:选择题(30个,一小时内),简答(2道)和编程题(2道) 涉及内容:(也有可能全是算法)C++,JAVA,数据结构,网络,Linux,计算题 1. 磁盘 ...

  2. Daily Scrumming* 2015.12.16(Day 8)

    一.团队scrum meeting照片 二.成员工作总结 姓名 任务ID 迁入记录 江昊 任务1036 https://github.com/buaaclubs-team/temp-front/com ...

  3. BlogPublishTool - 博客发布工具

    BlogPublishTool - 博客发布工具 这是一个发布博客的工具.本博客使用本工具发布. 本工具源码已上传至github:https://github.com/ChildishChange/B ...

  4. 《Linux内核设计与实现》第18章读书整理

    第十八章.调试 18.1 准备开始 如果bug能重现的话,将会有很大的帮助. 18.2 内核中的bug Bug多种多样,产生的原因可以有无数的原因,表象也变化多端. 从隐藏在源代码中的错误到展现在目击 ...

  5. Daily Scrum 10.22

    (写于10.22周四0晨) 昨天任务还未完成的继续完成任务. 每个人都查看自己的TFS,修改已经完成的任务状态,改为已关闭-已完成. 由于android studio运行过于慢,我们统一采取eclip ...

  6. 回忆--RYU流量监控

    RYU流量监控 前言 Ryu book上的一个流量监控的应用,相对比较好看懂 实验代码 github源码 from ryu.app import simple_switch_13 from ryu.c ...

  7. Beta吐槽

    目录 感想 管理团队 推进项目 pm吐槽 开发 经过我慎重的考虑,我jio得,Beta版本的吐槽还是不能少. 感想 管理团队 这学期十分庆幸成为我们这个这么优秀的团队的pm.身在pm的位置上经历这么一 ...

  8. OneZero第四周第二次站立会议(2016.4.12)

    1. 时间: 13:00--13:10  共计10分钟. 2. 成员: X 夏一鸣 * 组长 (博客:http://www.cnblogs.com/xiaym896/), G 郭又铭 (博客:http ...

  9. ELK之消息队列选择redis_kafka_rabbitmq

    前言描述 生产初级,Service服务较少,访问量较少,随着业务量的不断增加,日志量成倍增长,然后就遇到了消息队列redis被充爆,不能满足应用的情况.针对此情况,我们来分析下可用的消息多列. 官方推 ...

  10. Spring之jdbcTemplate:增删改

    JdbcTemplate增删改数据操作步骤:1.导入jar包:2.设置数据库信息:3.设置数据源:4.调用jdbcTemplate对象中的方法实现操作 package helloworld.jdbcT ...