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 滑水的更多相关文章

  1. 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 ...

  2. bzoj 1658: [Usaco2006 Mar]Water Slides 滑水

    题解: 很神奇的做法,把点分成入度大于出度和入度小于出度两种. 然后入度大于出度的点必须走到某个点,所以排序贪心. #include<stdio.h> #include<iostre ...

  3. [Luogu2991][USACO10OPEN]水滑梯Water Slides

    题面戳我 题面描述 受到秘鲁的马丘比丘的新式水上乐园的启发,Farmer John决定也为奶牛们建一个水上乐园.当然,它最大的亮点就是新奇巨大的水上冲浪. 超级轨道包含 E (1 <= E &l ...

  4. BZOJ1916[USACO 2010 Open Gold 2.Water Slides]——DP+记忆化搜索

    题目描述 受到秘鲁的马丘比丘的新式水上乐园的启发,Farmer John决定也为奶牛们建 一个水上乐园.当然,它最大的亮点就是新奇巨大的水上冲浪.超级轨道包含 E (1 <= E <=15 ...

  5. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  6. noip知识点总结之--贪心

    一.什么是贪心 贪心算法嘛... 就是在对某个问题求解时,总是做出在当前看来是最好的选择 In other wors,并不是从整体最优上加以考虑,而是在获得某种意义上的局部最优解 二.贪心算法的适用前 ...

  7. 在ubuntu 上创建 ssl 证书

    soap webservice 调试工具: soap UI, 可以下载下来玩一玩. Introduction TLS, or transport layer security, and its pre ...

  8. https 自签名SSL证书

    介绍 TLS或称传输层安全性,及其前身SSL(代表安全套接字层)是用于将正常流量包装在受保护的加密包装中的Web协议. 使用这种技术,服务器可以在服务器和客户端之间安全地发送流量,而不会被外部各方拦截 ...

  9. 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 ...

随机推荐

  1. SummerVocation_Learning--java的线程机制

    线程:是一个程序内部的执行路径.普通程序只有main()一条路径.如下列程序: import java.lang.Thread; //导入线程实现包 public class Test_Thread ...

  2. Java 获取Web项目相对webapp地址

    例如, import java.io.File; import java.io.FileInputStream; import javax.servlet.http.HttpServletReques ...

  3. js数组中去重对象

    var allCourses = new Array();var coursesId = new Array();function findCourses() { Courses.data().eac ...

  4. 第八篇:ORM框架SQLAlchemy 了解知识

    一 介绍 SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作,简言之便是:将对象转换成SQL,然后使用数据API执行SQL并获取 ...

  5. Python基础:输入与输出(I/O)

    来做一个NLP任务 步骤为: 1.读取文件: 2.去除所有标点符号和换行符,并把所有大写变成小写: 3.合并相同的词,统计每个词出现的频率,并按照词频从大到小排序: 4.将结果按行输出到文件 out. ...

  6. jichu

    第 题 请编写函数 fun,其功能时:计算并输出当 x<0.97 时下列多项式的值,直到| sn-s(n-)|<0.000001 为止. Sn=+.5x+)/!x()+…+)()…..() ...

  7. Eclipse主题更换方法

    1.打开Eclipse的Help->Eclipse Marketplace 2.在Find里搜索Eclipse Color Theme,点击Install按钮 3.打开Window->Pr ...

  8. mysql插入、修改、删除

    联合查询: union:合并.联合,将多次查询结果合并成一个结果 语法: 查询语句1: union[all] 查询语句2: union [all] ... 意义 1.将一条比较复杂的查询语句可拆分成多 ...

  9. Vs2012 打开项目 自动关闭 并停止工作 解决方法

    来源:http://q.cnblogs.com/q/52530/ http://www.microsoft.com/zh-cn/download/details.aspx?id=36020 下载并安装 ...

  10. easyui datagrid复选框控制单选

    使用easyui datagrid的时候,由于对数据表格操作太多,并且有单选和多选功能因此采用复选框.但是在单选的状态,使用CheckOnSelect和singleselect时发现,页面有明显延迟, ...