Codeforces Round #352 (Div. 2) C - Recycling Bottles

C. Recycling Bottles
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

It was recycling day in Kekoland. To celebrate it Adil and Bera went to Central Perk where they can take bottles from the ground and put them into a recycling bin.

We can think Central Perk as coordinate plane. There are n bottles on the ground, the i-th bottle is located at position (xi, yi). Both Adil and Bera can carry only one bottle at once each. 

For both Adil and Bera the process looks as follows: 

  1. Choose to stop or to continue to collect bottles. 
  2. If the choice was to continue then choose some bottle and walk towards it. 
  3. Pick this bottle and walk to the recycling bin. 
  4. Go to step 1

Adil and Bera may move independently. They are allowed to pick bottles simultaneously, all bottles may be picked by any of the two, it's allowed that one of them stays still while the other one continues to pick bottles.

They want to organize the process such that the total distance they walk (the sum of distance walked by Adil and distance walked by Bera) is minimum possible. Of course, at the end all bottles should lie in the recycling bin.

Input

First line of the input contains six integers axaybxbytx and ty (0 ≤ ax, ay, bx, by, tx, ty ≤ 109) — initial positions of Adil, Bera and recycling bin respectively.

The second line contains a single integer n (1 ≤ n ≤ 100 000) — the number of bottles on the ground.

Then follow n lines, each of them contains two integers xi and yi (0 ≤ xi, yi ≤ 109) — position of the i-th bottle.

It's guaranteed that positions of Adil, Bera, recycling bin and all bottles are distinct.

Output

Print one real number — the minimum possible total distance Adil and Bera need to walk in order to put all bottles into recycling bin. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.

Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct if .

Examples
input
3 1 1 2 0 0
3
1 1
2 1
2 3
output
11.084259940083
input
5 0 4 2 2 0
5
5 2
3 0
5 5
3 5
3 3
output
33.121375178000
Note

Consider the first sample.

Adil will use the following path: .

Bera will use the following path: .

Adil's path will be  units long, while Bera's path will be  units long.

题意:

告诉你a和b两人的坐标和垃圾桶t的坐标,两人捡垃圾,有n个垃圾,分别给出坐标。每次只能捡一项,每次捡到垃圾后需上交到垃圾桶处,之后可选择继续捡垃圾或休息。

垃圾必须捡完,问你捡完所有垃圾,a和b两人走的最小路程是多长。

分析:

对于a和b,当两人第1次捡过垃圾之后,必定会回到垃圾桶交垃圾,之后所有捡第2个到第n个垃圾的路程为:垃圾到垃圾桶的路程*2,因为都只能从垃圾桶出发,到垃圾坐标捡取,再回到垃圾桶。

那么a和b的初始位置决定了第1次捡垃圾时应选取哪一个垃圾作为捡取对象。实际上可将所有垃圾到垃圾桶的路程*2,并求和为sum。而a和b第一次捡垃圾时路程应为:人到垃圾的路程+垃圾到垃圾桶的路程。假设a将捡取第k个垃圾,那么总路程sum应加上(a到垃圾k的路程-垃圾k到垃圾桶的路程),即为当前最短路程,即相当于此时a节约了总路程,节约量为:(a到垃圾k的路程-垃圾k到垃圾桶的路程)。

那么确定a和b分别捡哪个垃圾,即可得出最终答案。

问题在于a、b捡垃圾的状态。由于(a到垃圾k的路程-垃圾k到垃圾桶的路程)不一定小于0,即有可能a到垃圾k的距离比垃圾桶到k还远,还不如让b捡完垃圾以后从垃圾桶出发捡取垃圾k,因此a和b的状态需要讨论。

设a捡取垃圾k时能使总路程最短。

当a节约的路程(a到垃圾k的路程-垃圾k到垃圾桶的路程)< 0时,对总路程有帮助,此时让a捡取k即可。

(a到垃圾k的路程-垃圾k到垃圾桶的路程)> 0时,那么a休息即可,因为此时a继续捡取垃圾无法缩短路程,应让b继续捡取。

b同理分析,但存在特殊情况。

1.a和b均休息时,此时必须让a或b继续捡垃圾,虽然此时两人捡垃圾都只能令sum增大,但只能选取相对令sum增大较小的人捡。

2.a和b选择了相同垃圾捡取,只需计算出a能使总路程最短的垃圾ak1与次短ak2,与相应的bk1与bk2。此时ak1=bk1,那么令a选择ak2或者b选择bk2,比较哪种能令路程更短,则选择哪种。

莫名其妙写了这么长,感觉之前题解写的太简略了,这次写长一些……

不知道是不是这场cf人太少,为什么这题过的人这么少……昨晚要是熬夜就能涨分了……可惜……

#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <fstream>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define INF 0x3f3f3f3f
const int N=100005;
const int mod=1e9+7;

struct poi{
    int x,y;
}g[N],a,b,t;

double dis(poi x,poi y) {
    double temp=0;
    temp=(double)(x.x-y.x)*(x.x-y.x)+(double)(x.y-y.y)*(x.y-y.y);
    temp=sqrt(temp);
    return temp;
}

int main() {
    int n;
    while (cin>>a.x>>a.y>>b.x>>b.y>>t.x>>t.y) {
        cin>>n;
        double am,bm,ax,bx;
        am=bm=0;
        ax=bx=1e20;
        double sum=0;
        for (int i=1; i<=n; i++) {
            scanf("%d %d",&g[i].x,&g[i].y);
            sum+=2*dis(g[i], t);
        }
        int flaga,flagb;
        double fa,fb;
        flaga=flagb=0;
        for (int i=1; i<=n; i++) {
            fa=dis(g[i], a)-dis(g[i], t);
            fb=dis(g[i], b)-dis(g[i], t);
            if (fa<am) {
                ax=am;
                am=fa;
                flaga=i;
            } else if (fa<ax) {
                ax=fa;
            }
            if (fb<bm) {
                bx=bm;
                bm=fb;
                flagb=i;
            } else if (fb<bx) {
                bx=fb;
            }
        }
        double temp=0;
        if (flaga!=0&&flagb!=0) {
            if (flaga==flagb) {
                temp=min(ax+bm, am+bx);
            } else temp=am+bm;
        } else if (flagb>0||flaga>0) {
            if (flaga>0) temp+=am;
            if (flagb>0) temp+=bm;
        } else {
            temp+=min(ax, bx);
        }
        sum+=temp;
        printf("%.12lf\n",sum);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值