Luogu 2573 [SCOI2012]滑雪
BZOJ 2753
首先可以按照题目要求的把所有的有向边建出来,然后进去广搜就可以求出第一问的解,然后考虑如何求解第二问,我们把所有搜到的边按照到达的点的高度位第一关键字,边的长度为第二关键字排序之后跑$kruskal$,这样子得到的最小生成树权值就是第二问所求的最大值。
考虑一下这样子为什么正确,首先“时间胶囊”的返回形式让答案一定是一棵生成树的权值,但是直接跑最小生成树可能会造成有一些点不能走到的情况,所以我们搜出所有的可能能到达的点,然后按照点的海拔高度先排序之后就一定能保证所有点都能走到,这样子求出来的生成树也一定是最小代价的。
时间复杂度$O(mlogm)$。
Code:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll; const int N = 1e5 + ;
const int M = 1e6 + ; int n, m, num = , tot = , head[N], edgeNum = , ufs[N];
ll h[N];
bool vis[N]; struct Edge {
int to, nxt;
ll val;
} e[M << ]; inline void add(int from, int to, ll val) {
e[++tot].to = to;
e[tot].val = val;
e[tot].nxt = head[from];
head[from] = tot;
} struct Pathway {
int u, v;
ll val; friend bool operator < (const Pathway &x, const Pathway &y) {
if(h[x.v] != h[y.v]) return h[x.v] > h[y.v];
else return x.val < y.val;
} } pat[M << ]; template <typename T>
inline void read(T &X) {
X = ; char ch = ; T op = ;
for(; ch > ''|| ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} inline void init() {
for(int i = ; i <= n; i++) ufs[i] = i;
} int find(int x) {
return ufs[x] == x ? x : ufs[x] = find(ufs[x]);
} queue <int> Q;
void bfs() {
Q.push();
vis[] = ; num = ;
for(; !Q.empty(); ) {
int x = Q.front(); Q.pop();
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
pat[++edgeNum] = (Pathway) {x, y, e[i].val};
if(!vis[y]) {
vis[y] = ; ++num;
Q.push(y);
}
}
}
} inline ll kruskal() {
init();
sort(pat + , pat + + edgeNum);
ll res = 0LL;
for(int cnt = , i = ; i <= edgeNum; i++) {
int u = find(pat[i].u), v = find(pat[i].v);
if(u == v) continue;
ufs[u] = v;
++cnt;
res += pat[i].val;
if(cnt >= num - ) break;
}
return res;
} int main() {
// freopen("3.in", "r", stdin); read(n), read(m);
for(int i = ; i <= n; i++) read(h[i]);
for(int i = ; i <= m; i++) {
int x, y; ll v;
read(x), read(y), read(v);
if(h[x] >= h[y]) add(x, y, v);
if(h[x] <= h[y]) add(y, x, v);
} bfs(); printf("%d %lld\n", num, kruskal());
return ;
}
Luogu 2573 [SCOI2012]滑雪的更多相关文章
- Bzoj2753 [SCOI2012]滑雪与时间胶囊
2753: [SCOI2012]滑雪与时间胶囊 Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 2282 Solved: 796 Descriptio ...
- 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 ...
- 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 ...
- bzoj 2753: [SCOI2012]滑雪与时间胶囊
Description a180285非常喜欢滑雪.他来到一座雪山,这里分布着M条供滑行的轨道和N个轨道之间的交点(同时也是景点),而且每个景点都有一编号i(1<=i<=N)和一高度Hi. ...
- [SCOI2012]滑雪与时间胶囊
题目描述 a180285非常喜欢滑雪.他来到一座雪山,这里分布着MMM条供滑行的轨道和NNN个轨道之间的交点(同时也是景点),而且每个景点都有一编号iii(1≤i≤N1 \le i \le N1≤i≤ ...
随机推荐
- LeetCode Split Concatenated Strings
原题链接在这里:https://leetcode.com/problems/split-concatenated-strings/description/ 题目: Given a list of st ...
- c# 统计运行时间
long startTime = Environment.TickCount; long endTime = Environment.TickCount; long totalTime = endTi ...
- 【spring源码学习】spring的AOP面向切面编程的实现解析
一:Advice(通知)(1)定义在连接点做什么,为切面增强提供织入接口.在spring aop中主要描述围绕方法调用而注入的切面行为.(2)spring定义了几个时刻织入增强行为的接口 => ...
- python操作rabbitmq、redis
1.启动rabbimq.mysql 在“”运行“”里输入services.msc,找到rabbimq.mysql启动即可 2.启动redis 管理员进入cmd,进入redis所在目录,执行redis- ...
- 处理mysql主从中断
主从同步中断跳过处理步骤: slave stop;set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;slave start; 在使用set global sql_slave_s ...
- Ubuntu-14.04-QT开发环境搭建-(一)
Ubuntu 14.04 QT 开发环境搭建 一 . 软件:qt-creator-linux-x86-opensource-2.7.0.binqt-everywhere-opensource-src- ...
- java工具类mht转html格式文件 及简单的HTML解析
package com.szy.project.utils; import java.io.BufferedInputStream; import java.io.BufferedOutputStre ...
- 验证DataGridView单元格的值
private void gridPurchaseOrderDetail_CellValidating(object sender, DataGridViewCellValidatingEventAr ...
- 2015 浙江省赛B Team Formation (技巧,动归)
Team Formation For an upcoming programming contest, Edward, the headmaster of Marjar University, is ...
- Java-Maven-Runoob:Maven Eclipse
ylbtech-Java-Maven-Runoob:Maven Eclipse 1.返回顶部 1. Maven Eclipse Eclipse 提供了一个很好的插件 m2eclipse ,该插件能将 ...