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, ...
随机推荐
- pip版本降级
使用命令:python -m pip install pip==8.1.1 其中, -m参数的解释 run library module as a script (terminates option ...
- Windows下更换MAC地址
使用TMAC软件是最佳方案.官网地址:www.technitium.com
- MyBatis 3.5.2 新特性介绍
1.MyBatis 最新版本 3.5.2 发布 MyBatis最新版本是:3.5.2,发布时间是:2019年7月15日 2.MyBatis 3.5.2 新特征介绍 我们知道,MyBatis 是支持定制 ...
- jqGrid 日期格式化,只显示日期,去掉小时分
{name:'operateTime',index:'operateTime', formatter:"date", formatoptions: {newformat:'Y-m- ...
- 夯实Java基础系列3:一文搞懂String常见面试题,从基础到实战,更有原理分析和源码解析!
目录 目录 string基础 Java String 类 创建字符串 StringDemo.java 文件代码: String基本用法 创建String对象的常用方法 String中常用的方法,用法如 ...
- JavaScript自动播放背景音乐
问题描述 js控制audio自动播放音乐时报错: Uncaught (in promise) DOMException 我的报错之前的代码: <audio id="myaudio&qu ...
- 浏览器兼容性知识&测试计划&测试报告
浏览器兼容性知识&测试计划&测试报告 浏览器兼容性问题:网页或网站兼容性问题,因为不同浏览器使用内核及所支持的HTML(标准通用标记语言下的一个应用)等网页语言标准不同:以及用户客户端 ...
- JSP常规内容
1.forword和redirect的区别? forword是服务器请求资源,服务器直接读取URL,把目标地址URL响应读取出来,然后再把这些内容发送给浏览器.(特点是url和request sess ...
- Zuhair and Strings-祖海和字符串 CodeForce#1105B
题目链接:Zuhair and Strings 题目原文 Given a string
- Spring boot 官网学习笔记 - Using Spring Boot without the Parent POM,但是还要使用Parent POM提供的便利
If you do not want to use the spring-boot-starter-parent, you can still keep the benefit of the depe ...