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, ...
随机推荐
- Chrome 谷歌浏览器安装使用 Postman 和 Sense 插件
博客地址:http://www.moonxy.com 一.前言 Google Chrome 的特点是简洁.快速等.Chrome 支持多标签浏览,每个标签页面都在独立的"沙箱"内运行 ...
- 基于python-django框架的支付宝支付案例
目录 @ 一. 开发前的准备 1. 必须了解的知识 SDK:软件开发工具包,可以为开发者提供快速开发的工具 沙箱环境:也就是测试环境 支付宝支付金额的精度:小数点后两位(面试) 支付宝用的什么加密方式 ...
- jdk1.8源码阅读
一.java.lang java的基础类 1.object 所有类的爸爸 registerNatives() Class<?> getClass():返回运行时的类 int hashCod ...
- Decorator:从原理到实践
前言 原文链接:Nealyang/personalBlog ES6 已经不必在过多介绍,在 ES6 之前,装饰器可能并没有那么重要,因为你只需要加一层 wrapper 就好了,但是现在,由于语法糖 c ...
- 【iOS 】把一些不太重要的任务放在空时执行
-(void)idleNotificationMethod { } -(void)registerForIdleNotification { [[NSNotificationCenter defaul ...
- oracle异机恢复测试
(一)问题背景 最近在生产环境中,开发人员误操作,使用truncate将oracle数据库某个表的数据全部删除了,在删除之后,开发人员发现自己闯祸了,于是联系值班的DBA进行紧急数据恢复. 经过分析, ...
- Android Studio [ImageView/使用第三方库加载图片]
ImageViewActivity.class package com.xdw.a122; import android.support.v7.app.AppCompatActivity; impor ...
- 正睿国庆DAY2动态规划专题
正睿国庆DAY2动态规划专题 排列-例题 1~n 的排列个数,每个数要么比旁边两个大,要么比旁边两个小 \(f[i][j]\) 填了前i个数,未填的数有\(j\)个比第\(i\)个小,是波峰 \(g[ ...
- 排坑日记之批量从库IO进程停止
早上刚睁眼,看到了一堆数据库告警的短信,其中一个内容如下: Problem started at 05:02:58 on 2019.10.12 Problem name: Slave is stopp ...
- MyBatis详解 一篇就够啦
第1章MyBatis框架配置文件详解 1.1 typeHandlers类型转换器 每当MyBatis 设置参数到PreparedStatement 或者从ResultSet 结果集中取得值时,就会使用 ...