解题报告:有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. CentOS-7.x Yum Repo Mirror

    一. 环境 1.1 主机信息 主机 OS Storage 备注 100.64.140.101 centos 7.6 /dev/sdb > 100GB 1.selinux disable; 2.放 ...

  2. OpenState: Programming Platform-independent Stateful OpenFlow Applications Inside the Switch

    文章名称:OpenState: Programming Platform-independent Stateful OpenFlow Applications Inside the Switch Op ...

  3. 调研ios开发环境的演变

    一:ios的发展演变: 以下两句为百度百科IOS,可自行查阅,不多赘述,就Ctrl+c,Ctrl+v两句表示一下. 2007年1月9日苹果公司在Macworld展览会上公布,随后于同年的6月发布第一版 ...

  4. Alpha冲刺——day3

    Alpha冲刺--day3 作业链接 Alpha冲刺随笔集 github地址 团队成员 031602636 许舒玲(队长) 031602237 吴杰婷 031602220 雷博浩 031602634 ...

  5. Windows 下 Docker 的简单学习使用过程之一 dockertoolbox

    1. Windows 下面运行 Docker 的两个主要工具1): Docker for Windows2): DockerToolbox区别:Docker For Windows 可以理解为是新一代 ...

  6. 2013长春网赛 1006 hdu 4764 Stone(巴什博弈)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4764 题意:Tang 和 Jiang 玩一个游戏,轮流写下一个数,Tang先手,第一次Tang只能写[ ...

  7. chrome-extension & inject.js

    chrome-extension & inject.js chrome-extension://gppongmhjkpfnbhagpmjfkannfbllamg/js/inject.js in ...

  8. Ryuji doesn't want to study 2018 徐州赛区网络预赛

    题意: 1.区间求 a[l]×L+a[l+1]×(L−1)+⋯+a[r−1]×2+a[r](L is the length of [ l, r ] that equals to r - l + 1) ...

  9. MT【194】又见和式变换

    (2007浙江省赛B卷最后一题)设$\sum\limits_{i=1}^{n}{x_i}=1,x_i>0,$求证:$n\sum\limits_{i=1}^n{x_i^2}-\sum\limits ...

  10. 【刷题】BZOJ 4289 PA2012 Tax

    Description 给出一个N个点M条边的无向图,经过一个点的代价是进入和离开这个点的两条边的边权的较大值,求从起点1到点N的最小代价.起点的代价是离开起点的边的边权,终点的代价是进入终点的边的边 ...