POJ 2421 Constructing Roads(Kruskal算法)
题意:给出n个村庄之间的距离,再给出已经连通起来了的村庄。求把所有的村庄都连通要修路的长度的最小值。
思路:Kruskal算法
课本代码:
//Kruskal算法
#include<iostream>
using namespace std; int fa[120];
int get_father(int x){
return fa[x]=fa[x]==x?x:get_father(fa[x]);//判断两个节点是否属于一颗子树(并查集)
}
int main(){
int n;
int p[120][120];
while(scanf("%d",&n)!=EOF){
int i,j,k,m;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&p[i][j]);
for(i=0;i<n;i++) fa[i]=i;
scanf("%d",&m);
while(m--){
int a,b;
scanf("%d%d",&a,&b);
fa[get_father(a-1)]=get_father(b-1);// a 合并到 b
}
int ans=0;
for(k=1;k<=1000;k++)// kruskal 算法,这里每个长度都进循环,若加一个长度存在的标记,可以节省时间
for(i=0;i<n;i++)
for(int j=0;j<n;j++)
if(p[i][j]==k &&get_father(i)!=get_father(j)){
fa[fa[i]]=fa[j];//合并两颗子树(并查集)
ans+=k;
}
printf("%d\n",ans);
}
return 0;
}
代码2:加了mark标记长度是否存在,代码中-------------为添加部分
//Kruskal算法
#include<iostream>
using namespace std; int fa[120];
int mark[1010];//-------------标记长度是否存在
int get_father(int x){
return fa[x]=fa[x]==x?x:get_father(fa[x]);//判断两个节点是否属于一颗子树(并查集)
}
int main(){
int n;
int p[120][120];
while(scanf("%d",&n)!=EOF){
memset(mark,0,sizeof(mark));
int i,j,k,m;
for(i=0;i<n;i++)
for(j=0;j<n;j++){
scanf("%d",&p[i][j]);
mark[p[i][j]]=1;//-------------把当前的长度标记一下
}
for(i=0;i<n;i++) fa[i]=i;
scanf("%d",&m);
while(m--){
int a,b;
scanf("%d%d",&a,&b);
fa[get_father(a-1)]=get_father(b-1);// a 合并到 b
}
int ans=0;
for(k=1;k<=1000;k++)// kruskal 算法
if(mark[k]){// -------------------长度存在的时候,再进入这个循环
for(i=0;i<n;i++)
for(int j=0;j<n;j++)
if(p[i][j]==k &&get_father(i)!=get_father(j)){
fa[fa[i]]=fa[j];//合并两颗子树(并查集)
ans+=k;
}
}
printf("%d\n",ans);
}
return 0;
}
POJ 2421 Constructing Roads(Kruskal算法)的更多相关文章
- POJ 2421 Constructing Roads (Kruskal算法+压缩路径并查集 )
Constructing Roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19884 Accepted: 83 ...
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- poj 2421 Constructing Roads 解题报告
题目链接:http://poj.org/problem?id=2421 实际上又是考最小生成树的内容,也是用到kruskal算法.但稍稍有点不同的是,给出一些已连接的边,要在这些边存在的情况下,拓展出 ...
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...
- POJ - 2421 Constructing Roads 【最小生成树Kruscal】
Constructing Roads Description There are N villages, which are numbered from 1 to N, and you should ...
- POJ - 2421 Constructing Roads (最小生成树)
There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...
- POJ 2421 Constructing Roads
题意:要在n个城市之间建造公路,使城市之间能互相联通,告诉每个城市之间建公路的费用,和已经建好的公路,求最小费用. 解法:最小生成树.先把已经建好的边加进去再跑kruskal或者prim什么的. 代码 ...
- [kuangbin带你飞]专题六 最小生成树 POJ 2421 Constructing Roads
给一个n个点的完全图 再给你m条道路已经修好 问你还需要修多长的路才能让所有村子互通 将给的m个点的路重新加权值为零的边到边集里 然后求最小生成树 #include<cstdio> #in ...
- POJ 2421 Constructing Roads(最小生成树)
Description There are N villages, which are numbered from 1 to N, and you should build some roads su ...
随机推荐
- Failed to read artifact descriptor for org.apache.httpcomponents:httpmime:jar
额,在Stackoverflow上找到了一个答案: I had this in eclipse and did this which fixed it(even though my command l ...
- memcached在Java中的应用以及magent的配置-每天进步一点点
memcached在Java中的应用: http://nhy520.iteye.com/blog/1775893 magent配置memcached分布式集群的应用: http://www.jians ...
- memcached使用总结
我的linux版本信息:Linux version 4.4.0-78-generic (buildd@lgw01-11) (gcc version 5.4.0 20160609 (Ubuntu 5.4 ...
- 使用Istio治理微服务入门
近两年微服务架构流行,主流互联网厂商内部都已经微服务化,初创企业虽然技术积淀不行,但也通过各种开源工具拥抱微服务.再加上容器技术赋能,Kubernetes又添了一把火,微服务架构已然成为当前软件架构设 ...
- 【文献阅读】Self-Normalizing Neural Networks
Self-Normalizing Neural Networks ,长达93页的附录足以成为吸睛的地方(给人感觉很厉害), 此paper提出了新的激活函数,称之为 SELUs ,其具有normaliz ...
- Windows下安装redis和在php中使用phpredis扩展
详细博客地址:https://my.oschina.net/junn/blog/281058
- python 迭代器,生成器与推导式
函数的动态传参 *args 动态接收所有位置参数 **kwargs 动态接收关键字参数 顺序: 位置参数, *args, 默认参数, **kwargs def func(*args, **kwargs ...
- CoreMotion 加速器陀螺仪
初始化CoreMotion #import <CoreMotion/CoreMotion.h> CMMotionManager *motionManager = [[CMMotionMan ...
- spring 事物管理
示例:模拟实现转账操作,"A"转给"B"1000,"A"少1000而"B"多一千. 一.转账环境搭建 1.xml配置文件 ...
- make编译一
在C和C++中,首先要把源文件编译成中间代码文件,在windows下就是obj文件,linux下就是.o文件:object file.这个动作叫做编译,然后再把大量的object file合成执行文件 ...