HDU 3371 Connect the Cities(并查集+Kruskal)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371
思路:
这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了。正是因为它先联通了几个点,所以为了判断连通性 很容易想到用并查集+kruskal。
不过要注意 这题有一个坑点,就是边数很多 上限是25000,排序的话可能就超时了。而点数则比较少 上限是500,所以很多人选择用Prim做。但我个人觉得这样连通性不好判断。其实边数多没关系,我们只要去重就好啦,用邻接矩阵存下两点间的最小权重 再排序即可,这样就不会超时啦~
代码:
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int inf=1e5;
struct node{
int v,u,w;
bool operator <(const node &x) const{
return w<x.w;
}
};
int pre[];
int mp[][];
int n,m,k;
int cnt;
vector<node>v;
void init(){
for(int i=;i<=n;i++){
pre[i]=i;
for (int j=; j<=n; j++) {
mp[i][j]=inf;
}
}
}
int query(int x){
int r=x;
while (pre[x]!=x) {
x=pre[x];
}
pre[r]=x;
return x;
}
bool join(int x,int y){
int fx=query(x);
int fy=query(y);
if(fx!=fy){
pre[fx]=fy;
return true;
}
return false;
}
int kruskal(){
int res=;
for(int i=;i<v.size();i++){
if(join(v[i].v, v[i].u)){
res+=v[i].w;
}
}
return res;
}
int main(){
int t;
int res;
scanf("%d",&t);
while (t--) {
cnt=;
v.clear();
scanf("%d%d%d",&n,&m,&k);
init();
for (int i=; i<m; i++) {
int v,u,w;
scanf("%d%d%d",&v,&u,&w);
mp[v][u]=min(mp[v][u],w);//保留最小权重
}
for (int i=; i<k; i++) {
int num,a,b;
scanf("%d",&num);
if(num) scanf("%d",&a);
for (int j=; j<num; j++) {
scanf("%d",&b);
join(a, b);
}
}
for (int i=; i<=n; i++) {
for (int j=; j<=n; j++) {
if(mp[i][j]==inf || i==j) continue;
v.push_back({i,j,mp[i][j]});//重新导入边,去掉了重复部分
}
}
sort(v.begin(), v.end());
res=kruskal();
for (int i=; i<=n; i++) {
if(pre[i]==i) cnt++;//算连通块个数
}
if(cnt==) printf("%d\n",res);
else printf("-1\n");
}
}
HDU 3371 Connect the Cities(并查集+Kruskal)的更多相关文章
- hdu 3371 Connect the Cities
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Description In 2100, since th ...
- HDU 3371 Connect the Cities(prim算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3371 Problem Description In 2100, since the sea leve ...
- hdu 3371 Connect the Cities(最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...
- hdu 2874 Connections between cities (并查集+LCA)
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- HDU 3371 Connect the Cities 最小生成树(和关于sort和qsort的一些小发现)
解题报告:有n个点,然后有m条可以添加的边,然后有一个k输入,表示一开始已经有k个集合的点,每个集合的点表示现在已经是连通的了. 还是用并查集加克鲁斯卡尔.只是在输入已经连通的集合的时候,通过并查集将 ...
- hdu 3371 Connect the Cities (最小生成树Prim)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...
- Hdu 3371 Connect the Cities(最小生成树)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 其实就是最小生成树,但是这其中有值得注意的地方:就是重边.题目没有告诉你两个城市之间只有一条路可走, ...
- hdu oj 3371 Connect the Cities (最小生成树)
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu 3635 Dragon Balls(并查集应用)
Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...
随机推荐
- python解释器安装和变量配置
python解释器安装 第一步https://www.python.org/ 下载 python-3.6.6 和 python-2.7.16 俩个版本 Downloads 下 选windows版本 4 ...
- linux中必备常用支持库的安装(CentOS)
在CentOS安装软件的时候,可能缺少一部分支持库,而报错.这里首先安装系统常用的支持库.那么在安装的时候就会减少很多的错误的出现 yum install -y gcc gdb strace gcc- ...
- SpringCloud-Hystrix Dashboard 之 Unable to connect to Command Metric Stream
实践hystrix dashboard仪表盘的时候,不管是按照书上的还是网上的,都提示Unable to connect to Command Metric Stream. 查了好久发现,如果使用sp ...
- JS 延时函数
function sleep(delay) { var start = (new Date()).getTime(); while((new Date()).getTime() - start < ...
- 2020Pycharm安装教程!最新2020pycharm安装!如何安装Pycharm2020版本!如何安装Pycharm!如何安装2020Pycharm!2020Pycharm永久激活!
本教程仅用作个人学习,请勿用于商业获利,造成后果自负!!! Pycharm安装 在这插一个小话题哈,Pycharm只是一个编译器,并不能代替Python,如果要使用Python,还是需要安装Pytho ...
- Spring 梳理 - filter、interceptor、aop实现与区别 -第二篇
spring mvc中的Interceptor可以理解为是Spring MVC框架对AOP的一种实现方式.一般简单的功能又是通用的,每个请求都要去处理的,比如判断token是否失效可以使用spring ...
- 【SQL server基础】objectproperty()函数
SQL Server OBJECTPROPERTY使用方法 OBJECTPROPERTY 返回有关当前数据库中的模式作用域对象的信息.此函数不能用于不是模式范围的对象,例如数据定义语言(DDL)触 ...
- Python常用端口扫描
from socket import * import sys host=sys.argv[1] service={':'HTTP', ':'SQL_Server', ':'Remote_Destop ...
- Linux之修改系统密码
目录 Linux之修改系统密码 参考 RHEL6修改系统密码 RHEL7修改系统密码 Linux之修改系统密码
- 主动降噪技术(ANC)的前生今世–行业分析
一 原始玩家 1978年,创始人Amar G. Bose博士在从欧洲飞往波士顿的飞机上,发现飞机引擎的噪音扫净了他戴耳机欣赏音乐的兴致.这激发了他在BOSE公司进行一项消噪科技的研究.弹指之间,十年过 ...