【PA 2014】Kuglarz
【题目链接】
时间复杂度是O(n^2log(n)),而prim的时间复杂度是O(n^2)
#include<bits/stdc++.h>
using namespace std;
#define MAXN 2010 struct info
{
int u,v;
long long w;
} e[MAXN*MAXN]; int n,i,j,tot;
long long ans,c; bool cmp(info a,info b)
{
return a.w < b.w;
}
class DisjointSet
{
private :
int fa[MAXN];
public :
inline void init(int n)
{
int i;
for (i = ; i <= n; i++) fa[i] = i;
}
inline int get_root(int x)
{
if (fa[x] == x) return x;
return fa[x] = get_root(fa[x]);
}
inline void merge(int u,int v)
{
fa[get_root(u)] = get_root(v);
}
} s;
inline void kruskal()
{
int i,sx,sy;
for (i = ; i <= tot; i++)
{
sx = s.get_root(e[i].u);
sy = s.get_root(e[i].v);
if (sx != sy)
{
ans += e[i].w;
s.merge(e[i].u,e[i].v);
}
}
}
int main()
{ scanf("%d",&n);
for (i = ; i <= n; i++)
{
for (j = i; j <= n; j++)
{
scanf("%lld",&c);
e[++tot] = (info){i-,j,c};
}
}
s.init(n);
sort(e+,e+tot+,cmp);
kruskal();
printf("%lld\n",ans); return ;
}
【PA 2014】Kuglarz的更多相关文章
- 【AMPPZ 2014】 The Captain
[题目链接] 点击打开链接 [算法] 按x轴排序,将相邻点连边 按y轴排序,将相邻点连边 然后对这个图跑最短路就可以了,笔者用的是dijkstra算法 [代码] #include<bits/st ...
- 【JSOI 2014】序列维护
[题目链接] 点击打开链接 [算法] 线段树 注意标记下传 [代码] #include<bits/stdc++.h> using namespace std; #define MAXN 5 ...
- 【SDOI 2014】 旅行
[题目链接] 点击打开链接 [算法] 树链剖分 每个宗教建一棵线段树,注意数据量大,要动态开点 [代码] #include<bits/stdc++.h> using namespace s ...
- 【TJOI 2014】 上升子序列
[题目链接] 点击打开链接 [算法] 先考虑50分的做法 : f[i]表示以i结尾的本质不同的上升子序列的个数 则f[i] = sigma(f[j]) (j < i,a[j] < a[i] ...
- 【LNOI 2014】 LCA
[题目链接] 点击打开链接 [算法] 考虑求lca(x,y)的深度 我们可以将从根到x路径上的点都打上标记,然后,询问y到根上路径的权值和 那么,求sigma(depth(lca(i,z)))(l & ...
- 【JLOI 2014】 松鼠的新家
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3631 [算法] 树上差分 [代码] #include<bits/stdc++. ...
- 【NOI 2014】 动物园
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3670 [算法] KMP [代码] #include<bits/stdc++.h ...
- 【BZOJ 3876】【AHOI 2014】支线剧情
http://www.lydsy.com/JudgeOnline/problem.php?id=3876 这道题每条支线的意思是每条边... 那么每条边的下界设为1就行了. 这样建出一个DAG,每条边 ...
- 【UOJ #29】【IOI 2014】holiday
http://uoj.ac/problem/29 cdq四次处理出一直向左, 一直向右, 向左后回到起点, 向右后回到起点的dp数组,最后统计答案. 举例:\(fi\)表示一直向右走i天能参观的最多景 ...
随机推荐
- python实现tcp文件下载器
服务器端代码 import socket import os import threading # 处理客户端请求下载文件的操作(从主线程提出来的代码) def deal_client_request ...
- Github上600多个iOS开源项目分类及介绍
将Github上600多个iOS开源项目进行分类并且有相应介绍,小伙伴们快来看呀 地址:http://github.ibireme.com/github/list/ios/
- [转]如何阅读android framework源码
但如果想深入的了解Android系统, 那么可以看下我的一些简单的总结. 知识 Java Java是AOSP的主要语言之一. 没得说, 必需熟练掌握. 熟练的Android App开发 Linux A ...
- C++对拍数据生成
#include<map> #include<ctime> #include<queue> #include<cmath> #include<cs ...
- vagrant的学习 之 LNMP和LAMP
vagrant的学习 之 LNMP和LAMP 本文根据慕课网的视频教程练习,感谢慕课网! 慕课的参考文档地址:https://github.com/apanly/mooc/tree/master/va ...
- ***CSS总结-原创
CSS规则 第一个是:选择符.和jquery的选择器是一个意思大括号是:申明块里面KV是:属性和属性值分号是结束符 类别选择符(器) p.red的意思,就是对p标签中类为red的进行CSS作用 ID选 ...
- java服务器图片压缩的几种方式及效率比较
以下是测试了三种图片压缩方式,通过测试发现使用jdk的ImageIO压缩时间更短,使用Google的thumbnailator更简单,但是thumbnailator在GitHub上的源码已经停止维护了 ...
- noi.openjudge——8465 马走日
http://noi.openjudge.cn/ch0205/8465/ 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 1024kB 描述 马在中国象棋以日字形规则移动. 请编写 ...
- 高数(A)下 第十一章
11.1 11.2 11.3 11.4 11.5
- laravel 实时facade
实时facade 创建一个目录叫services 创建一个weibo类 <?php namespace App\Services; class weibo { protected $http; ...