hdu1102 Constructing Roads 基础最小生成树
//克鲁斯卡尔(最小生成树)
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std; const int maxn = ;
int n, t;
struct node
{
int bagin, end, len;
}arr[maxn];
int fa[maxn]; void init()
{
for (int i = ; i <= n; i++)
{
fa[i] = i;
}
} bool cmp(node a, node b)
{
return a.len<b.len;
} int find(int x)
{
if (x != fa[x])
fa[x] = find(fa[x]);
return fa[x];
} int main()
{
while (cin >> n)
{
t = ;
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n; j++)
{
int x;
cin >> x;
arr[t].bagin = i; arr[t].end = j; arr[t].len = x;
t++;
}
}
sort(arr, arr + t, cmp);
init();
int m;
cin >> m;
while (m--)
{
int a, b;
cin >> a >> b;
int fx, fy;
fx = find(a); fy = find(b);
fa[fy] = fx;
}
int ans = ;
for (int i = ; i<t; i++)
{
int fx, fy;
fx = find(arr[i].bagin);
fy = find(arr[i].end);
if (fx != fy)
{
ans += arr[i].len;
fa[fy] = fx;
}
}
cout << ans << endl;
}
return ;
}
hdu1102 Constructing Roads 基础最小生成树的更多相关文章
- hdu1102 Constructing Roads (简单最小生成树Prim算法)
Problem Description There are N villages, which are numbered from 1 to N, and you should build some ...
- POJ2421 & HDU1102 Constructing Roads(最小生成树)
嘎唔!~又一次POJ过了HDU错了...不禁让我想起前两天的的Is it a tree? orz..这次竟然错在HDU一定要是多组数据输入输出!(无力吐槽TT)..题目很简单,炒鸡水! 题意: 告 ...
- 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 ...
- hdu 1102 Constructing Roads (最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...
- hdu oj1102 Constructing Roads(最小生成树)
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Constructing Roads(最小生成树)
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU1102 Constructing Roads —— 最小生成树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题解: 纯最小生成树,只是有些边已经确定了要加入生成树中,特殊处理一下这些边就可以了. krus ...
- POJ 2421 Constructing Roads(最小生成树)
Description There are N villages, which are numbered from 1 to N, and you should build some roads su ...
随机推荐
- Linux下mount FreeBSD分区
假设须要从第二块硬盘复制文件.该硬盘格式化为UFS 2文件系统.怎样mount 由FreeBSD创建的UFS 2文件系统到Ubuntu系统上呢? UFS文件系统广泛的使用在不同的操作系统(比如:HP- ...
- DICOM-RT:放疗流程与參与角色
背景: 放疗是一个复杂的过程,同一时候须要肿瘤医师.模拟定位技师.剂量师.物理师.治疗技师.护士等多重角色參与.总体流程涉及到成像系统.定位系统.计划系统.治疗系统.质控QA系统.信息管理系统等多个独 ...
- CAShapeLayer的使用
CAShapeLayer的使用 1.CAShapeLayer 简介 1.CAShapeLayer继承至CALayer,可以使用CALayer的所有属性值 2.CAShapeLayer需要与贝塞尔曲线配 ...
- 安卓动态逆向分析工具--Andbug&Androguard
工具使用方法: 转自: http://bbs.pediy.com/showthread.php?t=183412 https://testerhome.com/topics/3542 安装andbug ...
- (27) java web的struts2框架的使用-基于表单的多文件上传
和单个文件上传配置都是一样的,只是在action中接受参数时候,接受的是数组,不再是单个的文件. 一,action的实现: public class MutableFilesUpload extend ...
- HDFS运维和优化
常见问题 下面列举HDFS运行过程中可能出现的常见问题及解决方法,这些问题一般都会在日志中出现的相应的记录.Incompatible clusterIDs in … :namenode cluster ...
- 关于谷歌浏览器默认字体12px的解决方案
1. * Chrome 中文界面下默认会将小于 12px 的文本强制按照 12px 显示, 可通过加入 CSS 属性 -webkit-text-size-adjust: none; 解决. 超链接访问 ...
- jQuery插件之ajaxFileUpload API文档
ajaxFileUpload是一个异步上传文件的jQuery插件. 语法:$.ajaxFileUpload([options]) options参数说明: 1.url 上传处理程序地址. 2,fil ...
- hdu 1166 敌兵布阵 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题目意思:给出 N 个数你,通过对某些数进行更改(或者 + 或者 -),当输入的是 Query ...
- 关闭ext4文件系统的日志功能
最近在帮一个研究生弄一个虚拟化环境下的基于Innodb的日志文件的读写优化的实验,实验的具体详细内容就不说了,其中有一个步骤需要将MySQL的日志文件放置在一块单独的硬盘里面,这块硬盘要么是ext2, ...