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, ...
随机推荐
- Uva 232 一个换行WA 了四次
由于UVA OJ上没有Wrong anwser,搞的多花了好长时间去测试程序,之前一直以为改OJ有WA,后来网上一搜才知道没有WA,哎哎浪费了好长时间.此博客用来记录自己的粗心大意. 链接地址:htt ...
- F#周报2019年第38期
新闻 宣告.NET Core 3.0第一个候选版本 .NET Core 3.0第一个候选版本中ASP.NET Core与Blazor的更新 F#的就业市场情形如何 Finalization实现细节 G ...
- EF指定更新字段
使用EF做更新时,若没有进行跟踪会默认全字段更新,那怎么做到只更新我们想要更新的字段呢? /// <summary> /// 修改指定属性的单条数据 /// </summary> ...
- 跟我学SpringCloud | 第二十章:Spring Cloud 之 okhttp
1. 什么是 okhttp ? okhttp 是由 square 公司开源的一个 http 客户端.在 Java 平台上,Java 标准库提供了 HttpURLConnection 类来支持 HTTP ...
- 洗牌Shuffle'm Up POJ-3087 模拟
题目链接:Shuffle'm Up 题目大意 模拟纸牌的洗牌过程,已知两个牌数相等的牌堆.求解经过多少次洗牌的过程,使牌的顺序与目标顺序相同. 思路 直接模拟,主要是字符串的操作.问题是,如何判断出不 ...
- 小红书第五章——引用类型之function类型
有趣的函数——function类型 函数实际上是对象,每个函数都是function类型的实例,具有属性和方法. 1.函数的定义方法 1)函数声明语法 function sum(num1,num2){/ ...
- Atm 测试
Account.java package ATM;//信1705-1 20173628 赵路仓 public class Account { private int balance;//余额 priv ...
- 【SQL server基础】初步学习存储过程(好学易懂)
-------------------------------------------------------------------------- ------------------------- ...
- 【ADO.NET基础】后台获取前台控件
C# 后台获取前台 input 文本框值.string aa=Request.Form[headself]; 那么要是后台给前台input文本框赋值呢? 后台 public string Headse ...
- 利用python爬虫关键词批量下载高清大图
前言 在上一篇写文章没高质量配图?python爬虫绕过限制一键搜索下载图虫创意图片!中,我们在未登录的情况下实现了图虫创意无水印高清小图的批量下载.虽然小图能够在一些移动端可能展示的还行,但是放到pc ...