Connect the Cities(hdu3371)并查集(附测试数据)
Connect the Cities
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7338 Accepted Submission(s): 2093
#include <iostream>
#include <cstdlib>
#include<algorithm>
using namespace std; int father[],Q; struct sum
{
int a;
int b;
int c;
}num[]; //路线数 bool cmp(const sum &x,const sum &y) //按长度从小到大快排,
{
return x.c<y.c;//原理有待研究
} int Find(int x) //找出祖先
{
while(x!=father[x])
x=father[x];
return x;
} void Union(int a,int b,int i)
{
if(a!=b)
{
father[a]=b;
Q+=num[i].c; //并入家族且把长度加上来
}
} int main()
{
int T,k,n,m,i,j,l,p,q,c,t;
int ss[];
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&k);
for(i=;i<=n;i++)
father[i]=i;
for(i=;i<m;i++)
scanf("%d%d%d",&num[i].a,&num[i].b,&num[i].c);
memset(ss,,sizeof(ss));
for(l=;l<k;l++)
{
scanf("%d",&t);
for(j=;j<t;j++)
scanf("%d",&ss[j]);
for(j=;j<t;j++)
{
if(Find(ss[])!=Find(ss[j]))
father[Find(ss[j])]=Find(ss[]);
}
memset(ss,,sizeof(ss));
}
sort(num,num+m,cmp);//排序
for(i=,Q=;i<m;i++)
{
Union(Find(num[i].a),Find(num[i].b),i);
}
for(i=,t=;t<&&i<=n;i++)
if(father[i]==i)
t++;
if(t==)
printf("-1\n");
else
printf("%d\n",Q);
}
return ;
}
/*
5
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
6 4 3
1 4 3
2 6 2
6 2 1
3 4 33
2 1 2
2 1 3
3 4 5 6
6 4 3
1 4 2
2 6 1
2 3 5
3 4 33
2 1 2
2 2 3
2 4 5
6 4 3
1 4 3
2 6 2
6 2 1
3 4 33
2 1 2
2 1 3
2 4 6*/
在网上找了Kruskal函数, 有待学习!
#include<stdio.h> //Kruskal函数
#include<algorithm>
using namespace std; typedef struct{
int u;
int v;
int w;
}Edge; const int EdgeNum=;
const int PointNum=; Edge E[EdgeNum];
int P[PointNum]; int union_find(int x) // 并查集
{
return P[x]==x? x : P[x]=union_find(P[x]);
} bool cmp(Edge a,Edge b){ return a.w<b.w; } int MST_Kruskal(int n,int m) // 传入点数和边数
{
int i,j,x,y,k=,sum=;
for(i=;i<n;++i)
{
for(j=;j<n;++j)
{
if(i==j) continue;
if(P[j]==i)
{
k++;
// printf("P[%d]=%d\n",j,i);
}
}
}
// printf("k=%d\n",k);
sort(E,E+m,cmp);
for(i=;k<n&&i<m;++i)
{
x=union_find(E[i].u);
y=union_find(E[i].v);
if(x!=y)
{
sum+=E[i].w;
P[x]=y;
k++;
}
}
if(k<n) return -;
return sum;
} int f[];
int solve(int n,int m,int k)
{
int i,j,t;
for(i=;i<n;++i)
{ // 初始化并查集
P[i]=i;
}
for(i=;i<k;++i){
scanf("%d",&t);
for(j=;j<t;++j)
{
scanf("%d",&f[j]);
f[j]--;
}
for(j=;j<t;++j)
P[union_find(f[j])]=union_find(f[j-]);
}
return MST_Kruskal(n,m);
} int main()
{
int t,n,m,k,i,p,q,c,ans;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&k);
for(i=;i<m;++i)
{
scanf("%d%d%d",&p,&q,&c);
p--;q--;
E[i].u=p;E[i].v=q;E[i].w=c;
}
ans=solve(n,m,k);
printf("%d\n",ans);
}
return ;
}
Connect the Cities(hdu3371)并查集(附测试数据)的更多相关文章
- Connect the Cities[HDU3371]
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- [hdu2874]Connections between cities(LCA+并查集)
题意:n棵树,求任意两点的最短距离. 解题关键:并查集判断两点是否位于一棵树上,然后求最短距离即可.此题可以直接对全部区间直接进行st表,因为first数组会将连接的两点的区间表示出来. //#pra ...
- HDU 3371 Connect the Cities(并查集+Kruskal)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先 ...
- PAT 1013 Battle Over Cities(并查集)
1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...
- TOJ 2815 Connect them (kruskal+并查集)
描述 You have n computers numbered from 1 to n and you want to connect them to make a small local area ...
- hdu 2874 Connections between cities (并查集+LCA)
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- 九度OJ 1325:Battle Over Cities(城市间的战争) (并查集)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:376 解决:132 题目描述: It is vitally important to have all the cities connect ...
- hdu-2874 Connections between cities(lca+tarjan+并查集)
题目链接: Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/327 ...
- 1013 Battle Over Cities (25分) DFS | 并查集
1013 Battle Over Cities (25分) It is vitally important to have all the cities connected by highways ...
随机推荐
- 利用koa实现mongodb数据库的增删改查
概述 使用koa免不了要操纵数据库,现阶段流行的数据库是mongoDB,所以我研究了一下koa里面mongoDB数据库的增删改查,记录下来,供以后开发时参考,相信对其他人也有用. 源代码请看:我的gi ...
- java打包jar后,使之一直在linux上运行,不随终端退出而关闭
nohup java -jar xxx.jar&
- Android v7包下Toolbar和ActionBarActivity实现后退导航效果
android.support.v7包下的ToolBar和ActionBarActivity,均自带后退导航按钮,只是要手动开启,让它显示出来.先来看看ToolBar,页面前台代码: <andr ...
- 在vue里面使用iVew框架
iVew框架的文档 https://www.iviewui.com/docs/guide/install 这里使用的是 npm 来安装,在项目下执行下面命令npm install iview -- ...
- POJ 2860
#include<iostream> #define MAXN 20 using namespace std; int a_1[MAXN]; int a_2[MAXN]; int main ...
- JavaScript -- Anchor
-----052-Anchor.html----- <!DOCTYPE html> <html> <head> <meta http-equiv=" ...
- JavaScript -- Window-Scroll
-----037-Window-Scroll.html----- <!DOCTYPE html> <html> <head> <meta http-equiv ...
- salesforce 零基础学习(六十三)Comparable实现Object列表数据的自定义排序
项目中通常有些需求为需要将某个sObject的数据列表按照某种规则排序显示到前台页面上,但是list上面的sort远远满足不了复杂的功能,此种情况需要自定义比较两个object大小的方法,所以需要创建 ...
- 到网上收集了一个“高大上”的CSS3登入表单和大家分享一下
要求 必备知识 基本了解CSS语法,初步了解CSS3语法知识. 开发环境 Adobe Dreamweaver CS6 演示地址 演示地址 预览截图(抬抬你的鼠标就可以看到演示地址哦): 制作步骤: 一 ...
- SVN Hooks的介绍及使用
阅读此篇文章你可以: 对SVN Hooks有一定的了解 获取两个最常用的SVN Hooks案例 SVN hooks介绍 Hooks 钩子,主要实现的功能就是在特定事件发生之前或者之后自动执行事先定义好 ...