BZOJ2753 [SCOI2012]滑雪与时间胶囊 【kruskal】
题目链接
题解
完了我连\(kruskal\)裸题都做不出来了。。
题目是求最小树形图,即有向图最小生成树
我们不能直接上\(kruskal\),而要保证先加入前面的点,
所以我们排序的时候第一关键字改为高度即可
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 1000005,maxm = 2000005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int high[maxn],n,m;
int h[maxn],ne;
struct EDGE{int from,to,nxt; LL w;}ed[maxm];
inline void build(int u,int v,LL w){
ed[++ne] = (EDGE){u,v,h[u],w}; h[u] = ne;
}
int tot;
struct edge{int a,b; LL w;}e[maxm];
inline bool operator <(const edge& a,const edge& b){
return high[a.b] == high[b.b] ? a.w < b.w : high[a.b] > high[b.b];
}
int vis[maxn],q[maxn],head,tail,N;
void bfs(){
q[head = tail = 0] = 1; vis[1] = true; N = 1;
int u;
while (head <= tail){
u = q[head++];
Redge(u) if (!vis[to = ed[k].to]){
N++; vis[to] = true; q[++tail] = to;
}
}
}
int pre[maxn];
int find(int u){return u == pre[u] ? u : pre[u] = find(pre[u]);}
void kruskal(){
for (int i = 1; i <= n; i++) pre[i] = i;
LL ans = 0; int cnt = N,fa,fb;
sort(e + 1,e + 1 + tot);
for (int i = 1; i <= tot && cnt > 1; i++){
fa = find(e[i].a); fb = find(e[i].b);
if (fa != fb){
pre[fb] = fa;
cnt--;
ans += e[i].w;
}
}
printf("%d %lld\n",N,ans);
}
int main(){
n = read(); m = read();
for (int i = 1; i <= n; i++) high[i] = read();
int a,b,w;
for (int i = 1; i <= m; i++){
a = read(); b = read(); w = read();
if (high[a] >= high[b]) build(a,b,w);
if (high[b] >= high[a]) build(b,a,w);
}
bfs();
for (int i = 1; i <= ne; i++){
if (vis[ed[i].from] && vis[ed[i].to])
e[++tot] = (edge){ed[i].from,ed[i].to,ed[i].w};
}
kruskal();
return 0;
}
BZOJ2753 [SCOI2012]滑雪与时间胶囊 【kruskal】的更多相关文章
- BZOJ2753 SCOI2012 滑雪与时间胶囊 【最小生成树】*
BZOJ2753 SCOI2012 滑雪与时间胶囊 Description a180285非常喜欢滑雪.他来到一座雪山,这里分布着M条供滑行的轨道和N个轨道之间的交点(同时也是景点),而且每个景点都有 ...
- Bzoj2753 [SCOI2012]滑雪与时间胶囊
2753: [SCOI2012]滑雪与时间胶囊 Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 2282 Solved: 796 Descriptio ...
- bzoj2753[SCOI2012]滑雪与时间胶囊 最小生成树
Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 2843 Solved: 993[Submit][Status][Discuss] Descripti ...
- [BZOJ2753][SCOI2012]滑雪与时间胶囊(特殊的有向树形图)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2753 分析: 第一问:直接BFS扩展知道无法扩展 第二问: 看似就是最小树形图啊= = ...
- 2019.01.17 bzoj2753: [SCOI2012]滑雪与时间胶囊(最小生成树)
传送门 最小生成树菜题. 题意:给出一些有向边,问有向的最小生成树. 思路:先dfsdfsdfs一把所有有用的边都存起来,然后按终点点权为第一关键字,边权为第二关键字给边排序保证最小生成树的合法性,排 ...
- BZOJ2753 SCOI2012滑雪与时间胶囊(最小生成树)
首先显然可以把所有能到的点拎出来建个新图,这样第一问也就做好了. 剩下的部分似乎是一个裸的最小树形图.但显然这个东西是没什么学的必要的并且不太能跑过去. 考虑建出来的图有什么性质.可以发现如果没有高度 ...
- BZOJ 2753 [SCOI2012] 滑雪和时间胶囊 最小生成树
题目链接: 题目 2753: [SCOI2012]滑雪与时间胶囊 Time Limit: 50 Sec Memory Limit: 128 MB 问题描述 a180285非常喜欢滑雪.他来到一座雪山, ...
- CDOJ 42/BZOJ 2753 滑雪与时间胶囊 kruskal
2753: [SCOI2012]滑雪与时间胶囊 Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 1376 Solved: 487[Submit][St ...
- 2753: [SCOI2012]滑雪与时间胶囊
2753: [SCOI2012]滑雪与时间胶囊 Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 2633 Solved: 910 Descriptio ...
随机推荐
- HDSF读写文件
HDFS 读取文件 HDFS的文件读取原理,主要包括以下几个步骤: 1.首先调用FileSystem对象的open方法,其实获取的是一个DistributedFileSystem的 实例. 2.D ...
- react-router 4.0中跳转失灵
在https://github.com/ReactTraining/history文档中,跳转是 用这种方法,但是,用了之后就存在这么一个问题,网址换了但是页面并没有刷新. 查了资料后,history ...
- 隐式Dijkstra:在状态集合中用优先队列求前k小
这种技巧是挺久以前接触的了,最近又突然遇到几道新题,于是总结了一下体会. 这种算法适用的前提是,标题所述的"状态集合"大到不可枚举(否则枚举就行了qaq) ...
- ubuntu配置机器学习环境(一) ubuntu安装
第一部分:Ubuntu14.04安装 Step :安装Ubuntu Step 1.1:准备安装U盘 首先到官网下载Ubuntu的镜像,我使用的是Ubuntu 14.04.3的ISO. 然后使用Ultr ...
- 4 class类 web服务器
1.换行符 2.pycharm 连接Ubuntu 1)添加环境变量 2)查看ip 3)配置目录 4)上传或者下载 3.面向对象抽象web服务器 1)版本1:类 class HttpServer(obj ...
- idea离线安装lombock插件
技术交流群:233513714 安装过程 1.首先找到插件包 插件包可以在两个地方下载,分别是IDEA的官方插件仓库和GitHub里lombok-intellij-plugin仓库中的release包 ...
- 创龙TMS320C6748开发板串口和中断学习笔记
1. 硬件上,底板有2个串口,UART1和UART2(使用了MAX3232电平转换芯片),其中UART2也可以转RS485的. 2. 看下数据手册部分,不过一直不理解过采样的意思,16字节的FIFO ...
- 虚拟接VMnet1 和VMnet8的区别
vmnet1是host-only,也就是说,选择用vmnet1的话就相当于VMware给你提供了一个虚拟交换机,仅将虚拟机和真实系统连上了,虚拟机可以与真实系统相互共享文件,但是虚拟机无法访问外部互联 ...
- MYSQL--事务处理(转)
事务处理在各种管理系统中都有着广泛的应用,比如人员管理系统,很多同步数据库操作大都需要用到事务处理.比如说,在人员管理系统中,你删除一个人员,你即需要删除人员的基本资料,也要删除和该人员相关的信息,如 ...
- 【APUE】Chapter1 UNIX System Overview
这章内容就是“provides a whirlwind tour of the UNIX System from a programmer's perspective”. 其实在看这章内容的时候,已经 ...