Connect the Cities

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16070    Accepted Submission(s): 4177

Problem Description
In 2100, since the sea level rise, most of the cities disappear.
Though some survived cities are still connected with others, but most of
them become disconnected. The government wants to build some roads to
connect all of these cities again, but they don’t want to take too much
money.  
 
Input
The first line contains the number of test cases.
Each
test case starts with three integers: n, m and k. n (3 <= n
<=500) stands for the number of survived cities, m (0 <= m <=
25000) stands for the number of roads you can choose to connect the
cities and k (0 <= k <= 100) stands for the number of still
connected cities.
To make it easy, the cities are signed from 1 to n.
Then follow m lines, each contains three integers p, q and c (0 <= c <= 1000), means it takes c to connect p and q.
Then
follow k lines, each line starts with an integer t (2 <= t <= n)
stands for the number of this connected cities. Then t integers follow
stands for the id of these cities.
 
Output
For each case, output the least money you need to take, if it’s impossible, just output -1.
 
Sample Input
1
6 4 3
1 4 2
2 6 1
2 3 5
3 4 33
2 1 2
2 1 3
3 4 5 6
 
Sample Output
1
 
题意:有n个城市,可以修其中的m条道路,然后还有k行,每一行有t个城市,这t个城市都已经连通。
题解:开始以为是普通的并查集,结果死活TLE,用了启发式合并才AC。而且就像很多人的题解里面说的用C++ AC不了。
启发式合并:启发式合并是为了解决合并过程中树退化成链的情况,用dep[i]表示根为i的树的最大深度,合并ra和rb时,采用最大深度小的向最大深度大的进行合 并,如果两棵树的最大深度一样,则随便选择一个作为根,并且将根的最大深度dep自增1,这样做的好处是在n次操作后,任何一棵集合树的最大深度都不会超过log(n),所以使得查找的复杂度降为O( log(n) )。
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int N = ;
const int M = ;
struct Edge
{
int s,e,len;
} edge[M];
int father[N],n,m,k;
int dep[N];
int _find(int x)
{
if(x==father[x])return x;
return _find(father[x]);
}
int cmp(Edge a,Edge b)
{
return a.len<b.len;
}
int kruskal(int m)
{
sort(edge+,edge+m+,cmp);
int cost = ;
for(int i=; i<=m; i++)
{
int x = _find(edge[i].s);
int y = _find(edge[i].e);
if(x!=y)
{
if(dep[x]==dep[y])
{
father[x] = y;
dep[y]++;
}
else if(dep[x]<dep[y])
{
father[x] = y;
}
else
{
father[y]=x;
}
cost += edge[i].len;
}
}
return cost;
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--)
{
scanf("%d%d%d",&n,&m,&k);
for(int i=; i<=n; i++)
{
father[i] = i;
dep[i] =;
}
for(int i=; i<=m; i++)
{
scanf("%d%d%d",&edge[i].s,&edge[i].e,&edge[i].len);
}
while(k--)
{
int t,a;
scanf("%d%d",&t,&a);
t--;
while(t--)
{
int b;
scanf("%d",&b);
int x = _find(a);
int y = _find(b);
if(x!=y)
{
if(dep[x]==dep[y])
{
father[x] = y;
dep[y]++;
}
else if(dep[x]<dep[y])
{
father[x] = y;
}
else
{
father[y]=x;
}
}
}
}
int ans = ;
int cost = kruskal(m);
for(int i=; i<=n; i++)
{
if(father[i]==i) ans++;
}
if(ans==)printf("%d\n",cost);
else printf("-1\n");
}
}
 

hdu 3371(启发式合并的最小生成树)的更多相关文章

  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 最小生成树(和关于sort和qsort的一些小发现)

    解题报告:有n个点,然后有m条可以添加的边,然后有一个k输入,表示一开始已经有k个集合的点,每个集合的点表示现在已经是连通的了. 还是用并查集加克鲁斯卡尔.只是在输入已经连通的集合的时候,通过并查集将 ...

  4. HDU - 6133 启发式合并

    题意:给出一棵树共\(n\)个顶点,每个顶点有一个权值\(val_i\),你需要对每个节点统计一个最优解 每个节点的解按照一定规则产生:取出该节点的子树下所有的顶点,把顶点任意排序成一个序列,设为\( ...

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

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

  6. HDU 5029 Relief grain(离线+线段树+启发式合并)(2014 ACM/ICPC Asia Regional Guangzhou Online)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5029 Problem Description The soil is cracking up beca ...

  7. [HDU 3712] Fiolki (带边权并查集+启发式合并)

    [HDU 3712] Fiolki (带边权并查集+启发式合并) 题面 化学家吉丽想要配置一种神奇的药水来拯救世界. 吉丽有n种不同的液体物质,和n个药瓶(均从1到n编号).初始时,第i个瓶内装着g[ ...

  8. 数据结构(trie,启发式合并):HDU 5841 Alice and Bob

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABJEAAAE6CAIAAAApz1RvAAAgAElEQVR4nO3d3css1b3g8fyTdbHJbD

  9. hdu 6133---Army Formations(启发式合并+树状数组)

    题目链接 Problem Description > Stormtroopers were the assault/policing troops of the Galactic Empire. ...

随机推荐

  1. 一个简单的同步集群的shell脚本

    编写一个xsync文件 然后放在/usr/local/bin 目录下面 xsync文件如下: #!/bin/bash #1 获取输入参数个数,如果没有参数,直接退出 pcount=$# if((pco ...

  2. 怎么用 copy 关键字?

    NSString.NSArray.NSDictionary等等经常使用copy关键字,是因为他们有对应的可变类型:NSMutableString.NSMutableArray.NSMutableDic ...

  3. HDU 4665 Unshuffle DFS找一个可行解

    每层找一对相等的整数,分别放在两个不同的串中. 参考了学弟的解法,果断觉得自己老了…… #include <cstdio> #include <cstring> #includ ...

  4. linux下的静态库和共享库

    转载&&增加:      我们在编写一个C语言程序的时候,经常会遇到好多重复或常用的部分,如果每次都重新编写固然是可以的,不过那样会大大降低工作效率,并且影响代码的可读性,更不利于后期 ...

  5. docker 生成新的镜像

    下载了ubuntu的初始化镜像,但是没有网络安装包,安装了字后,如果生成新的镜像 sudo docker commit -m "add ifconfig/ping package" ...

  6. 浅析Kerberos原理,及其应用和管理

    文章作者:luxianghao 文章来源:http://www.cnblogs.com/luxianghao/p/5269739.html  转载请注明,谢谢合作. 免责声明:文章内容仅代表个人观点, ...

  7. BZOJ4825 [Hnoi2017]单旋 【线段树】

    题目链接 BZOJ4825 题解 手模一下操作,会发现一些很优美的性质: 每次旋到根,只有其子树深度不变,剩余点深度\(+1\) 每次旋到根,[最小值为例]右儿子接到其父亲的左儿子,其余点形态不改变, ...

  8. 命令__shell数字-字符串比较

    shell常用逻辑判断 -b file 若文件存在且是一个块特殊文件,则为真 -c file 若文件存在且是一个字符特殊文件,则为真 -d file 若文件存在且是一个目录,则为真 -e file 若 ...

  9. sql或oracle插入数据时进行md5加密

    1.sql简单直接调用: SELECT hashbytes(') ; 2.oracle要复杂些 首写需要建函数: CREATE OR REPLACE FUNCTION MD5( passwd IN V ...

  10. echarts移动端字体模糊解决方法

    echarts使用canvas画图,在移动端使用rem时候,若viewport的scale被缩放,则字体会发生模糊,本人采用的解决方法是在不同的dpr下使用不同的字体大小,具体代码如下: 获取字体大小 ...