D - Constructing Roads - 2421
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<math.h>
#include<vector>
using namespace std; #define maxn 105 int f[maxn];
struct node
{
int u, v, len;
friend bool operator < (node a, node b)
{
return a.len > b.len;
}
};
int Find(int x)
{
if(f[x] != x)
f[x] = Find(f[x]);
return f[x];
} int main()
{
int N; while(scanf("%d", &N) != EOF)
{
int M, u, v;
node s;
priority_queue<node> Q; for(s.u=1; s.u<=N; s.u++)
{
f[s.u] = s.u;
for(s.v=1; s.v<=N; s.v++)
{
scanf("%d", &s.len);
Q.push(s);
}
} scanf("%d", &M); while(M--)
{
scanf("%d%d", &u, &v);
u = Find(u), v = Find(v);
f[u] = v;
} int ans = 0; while(Q.size())
{
s = Q.top();Q.pop();
u = Find(s.u), v = Find(s.v); if(u != v)
{
f[u] = v;
ans += s.len;
}
} printf("%d\n", ans);
} return 0;
}
D - Constructing Roads - 2421的更多相关文章
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- 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 (Kruskal算法+压缩路径并查集 )
Constructing Roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19884 Accepted: 83 ...
- Constructing Roads——F
F. Constructing Roads There are N villages, which are numbered from 1 to N, and you should build som ...
- Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)
Constructing Roads In JGShining's Kingdom HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...
- [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- HDU 1102 Constructing Roads
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Constructing Roads (MST)
Constructing Roads Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
随机推荐
- 500 OOPS: vsftpd: refusing to run with writable root inside chroot()解决方法
vsftpd.conf配置文件如下: [root@rusky ~]# cat /etc/vsftpd/vsftpd.conf | grep -v "#" anonymous_ena ...
- NYOJ 47过河问题
主要思路:先排序.有两种可能是最小的情况,一种是让最小的去带着最大的过去,然后最小的再回来,还有一种就是先最小的和第二小的一块过去, 然后最小的回来,让最大的和第二大的过去,接着第二小的回来,第二小和 ...
- python字符串replace()方法
python字符串replace()方法 >>> help(str.replace)Help on method_descriptor:replace(...) S.repla ...
- ASP.NET5 静态文件
静态文件,包括HTML文件,CSS文件,图像文件和JavaScript文件,它是一个应用里所包含的资源. 1. 提供静态文件 默认的,静态文件存储在你的webroot目录下面,webroot的路径定义 ...
- C# - 二叉树表达式计算
很早以前就写过双栈的表达式计算. 这次因为想深入学一下二叉树,网上都是些老掉牙的关于二叉树的基本操作. 感觉如果就学那些概念,没意思也不好记忆.于是动手写了一个表达式计算的应用例子. 这样学习印象才深 ...
- C#基础知识01(continue、break 和 return、ref 和 out)
break[跳出循环或者退出一个switch语句]由于它是用来退出循环或者switch语句的,所以只有当它出现在这些语句中时才是合法的. continue 语句和break语句相似,只是它不是退出一个 ...
- 一些iOS笔试题目
1.什么是arc?(arc是为了解决什么问题诞生的?) 首先解释ARC: automatic reference counting自动引用计数. ARC几个要点: 在对象被创建时 retain cou ...
- JS字符串常用方法
// 来自 http://www.runoob.com/js/js-strings.html var str01 = "odd open xboxone" , str02 ...
- Hive学习之二 《Hive的安装之自定义mysql数据库》
由于MySQL便于管理,在学习过程中,我选择MySQL. 一,配置元数据库. 1.安装MySQL,采用yum方式. ①yum install mysql-server,安装mysql服务端,安装服 ...
- Log4Net使用指南(转)
转自:http://www.cnblogs.com/dragon/archive/2005/03/24/124254.html 声明:本文内容主要译自Nauman Leghari的Using log4 ...