BZOJ 1658 Water Slides 滑水
Water Slides 滑水
【问题描述】
It's a hot summer day, and Farmer John is letting Betsy go to the water park where she intends to ride every single slide. The water park has N (1 <= N <= 10,000) platforms (numbered 1..N) from which to enter the M (1 <= M <= 10,000) water slides. Each water slide starts at the top of some platform and ends at the bottom of some platform (possibly the same one). Some platforms might have more than one slide; some might not have any. The park is very thin, so the platforms lie along a straight line, each platform at a position Xi (0 <= Xi <= 100,000) meters from one end of the park. One walks from one platform to the next via a sidewalk parallel to the line of platforms.The platforms of the water park are weakly connected; that is, the park cannot be divided into two sets of platforms with no slides running between the two sets. Both the entrance and exit to the park are at platform 1, so Betsy will start and end there. In order to spend more time on the slides, Betsy wants to walk as little as possible. Find the minimum distance Betsy must travel along the ground in order to try every slide in the park exactly once without repeating.
炎热的夏日里,约翰带贝茜去水上乐园滑水.滑水是在一条笔直的人工河里进行的,沿河设有N(1≤N≤10000)个中转站,并开通了M(1≤M≤10000)条滑水路线.路线的起点和终点总在某个中转站上,起点和终点可能相同.有些中转站可能是许多条路线的起点或终点,而有些站则可能没有在任何路线里被用上.贝茜希望能把所有的路线都滑一遍.所有中转站排成一条直线,每个中转站位于离河的源头Xi(0≤Xi≤100000)米处.沿着河边的人行道,贝茜可以从任意位置走到任意一个中转站.中转站与滑水路线的布局满足下述的性质:任意两个中转站之间都有滑水路线直接成间接相连.水上乐园的入口与出口都在1号中转站旁,也就是说,贝茜的滑水路线的起点和终点都是1号中转站.
为了更好地享受滑水的快乐,贝茜希望自己花在走路上的时间越少越好.请你帮她计算一下,如果按她的计划,把所有的路线都不重复地滑一遍,那她至少要走多少路.
【输入格式】
* Line 1: Two integers, N and M.
* Lines 2..N+1: Line i+1 contains one integer, Xi, the position of platform i. * Lines N+2..M+N+1: Line i+N+1 contains two integers, Si and Di, respectively the start and end platforms of a slide.
第1行:两个整数N和M,用空格隔开.
第2到N+1行:第i+l行包括一个整数Xi,表示i号中转站距河源头的距离.
第N+2到M+N+1行:第i+N+1行包括两个整数Si和Di,分别表示了一条滑水路线的起点和终点.
【输出格式】
* Line 1: One integer, the minimum number of meters Betsy must walk.
输出一个整数,即贝茜要走的路程长度至少为多少米
【样例输入】
5 7
5
3
1
7
10
1 2
1 2
2 3
3 1
4 5
1 5
4 1
【样例输出】
8
题解:
由于图是连通的,所以不管贝茜按规则怎么滑,她都可以到达她没走过的中转站
那么统计每个中转站的出度与入度
把入度减去出度作为每个点的权值
就相当于每次能带1的值到其它点,使所有点权值为0的最小路程和
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1e5 + ;
int n, m;
int ans;
int na, nb;
int a[maxn], b[maxn], d[maxn], deg[maxn];
inline void Scan(int &x)
{
char c;
bool o = false;
while(!isdigit(c = getchar())) o = (c != '-') ? o : true;
x = c - '';
while(isdigit(c = getchar())) x = x * + c - '';
if(o) x = -x;
}
inline bool rule(int x, int y)
{
return d[x] < d[y];
}
int main()
{
Scan(n), Scan(m);
for(int i = ; i <= n; ++i) Scan(d[i]);
int x, y;
for(int i = ; i <= m; ++i)
{
Scan(x), Scan(y);
++deg[x], --deg[y];
}
for(int i = ; i <= n; ++i)
{
if(deg[i] < ) a[++na] = i;
if(deg[i] > ) b[++nb] = i;
deg[i] = abs(deg[i]);
}
sort(a + , a + + na, rule);
sort(b + , b + + nb, rule);
int pos = ;
for(int i = ; i <= na; ++i)
{
while(deg[a[i]])
{
if(deg[b[pos]] >= deg[a[i]]) ans += deg[a[i]] * abs(d[a[i]] - d[b[pos]]), deg[b[pos]] -= deg[a[i]], deg[a[i]] = ;
else deg[a[i]] -= deg[b[pos]], ans += deg[b[pos]] * abs(d[a[i]] - d[b[pos]]), ++pos;
}
}
printf("%d", ans);
}
BZOJ 1658 Water Slides 滑水的更多相关文章
- bzoj1658: [Usaco2006 Mar]Water Slides 滑水
Description It's a hot summer day, and Farmer John is letting Betsy go to the water park where she i ...
- bzoj 1658: [Usaco2006 Mar]Water Slides 滑水
题解: 很神奇的做法,把点分成入度大于出度和入度小于出度两种. 然后入度大于出度的点必须走到某个点,所以排序贪心. #include<stdio.h> #include<iostre ...
- [Luogu2991][USACO10OPEN]水滑梯Water Slides
题面戳我 题面描述 受到秘鲁的马丘比丘的新式水上乐园的启发,Farmer John决定也为奶牛们建一个水上乐园.当然,它最大的亮点就是新奇巨大的水上冲浪. 超级轨道包含 E (1 <= E &l ...
- BZOJ1916[USACO 2010 Open Gold 2.Water Slides]——DP+记忆化搜索
题目描述 受到秘鲁的马丘比丘的新式水上乐园的启发,Farmer John决定也为奶牛们建 一个水上乐园.当然,它最大的亮点就是新奇巨大的水上冲浪.超级轨道包含 E (1 <= E <=15 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- noip知识点总结之--贪心
一.什么是贪心 贪心算法嘛... 就是在对某个问题求解时,总是做出在当前看来是最好的选择 In other wors,并不是从整体最优上加以考虑,而是在获得某种意义上的局部最优解 二.贪心算法的适用前 ...
- 在ubuntu 上创建 ssl 证书
soap webservice 调试工具: soap UI, 可以下载下来玩一玩. Introduction TLS, or transport layer security, and its pre ...
- https 自签名SSL证书
介绍 TLS或称传输层安全性,及其前身SSL(代表安全套接字层)是用于将正常流量包装在受保护的加密包装中的Web协议. 使用这种技术,服务器可以在服务器和客户端之间安全地发送流量,而不会被外部各方拦截 ...
- Gym - 101670B Pond Cascade(CTU Open Contest 2017 贪心,二分)
题目: The cascade of water slides has been installed in the park recently and it has to be tested. The ...
随机推荐
- sendmail安装与配置
一.安装sendmail与mail 1.安装sendmail: 1) centos下可以安装命令:yum -y install sendmail 2) 安装完后启动sendmail命令:servic ...
- 使用vscode开发vue cli 3项目,配置eslint以及prettier
初始化项目时选择eslint-config-standard作为代码检测规范,vscode安装ESLint和Prettier - Code formatter两个插件,并进行如下配置 { " ...
- Mbps、Kbps、bps、kb、mb区别和换算
Mbps 即 Milionbit pro second(百万位每秒) Kbps 即 Kilobit pro second(千位每秒) bps 即 bit pro second(位每秒) 速度单位,bi ...
- hashlib模块常用功能
什么是hash hash是一种算法,该算法接受传入的内容,经过运算得到一串hash值 如果把hash算法比喻为一座工厂 那传给hash算法的内容就是原材料 生成的hash值就是生产出的产品 2.为何要 ...
- python 2.7版本解决TypeError: 'encoding' is an invalid keyword argument for this function
今天在用yaml处理数据时,由于yaml.load可接收一个byte字符串,unicode字符串,打开的二进制文件或文本文件对象,但字节字符串和文件必须是utf-8,utf-16-be或utf-16- ...
- python学习笔记(四):生成器、内置函数、json
一.生成器 生成器是什么?其实和list差不多,只不过list生成的时候数据已经在内存里面了,而生成器中生成的数据是当被调用时才生成呢,这样就节省了内存空间. 1. 列表生成式,在第二篇博客里面我写了 ...
- 【Isamaru, Hound of Honda】SVN常用命令补遗
一些常用的 就是svn commit的时候 都必须是最新版本的东西 不能不是,但是其实只是.svn在控制,所以可以update到最新版本再svn merge -r 20:10 将版本10和20的融合, ...
- TI C6000优化手册——让代码看起来像钉子
DSP芯片的出现,是为了解决大量的数字运算问题.通过集成专用的加法器.乘法器.地址产生器.复杂逻辑等硬件单元,DSP能实现比普通单片机更快速的数字运算,使处理器更适用于实时性高.复杂度强的处理场合.也 ...
- PowerShell批量启动/关闭Azure VM
备注:以下例子中出现的JohnsonWeb, JohnsonVm均是虚拟机的名称.在运行Powershell脚本之前,请导入您的订阅文件. 根据条件启动/关闭虚拟机,例如根据虚拟机名称,批量启动/关闭 ...
- 快速从mysqldump文件中恢复一个表
快速从较大的mysqldump文件中恢复一个表到数据库中: 1.先获取目标表(md_gas_check_record)在文件中的位置 [publish@LF-PRO-DB-01 ~]$ cat dby ...