hdu 3371(启发式合并的最小生成树)
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
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.
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.
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
#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(启发式合并的最小生成树)的更多相关文章
- hdu 3371 Connect the Cities(最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...
- hdu 3371 Connect the Cities (最小生成树Prim)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...
- HDU 3371 Connect the Cities 最小生成树(和关于sort和qsort的一些小发现)
解题报告:有n个点,然后有m条可以添加的边,然后有一个k输入,表示一开始已经有k个集合的点,每个集合的点表示现在已经是连通的了. 还是用并查集加克鲁斯卡尔.只是在输入已经连通的集合的时候,通过并查集将 ...
- HDU - 6133 启发式合并
题意:给出一棵树共\(n\)个顶点,每个顶点有一个权值\(val_i\),你需要对每个节点统计一个最优解 每个节点的解按照一定规则产生:取出该节点的子树下所有的顶点,把顶点任意排序成一个序列,设为\( ...
- HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑
这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...
- 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 ...
- [HDU 3712] Fiolki (带边权并查集+启发式合并)
[HDU 3712] Fiolki (带边权并查集+启发式合并) 题面 化学家吉丽想要配置一种神奇的药水来拯救世界. 吉丽有n种不同的液体物质,和n个药瓶(均从1到n编号).初始时,第i个瓶内装着g[ ...
- 数据结构(trie,启发式合并):HDU 5841 Alice and Bob
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABJEAAAE6CAIAAAApz1RvAAAgAElEQVR4nO3d3css1b3g8fyTdbHJbD
- hdu 6133---Army Formations(启发式合并+树状数组)
题目链接 Problem Description > Stormtroopers were the assault/policing troops of the Galactic Empire. ...
随机推荐
- elk-logstash: window下指定jdk目录
\bin\logstash.bat文件中, SETLOCAL的后面,CALL "%SCRIPT_DIR%\setup.bat" 的前面增加一行: @echo off SETLOCA ...
- Python学习-day18 Web框架
众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 ...
- (转\整)UE4游戏优化 多人大地型游戏的优化(三)GPU的优化
施主分享随缘,评论随心,@author:白袍小道 小道暗语: 1.因为小道这里博客目录没自己整,暂时就用随笔目录结构,所以二级目录那啥就忽略了.标题格式大致都是(原or转) 二级目录 (标题) 2.因 ...
- xcrun: error: active developer path
xcrun: error: active developer path ("/Applications/Xcode 2.app/Contents/Developer") does ...
- redis的socket event loop
很早之前就因为nosql就听说了redis,直到去年才真正去了解,只能说相见恨晚. 因为数据库相关,我以为这应该是个庞然大物,万万没想到,源码不到2M,所以,我不知道该说啥了... 还是来点靠谱的: ...
- Client does not support authentication protocol requested by server
关于由于版本号码不同而引起的 Client does not support authentication protocol requested by server 问题 搜索类似的问题,得到的答案类 ...
- HDU 2491
欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Priest John's Busiest Day Time Limit: 4000/2000 MS (Java/Others) ...
- Struts1 多个配置文件的实现
在Struts 1.0中,我们只能在web.xml中为ActionServlet指定一个配置文件,这对于我们这些网上的教学例子来说当然没什么问题,但是在实际的应用开发过程中,可能会有些麻烦.因为许多开 ...
- [洛谷P4346][CERC2015]ASCII Addition
题目大意:给一个像素的$a+b$,每个数字为$7\times5$的像素,每两个数字之间有间隔 题解:乱搞读入 卡点:无 C++ Code: #include <cstdio> #inclu ...
- [poj] 2618 popular cows
原题 这是一个强连通分量板子题. a thinks b is popular 即为a到b有一条边,要求被所有牛popular的牛的个数. 所求为对图进行强连通分量缩点后,没有出度的强连通分量里的点数( ...