【bzoj2753】[SCOI2012]滑雪与时间胶囊
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std; typedef long long LL; #define N 1000010 struct edge
{
LL to,next;
}e[N<<1];
LL head[N<<1];
LL cnt; struct Node
{
LL x,y,z;
}a[N<<1]; LL n,m;
LL x,y,z;
LL tot,ans; LL fa[N],vis[N],q[N]; LL h[N]; int find(LL x)
{
return fa[x]==x ? x : fa[x]=find(fa[x]);
} void link(LL x,LL y,LL z)
{
e[++cnt]=(edge){y,head[x]};
head[x]=cnt;
a[cnt]=(Node){x,y,z};
} int cmp(Node x,Node y)
{
return h[x.y]>h[y.y] || (h[x.y]==h[y.y] && x.z<y.z);
} void bfs()
{
queue<LL>q;
q.push(1);
vis[1]=1;
while (!q.empty())
{
LL now=q.front();
q.pop();
for (LL i=head[now];i;i=e[i].next)
{
LL t=e[i].to;
if (!vis[t])
{
q.push(t);
vis[t]=1;
tot++;
}
}
}
} int main()
{
scanf("%lld%lld",&n,&m);
for (LL i=1;i<=n;i++)
scanf("%lld",&h[i]);
for (LL i=1;i<=m;i++)
{
scanf("%lld%lld%lld",&x,&y,&z);
if (h[x]>=h[y])
link(x,y,z);
if (h[x]<=h[y])
link(y,x,z);
}
bfs();
printf("%lld ",tot+1);
for (LL i=1;i<=n;i++)
fa[i]=i;
sort(a+1,a+cnt+1,cmp);
for (LL i=1;i<=cnt;i++)
{
x=a[i].x;
y=a[i].y;
if (!vis[x] || !vis[y])
continue;
LL r1=find(x),r2=find(y);
if (r1!=r2)
fa[r1]=r2,ans+=a[i].z;
}
printf("%lld\n",ans);
return 0;
}
#include<algorithm>#include<iostream>#include<cstdlib>#include<cstring>#include<cstdio>#include<cmath>#include<queue>using namespace std;typedef long long LL;#define N 1000010struct edge{ LL to,next;}e[N<<1];LL head[N<<1];LL cnt;struct Node{ LL x,y,z;}a[N<<1];LL n,m;LL x,y,z;LL tot,ans;LL fa[N],vis[N],q[N];LL h[N];int find(LL x){ return fa[x]==x ? x : fa[x]=find(fa[x]);}void link(LL x,LL y,LL z){ e[++cnt]=(edge){y,head[x]}; head[x]=cnt; a[cnt]=(Node){x,y,z};}int cmp(Node x,Node y){ return h[x.y]>h[y.y] || (h[x.y]==h[y.y] && x.z<y.z);}void bfs(){ queue<LL>q; q.push(1); vis[1]=1; while (!q.empty()) { LL now=q.front(); q.pop(); for (LL i=head[now];i;i=e[i].next) { LL t=e[i].to; if (!vis[t]) { q.push(t); vis[t]=1; tot++; } } }}int main(){ scanf("%lld%lld",&n,&m); for (LL i=1;i<=n;i++) scanf("%lld",&h[i]); for (LL i=1;i<=m;i++) { scanf("%lld%lld%lld",&x,&y,&z); if (h[x]>=h[y]) link(x,y,z); if (h[x]<=h[y]) link(y,x,z); } bfs(); printf("%lld ",tot+1); for (LL i=1;i<=n;i++) fa[i]=i; sort(a+1,a+cnt+1,cmp); for (LL i=1;i<=cnt;i++) { x=a[i].x; y=a[i].y; if (!vis[x] || !vis[y]) continue; LL r1=find(x),r2=find(y); if (r1!=r2) fa[r1]=r2,ans+=a[i].z; } printf("%lld\n",ans); return 0;}【bzoj2753】[SCOI2012]滑雪与时间胶囊的更多相关文章
- 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滑雪与时间胶囊(最小生成树)
首先显然可以把所有能到的点拎出来建个新图,这样第一问也就做好了. 剩下的部分似乎是一个裸的最小树形图.但显然这个东西是没什么学的必要的并且不太能跑过去. 考虑建出来的图有什么性质.可以发现如果没有高度 ...
- BZOJ2753 [SCOI2012]滑雪与时间胶囊 【kruskal】
题目链接 BZOJ2753 题解 完了我连\(kruskal\)裸题都做不出来了.. 题目是求最小树形图,即有向图最小生成树 我们不能直接上\(kruskal\),而要保证先加入前面的点, 所以我们排 ...
- [BZOJ2753][SCOI2012]滑雪与时间胶囊(特殊的有向树形图)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2753 分析: 第一问:直接BFS扩展知道无法扩展 第二问: 看似就是最小树形图啊= = ...
- 2019.01.17 bzoj2753: [SCOI2012]滑雪与时间胶囊(最小生成树)
传送门 最小生成树菜题. 题意:给出一些有向边,问有向的最小生成树. 思路:先dfsdfsdfs一把所有有用的边都存起来,然后按终点点权为第一关键字,边权为第二关键字给边排序保证最小生成树的合法性,排 ...
- BZOJ 2753 [SCOI2012] 滑雪和时间胶囊 最小生成树
题目链接: 题目 2753: [SCOI2012]滑雪与时间胶囊 Time Limit: 50 Sec Memory Limit: 128 MB 问题描述 a180285非常喜欢滑雪.他来到一座雪山, ...
- bzoj 2753: [SCOI2012]滑雪与时间胶囊 -- 最小生成树
2753: [SCOI2012]滑雪与时间胶囊 Time Limit: 50 Sec Memory Limit: 128 MB Description a180285非常喜欢滑雪.他来到一座雪山,这 ...
- 【BZOJ 2753】 2753: [SCOI2012]滑雪与时间胶囊 (分层最小树形图,MST)
2753: [SCOI2012]滑雪与时间胶囊 Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 2457 Solved: 859 Descriptio ...
随机推荐
- xshell连接Linux(centos6.8)失败的解决方法
注意:本人使用的是Centos6.8版本. Centos7以上版本linux命令会又不一样的地方. 启动xshell终端进行连接服务器:使用命令:ssh + ip地址或是手动使用可视化操作窗口操作, ...
- centos6.7升级python3.6.1
--安装依赖包 yum -y install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel ...
- Django框架基础知识10-内置分页系统
from django.shortcuts import render, redirect, reversefrom datetime import datetime# Create your vie ...
- 使用PL/SQL将sql脚本数据导入数据库
一. PL/SQL登录到数据库,使用tools工具进行导入.使用plsql登录到需要导入数据的数据库.点击工具栏上[tools]--[Import tables] 二.commit;
- HDU3785寻找大富翁~~真真切切的水题
寻找大富翁 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- 从Excel中读取数据(python-xlrd)
从Excel中读取数据(python-xlrd) 1.导入模块 import xlrd 2.打开Excel文件读取数据 data = xlrd.open_workbook('excelFile.xls ...
- ZOJ 2588 求割边问题
题目链接:http://vjudge.net/problem/viewProblem.action?id=14877 题目大意: 要尽可能多的烧毁桥,另外还要保证图的连通性,问哪些桥是绝对不能烧毁的 ...
- [CodePlus2017]晨跑
Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 166 Solved: 125 Description "无体育,不清华".&qu ...
- Minimum Spanning Tree.prim/kruskal(并查集)
开始了最小生成树,以简单应用为例hoj1323,1232(求连通分支数,直接并查集即可) prim(n*n) 一般用于稠密图,而Kruskal(m*log(m))用于系稀疏图 #include< ...
- Python从文件中读取字符串,用正则表达式匹配中文字符的问题
2013-07-27 21:01:37| 在Windows下,用Python从.txt文件中读取字符串,并用正则表达式匹配中文,在网上看了方法,用的时候发现中文没有被匹配. ...