杭电-1102Constructing Roads(最小生成树)

『杭电1102』Constructing Roads Problem Description There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or 阅读详情

Constructing Roads

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18054    Accepted Submission(s): 6878


Problem Description
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.

We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
 

Input
The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.

Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
 

Output
You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.
 

Sample Input
3 0 990 692 990 0 179 692 179 0 1 1 2
 

Sample Output
179


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int map[110][110],per[110],m,n;
struct node {
	int x,y,dis;
}s[10000];
void init()
{
	for(int i=0;i<=n;i++)
	per[i]=i;
}
int find (int x)
{
	while(x!=per[x])
	x=per[x];
	
	return x;
}
bool join(int x,int y)
{
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy)
	{
		per[fx]=fy;
		return true;
	}
	return false;
}
bool cmp(node x,node y)
{
	return x.dis<y.dis;
}
int main()
{
	int a,b,i,j,k;
	while(scanf("%d",&n)!=EOF)
	{
		init();
		for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
		scanf("%d",&map[i][j]);
		
		scanf("%d",&m);
		for(i=0;i<m;i++)
		{
			scanf("%d%d",&a,&b);
			map[a][b]=map[b][a]=0;
		}
		int t=0;
		for(i=1;i<=n;i++)
		{
			for(j=i;j<=n;j++)
			{
				s[t].x=i;
				s[t].y=j;
				s[t++].dis=map[i][j];
			}
		}
		sort(s,s+t,cmp);
		int sum=0;
		for(i=0;i<t;i++)
		{
			if(join(s[i].x,s[i].y))
			sum+=s[i].dis;
		}
		printf("%d\n",sum);
	}
	return 0;
}




SECS/GEM协议库及开源代码 以下是在开发SECS/GEM协议过程中,在网络上搜集协议实现资源,希望对SECS/GEM协议感兴趣的同学能有所帮助。 开源代码 freesecs FREESECS 是一个开源的SEMI SECS协议库,采用C++语言实现,可运行于Linux, MacOS X, NetBSD。 实现协议:E37.1、E5 开源地址:freesecs download | SourceForge.net 下载地址:https://download.youkuaiyun.com/download/BT_LEO/24305429 阅读详情

相关推荐

【推荐算法】召回模型总结

【推荐算法】召回模型总结

m0_48086806的博客 6241

杭电 hdu 1102 Constructing Roads (最小生成树)

杭电  hdu   1102  Constructing Roads  (最小生成树) Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16548    Accepted Sub

thestarfish的博客 451

Source Insight 4.0舒适黑主题 仿心情看的舒服就行

Source Insight 4.0主题 仿心情看的舒服就行 source insight自带的主题怎么看怎么别扭,于是在试用全网各种主题配色后,在其中一个基础上自定义了c/c++的语法配色 截全图有点糊 函数定义 全局变量 局部变量 枚举变量引用 全局变量引用及结构体成员 宏定义引用 ----------------------后面会再把主题文件贴上来-------------- ...

weixin_43443402的博客 1303

杭电 1102 Constructing Roads 【最小生成树&&Kruskal】

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17411    Accepted Submission(s): 6617 Problem Description There ar

巧克力华夫脆 459

科研作图不发愁:手把手教你用PyMOL给蛋白-DNA互作图‘美颜’(附完整配色与标注方案)

本文详细介绍了如何使用PyMOL对蛋白-DNA互作结构图进行专业美化,包括从PDB文件加载、结构展示与标注、互作位点突出显示到高质量渲染输出的完整流程。通过具体案例(PDB ID:8SSS)演示了锌指结构域区分、关键残基标注和互作距离测量等实用技巧,帮助科研人员制作可直接用于期刊投稿的高质量示意图。

weixin_42523792的博客 552

GROMACS自由能景观图绘制全流程:从PCA分析到Origin 3D可视化(附避坑指南)

本文详细解析了使用GROMACS绘制自由能景观图的全流程,涵盖从PCA分析、数据预处理、自由能计算到最终在Origin中进行3D可视化的完整步骤。文章重点分享了数据处理与格式转换中的常见“避坑”指南,帮助研究者从分子动力学轨迹中有效提取并解读构象自由能信息,深化对生物大分子动力学过程的理解。

oauth7security的博客 309

杭电1102 Constructing Roads(最小生成树)

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14028    Accepted Submission(s): 5337 Problem Description There are

听自己心跳的声音 751

手把手教你用VCS+Verdi在Linux下仿真蜂鸟E203 RISC-V核(附波形调试技巧)

本文详细介绍了在Linux环境下使用VCS和Verdi工具链对蜂鸟E203 RISC-V核进行仿真验证的完整流程。从环境配置、工程结构解析到VCS编译优化和Verdi波形分析技巧,提供了实战性强的调试方法和性能优化策略,帮助工程师高效完成数字IC验证工作。

weixin_42531163的博客 415

【杭电oj】1102 - Constructing Roads(畅通工程,最小生成树)

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18366    Accepted Submission(s): 7018 Problem Description There

wyg1997 671

13、基因相似性度量的相关性及特征选择研究

本文研究了基因相似性度量和特征选择在生物学中的应用,详细介绍了GO术语相似性度量的多种方法,包括语义度量和路径长度度量,并分析了不同方法在酵母和人类数据集上的表现。研究发现,Avg-sum方法和基于路径长度的度量在多数情况下效果较好。此外,通过晚期辐射毒性预测的案例研究,探讨了特征选择的重要性及可能面临的过拟合和噪声干扰问题,并提出了相应的解决策略。

js777的博客 127

BP神经网络回归预测+SHAP可解释分析+新数据预测(MATLAB完整源码)

BP神经网络回归预测+SHAP可解释分析+新数据预测(MATLAB完整源码)

机器学习之心的博客,关注并私信文章链接,获取对应文章源码和数据。 390

Constructing Roads 最小生成树 杭电1102

Problem Description There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are...

qq_40835329的博客 180

LVGL hal disp(porting display)

LVGL本身是没有对接最终的显示框架的,所以到具体的某个平台上,需要自行porting。

Bigan(安)的博客 2097

【最小生成树+Prim】杭电 hdu 1102 Constructing Roads

/* THE PROGRAM IS MADE BY PYY */ /*----------------------------------------------------------------------------// Copyright (c) 2012 panyanyany All rights reserved. URL : http://acm.hdu.edu

panyanyany(飞雨幻沙) 1114

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

杭电1102Constructing Roads(kruskal)(最小生成树)

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18273    Accepted Submission(s): 6979 Problem Description There

mengxiang000000 1262

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

杭电oj-1102 Constructing Roads(最小生成树Kruskal算法)

题意: 给吃n个村庄,接下来是n行n列数据,例如第一行第二个数据代表第一个村庄和第二个村庄之间的距离。 输入q; 接下来q行代表哪两个村庄之间已经修好路,让你求,还需要最少修多长的路。 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1102 分析: 考查最小生成树,最小生成树有两种算法(Prim算法和Kruskal算法),我用的是Kru

zfldongcidaci的博客 532

杭电 1301 Jungle Roads 【最小生成树&&Kruskal】

Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5530    Accepted Submission(s): 3993 Problem Description The Head

巧克力华夫脆 428

杭电Hdu 1102 Constructing Roads

这道题也是一道经典最小生成树的题目,用的prim算法。贵在会方法,并且能准确读懂题,我就读了好久,我犹豫的地方比方说:城镇B,C之间相连有一条路,如果城镇A可以通向C,那它也能通向B。开始我把它理解成了,A必须连着B,之间有条路;也必须连着C,之间有条路,其实不是这样。A通向B或C任何一个城镇就相当于也通向了另一个城镇。这才是最小生成树的意思吗,下面就好弄了。 还有一个地方,就是正在建设的路,那

Zhongyou2010的专栏 571

杭电1102 Constructing Roads

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13518    Accepted Submission(s): 5128 Problem Description There are

bluedream1219的专栏 839

杭电OJ 1102(Constructing Roads)解题报告

Problem Description There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are conn

路漫漫 始于足下 691

HDU 1102 Constructing Roads

最小生成树系列,总算我也有道题能在杭电上做到第一了,感动啊 跟畅通一样,用prim就可以搞定 Title #include <iostream> using namespace std; #define inf 10000000 int Dis[110][110],sum; struct Edge { int point; int low...

a278002424的博客 182

【杭电】[1102]Constructing Roads

最小生成树模板题先以矩阵形式给了村庄与村庄之间道路的距离 然后再给出哪些村庄之间道路已经通了 此时把这两个村庄的距离改为0就好了

BoilTask Talk Show 536

杭电ACM1102——Constructing Roads

这题,简单的最小生成树的应用,只是输入的方式比较特殊。还会告诉你哪一些村庄是已经有路的。 输入的第一个数n,是村庄的数目,然后下面n * n 个数代表的是:第i行第j个就是村庄i和j之间的距离。 理解了这个,就很容易写出代码了。 下面是AC的代码: #include #include #include using namespace std; class data

XD 752

杭电1102Constructing Roads

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17154    Accepted Submission(s): 6512 Problem Description There

hpuzsk的博客 596

杭电1102--Constructing Roads(简单并查集)

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16938Accepted Submission(s): 6435 Problem Description There are N...

dixi7825的博客 150

hdu 1102__Constructing Roads (Prim算法)

链接:Constructing Roads Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17443    Accepted Submission(s): 6632 Pr

Han_zhenyu的博客 463
上一篇: poj-1789Truck History(最小生成树)
下一篇: 杭电-1301Jungle Roads(最小生成树)
Eric_keke
博客等级 码龄12年 37粉丝 221原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值