P2573 [SCOI2012]滑雪 题解
下午花了三个小时肝这道题,心态差点爆炸!
下面是分析:
1 题目要求:
2 求最小生成树
3 但是
4 - a是从1号点开始的 --> 如果以后的某个点比一号高,则不可能到达
5 - a只能从高往低处滑
6 - 可能某两个景点没有轨道连接 -->没有处在一个联通快里?
7
8 生成树的要求:
9 - 树根高度最大
10 - 子节点的高度必须比父亲结点小
11 - 边权和尽可能小
12
13 自己造的样例:
14 输入:
15 4 4
16 4 2 3 1
17 1 2 3
18 2 3 2
19 2 4 4
20 3 4 1
21
22 输出:
23 3 7
从题解上爬的Solotion:
1 为保证我们只会由高到低,我们就只建立由高向低的单向边即可。
2
3 对于建立出来的图A,由1点开始宽搜,将扩展到的点和边加入一个新图B,
4 所有扩展到的点便是能到达的最多点。
5
6 我们再在这个新图上跑Kruskal求最小生成树,求得最短距离。
7
8 对于排序部分,为保证有尽可能多的点在最小生成树里,
9 我们按终点的高度为第一关键字从大到小排序,边长为第二关键字从小到大排序;
10
11 这样就能保证拓展的点最多,进而再用最小生成树求最短距离。
最终AC代码:
1 /*
2 Work By:Suzt_ilymtics
3 */
4 #include<iostream>
5 #include<cstdio>
6 #include<algorithm>
7 #include<queue>
8 using namespace std;
9 const int MAXN=1e5+5;
10 const int MAXM=1e6+6;
11 struct edge{
12 int from,to,nxt;
13 long long w;
14 }e_a[MAXM << 1],e_b[MAXM << 1];
15 int head_a[MAXN],num_edge_a;
16 int num_edge_b;
17 int n,m,cnt;
18 long long ans;
19 int h[MAXN],f[MAXN];
20 bool vis[MAXN];
21 queue<int> q;
22
23 int find(int x) {return f[x] == x ? x : f[x]=find(f[x]);}
24
25 bool cmp(edge x,edge y){return h[x.to] == h[y.to] ? x.w < y.w : h[x.to] > h[y.to];}
26
27 void add_a(int from,int to,int w){
28 e_a[++num_edge_a].from = from;
29 e_a[num_edge_a].to = to;
30 e_a[num_edge_a].w = w;
31 e_a[num_edge_a].nxt = head_a[from];
32 head_a[from] = num_edge_a;
33 }
34
35 void add_b(int from,int to,int w){
36 e_b[++num_edge_b].from = from;
37 e_b[num_edge_b].to = to;
38 e_b[num_edge_b].w = w;
39 }
40
41 void bfs(int x){
42 q.push(x);vis[1]=1;
43 while(!q.empty()){
44 int t=q.front(); q.pop();
45 for(int i=head_a[t];i;i=e_a[i].nxt){
46 add_b(e_a[i].from,e_a[i].to,e_a[i].w);
47 if(!vis[e_a[i].to]){
48 vis[e_a[i].to]=1;
49 q.push(e_a[i].to);
50 cnt++;
51 }
52 }
53 }
54 }
55
56 void kls(){
57 for(int i=1;i<=num_edge_b;++i){
58 int uf=find(e_b[i].from),vf=find(e_b[i].to);
59 if( uf == vf ) continue;
60 else{
61 f[uf] = vf;
62 ans+=e_b[i].w;
63 }
64 }
65 }
66
67 int main()
68 {
69 scanf("%d%d",&n,&m);
70 for(int i=1;i<=n;++i){
71 scanf("%d",&h[i]),f[i]=i;
72 }
73 for(int i=1,u,v,w;i<=m;++i){
74 scanf("%d%d%d",&u,&v,&w);
75 if(h[u]>=h[v]) add_a(u,v,w);
76 if(h[u]<=h[v]) add_a(v,u,w);
77 }
78 bfs(1);
79 sort(e_b+1,e_b+num_edge_b+1,cmp);
80 kls();
81 printf("%d %lld",cnt+1,ans);
82 return 0;
83 }
The end
(这篇文章终于能看了点)
P2573 [SCOI2012]滑雪 题解的更多相关文章
- P2573 [SCOI2012]滑雪
题目描述 a180285非常喜欢滑雪.他来到一座雪山,这里分布着 M 条供滑行的轨道和 N 个轨道之间的交点(同时也是景点),而且每个景点都有一编号 i ( 1≤i≤N )和一高度 Hi.a18028 ...
- BZOJ 2753 [SCOI2012] 滑雪和时间胶囊 最小生成树
题目链接: 题目 2753: [SCOI2012]滑雪与时间胶囊 Time Limit: 50 Sec Memory Limit: 128 MB 问题描述 a180285非常喜欢滑雪.他来到一座雪山, ...
- Bzoj2753 [SCOI2012]滑雪与时间胶囊
2753: [SCOI2012]滑雪与时间胶囊 Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 2282 Solved: 796 Descriptio ...
- Code[VS] 2152 滑雪题解
Code[VS] 2152 滑雪题解 题目描述 Description trs喜欢滑雪.他来到了一个滑雪场,这个滑雪场是一个矩形,为了简便,我们用r行c列的矩阵来表示每块地形.为了得到更快的速度,滑行 ...
- 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 ...
- BZOJ2753 SCOI2012 滑雪与时间胶囊 【最小生成树】*
BZOJ2753 SCOI2012 滑雪与时间胶囊 Description a180285非常喜欢滑雪.他来到一座雪山,这里分布着M条供滑行的轨道和N个轨道之间的交点(同时也是景点),而且每个景点都有 ...
- 2753: [SCOI2012]滑雪与时间胶囊
2753: [SCOI2012]滑雪与时间胶囊 Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 2633 Solved: 910 Descriptio ...
- bzoj 2753: [SCOI2012] 滑雪与时间胶囊 Label:MST
题目描述 a180285非常喜欢滑雪.他来到一座雪山,这里分布着M条供滑行的轨道和N个轨道之间的交点(同时也是景点),而且每个景点都有一编号i(1<=i<=N)和一高度Hi.a180285 ...
随机推荐
- java中远程调用接口springboot
package com.kakarote.crm.utils; import cn.hutool.core.util.ObjectUtil; import org.apache.http.client ...
- 利用MD5进行加密
package com.cn.peitest; import java.io.UnsupportedEncodingException; import java.security.MessageDig ...
- 对象存储Backblaze B2作为ShareX图床的Windows及安卓端配置
标题: 对象存储Backblaze B2作为ShareX图床的Windows及安卓端配置 作者: 梦幻之心星 sky-seeker@qq.com 标签: [对象存储,图床,Backblaze,Shar ...
- python之scrapy篇(二)
一.创建工程 scarpy startproject xxx 二.编写iteam文件 # -*- coding: utf-8 -*- # Define here the models for your ...
- Turtlebot3新手教程-应用-跟随
本文针对如何利用Turtlebot3可实现的各种应用进行讲解 具体步骤如下: [Remote PC]安装应用包 cd ~/catkin_ws/src git clone https://github. ...
- Turtlebot3新手教程:OpenCR软件设置(shell)
*本文针对如何利用脚本来更新固件进行讲解 具体步骤如下: burger的固件更新 $ export OPENCR_PORT=/dev/ttyACM0 $ export OPENCR_MODEL=bur ...
- 效率工具 | 快速创建虚拟机,Vagrant真香!
Vagrant 是一个基于Ruby的工具,主要用于创建和部署虚拟化开发环境.它以来于Oracle的开源VirtualBox虚拟化系统,通过使用 Chef创建自动化虚拟环境. Vagrant 主要的功能 ...
- express安装问题
步骤1 npm install -g express(全局安装express) (安装node就不必说了) 步骤2 npm install -g express-generator(安装命令工具) 完 ...
- win10中安装Linux子系统
前言 Win10的Linux子系统闻名已久,今天就来操作一下 正文 限制 该功能是win10 1809 及之后才加入的功能,故请先核对版本. 开启功能 打开windows设置 设置 -> 更新和 ...
- 【Azure Developer】使用Postman获取Azure AD中注册应用程序的授权Token,及为Azure REST API设置Authorization
Azure Active Directory (Azure AD) is Microsoft's cloud-based identity and access management service, ...