2753: [SCOI2012]滑雪与时间胶囊
2753: [SCOI2012]滑雪与时间胶囊
Time Limit: 50 Sec Memory Limit: 128 MB
Submit: 2633 Solved: 910
Description
Input
Output
Sample Input
3 2 1
1 2 1
2 3 1
1 3 10
Sample Output
HINT
【数据范围】
对于30%的数据,保证 1<=N<=2000
对于100%的数据,保证 1<=N<=100000
对于所有的数据,保证 1<=M<=1000000,1<=Hi<=1000000000,1<=Ki<=1000000000。
分析
分成两问,首先找出能到达的点(bfs),在算距离(kruskal),最小生成树时可以按高度为第一关键字,权值为第二关键字排序。先处理高的点,在处理低的点。
排序之后,我们首先拿到的是高度较高,权值较小的边,然后判断能不能到达这两个点(在搜索时处理),之后依次放高度较低的边(层层往下,直到最底层,什么也到不了)。高度相等的点不需要在考虑高度的影响,直接找最小的边,所以我们开始时先按高度在按权值排序,即分层最小生成树。
代码
#include<cstdio>
#include<algorithm>
#include<queue> using namespace std;
const int MAXN = ;
const int MAXM = ;
struct Edge{
int x,y;
long long w;
}e[MAXM];
struct Node{
int nxt,to;
}t[MAXM];
int head[MAXN],h[MAXN],fa[MAXN];
bool vis[MAXN];
int n,m,cnt,ans=;
long long sum;
queue<int>q; void add(int u,int v,long long w)
{
++cnt;
t[cnt].to = v;
t[cnt].nxt = head[u];
head[u] = cnt;
e[cnt] = (Edge){u,v,w};
}
int find(int x)
{
return x==fa[x]?x:fa[x]=find(fa[x]);
}
bool cmp(Edge a,Edge b) //先按高度排序,再按权值
{
return h[a.y]>h[b.y] || (h[a.y]==h[b.y] && a.w<b.w);
}
void bfs() //搜索出能够到达的点,和点的个数
{
q.push();
vis[] = true;
while (!q.empty())
{
int u = q.front();
q.pop();
for (int i=head[u]; i; i=t[i].nxt)
{
int v = t[i].to;
if (!vis[v])
{
q.push(v);
vis[v] = true;
ans++;
}
}
}
printf("%d",ans);
}
void init()
{
scanf("%d%d",&n,&m);
for (int i=; i<=n; ++i)
{
scanf("%d",&h[i]);
fa[i] = i; //初始化
}
for (int x,y,i=; i<=m; ++i)
{
long long z;
scanf("%d%d%lld",&x,&y,&z);
if (h[x]>=h[y]) add(x,y,z); //排序时,结构体中是小的
if (h[y]>=h[x]) add(y,x,z);
}
}
void work()
{
sort(e+,e+cnt+,cmp);
for (int i=; i<=cnt; ++i)
{
if (!vis[e[i].x] || !vis[e[i].y]) continue ; //不能到达,就continue
int rx = find(e[i].x);
int ry = find(e[i].y);
if (rx!=ry)
{
fa[rx] = ry;
sum += e[i].w;
}
}
printf(" %lld",sum);
}
int main()
{
init(); //初始化,输入
bfs(); //bfs求能到达的点
work(); //kruskal求距离
return ;
}
2753: [SCOI2012]滑雪与时间胶囊的更多相关文章
- 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 ...
- bzoj 2753: [SCOI2012] 滑雪与时间胶囊 Label:MST
题目描述 a180285非常喜欢滑雪.他来到一座雪山,这里分布着M条供滑行的轨道和N个轨道之间的交点(同时也是景点),而且每个景点都有一编号i(1<=i<=N)和一高度Hi.a180285 ...
- bzoj 2753: [SCOI2012]滑雪与时间胶囊
Description a180285非常喜欢滑雪.他来到一座雪山,这里分布着M条供滑行的轨道和N个轨道之间的交点(同时也是景点),而且每个景点都有一编号i(1<=i<=N)和一高度Hi. ...
- 【刷题】BZOJ 2753 [SCOI2012]滑雪与时间胶囊
Description a180285非常喜欢滑雪.他来到一座雪山,这里分布着M条供滑行的轨道和N个轨道之间的交点(同时也是景点),而且每个景点都有一编号i(1<=i<=N)和一高度Hi. ...
- Bzoj2753 [SCOI2012]滑雪与时间胶囊
2753: [SCOI2012]滑雪与时间胶囊 Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 2282 Solved: 796 Descriptio ...
- 【最小树形图(奇怪的kruskal)】【SCOI 2012】【bzoj 2753】滑雪与时间胶囊
2753: [SCOI2012]滑雪与时间胶囊 Time Limit: 50 Sec Memory Limit: 128 MB Submit: 1621 Solved: 570 Description ...
- BZOJ2753 SCOI2012 滑雪与时间胶囊 【最小生成树】*
BZOJ2753 SCOI2012 滑雪与时间胶囊 Description a180285非常喜欢滑雪.他来到一座雪山,这里分布着M条供滑行的轨道和N个轨道之间的交点(同时也是景点),而且每个景点都有 ...
随机推荐
- 【QT】Qaction和触发函数建立连接的方法
说明:我是在ui里面编辑好控件以及位置,然后在程序里面将控件和触发函数进行绑定,实现的触发操作. 代码如下: MainWindow::MainWindow(QWidget *parent) : QMa ...
- Struts2_Struts标签大致内容
Struts-Tags1.通用标签 a) property b) set i.默认为 action scope,会将值放入 request 和 ActionContext中 ii. page.requ ...
- [topcoder]SRM 647 DIV 2
第一题,送分题. 第二题, #include <vector> #include <algorithm> #include <map> #include <q ...
- HCNA管理设置文件系统FTP服务上传下载文件
1.拓扑图 2.R2配置 The device is running! ###################################### <Huawei>sys Enter s ...
- 封装win7系统、制作win7GHO镜像、制作一个自定义的镜像文件具体步骤、制作Win10镜像gho
作者:导演你让灰太狼吃只羊 来源:CSDN 原文:https://blog.csdn.net/qq_35057426/article/details/83015516 版权声明:本文为博主原创文章,转 ...
- ABAP Netweaver和Hybris Enterprise Commerce Platform的登录认证
ABAP Netweaver 在我的博客Learn more detail about Standard logon procedure里有详细介绍. Hybris ECP Hybris Admini ...
- Android(java)学习笔记62:android.intent.action.MAIN 与 android.intent.category.LAUNCHER 理解
1. 先看看网路上的说法: android.intent.action.MAIN 决定应用程序最先启动的 Activity android.intent.category.LAUNCHER 决定应用程 ...
- Delphi7 企业版安装记录
Borland Delphi Enterprise Version 7.0[Build 4.453] 云盘下载: 链接:http://pan.baidu.com/s/1gff6Fuz 密码:z ...
- Rich feature hierarchies for accurate object detection and semantic segmentation(RCNN)
https://zhuanlan.zhihu.com/p/23006190?refer=xiaoleimlnote http://blog.csdn.net/bea_tree/article/deta ...
- sz 命令
sz命令 下载文件命令 sz 文件名