# 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 #include<cmath>
2 #include<cstdio>
3 #include<cstdlib>
4 #include<cstring>
5 #include<iostream>
6 #include<algorithm>
7 using namespace std;
8 const int maxn = 1e5 + 233;
9 int n, m;
10 int ans;
11 int na, nb;
12 int a[maxn], b[maxn], d[maxn], deg[maxn];
13 inline void Scan(int &x)
14 {
15     char c;
16     bool o = false;
17     while(!isdigit(c = getchar())) o = (c != '-') ? o : true;
18     x = c - '0';
19     while(isdigit(c = getchar())) x = x * 10 + c - '0';
20     if(o) x = -x;
21 }
22 inline bool rule(int x, int y)
23 {
24     return d[x] < d[y];
25 }
26 int main()
27 {
28     Scan(n), Scan(m);
29     for(int i = 1; i <= n; ++i) Scan(d[i]);
30     int x, y;
31     for(int i = 1; i <= m; ++i)
32     {
33         Scan(x), Scan(y);
34         ++deg[x], --deg[y];
35     }
36     for(int i = 1; i <= n; ++i)
37     {
38         if(deg[i] < 0) a[++na] = i;
39         if(deg[i] > 0) b[++nb] = i;
40         deg[i] = abs(deg[i]);
41     }
42     sort(a + 1, a + 1 + na, rule);
43     sort(b + 1, b + 1 + nb, rule);
44     int pos = 1;
45     for(int i = 1; i <= na; ++i)
46     {
47         while(deg[a[i]])
48         {
49             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]] = 0;
50             else deg[a[i]] -= deg[b[pos]], ans += deg[b[pos]] * abs(d[a[i]] - d[b[pos]]), ++pos;
51         }
52     }
53     printf("%d", ans);
54 }
posted @ 2017-05-24 14:59  草根柴鸡  阅读(349)  评论(0编辑  收藏  举报