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:

Choose to stop or to continue to collect bottles.
If the choice was to continue then choose some bottle and walk towards it.
Pick this bottle and walk to the recycling bin.
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 ax, ay, bx, by, tx 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

题意:有两个人 A B,还有一个垃圾桶和 n 个瓶子,需要将所有的瓶子都放到垃圾桶中,每次只能拿一个瓶子,给出各点坐标,求A B 行走的最短距离。

思路:因为要求最短距离,所以说A B 从所在地出来之后就不能回去了,所以说,我们处理出每个瓶子和A B 还有垃圾桶的距离,然后找出出发点(A B <script id="MathJax-Element-1631" type="math/tex">B</script>),然后出发之后,捡一个瓶子的距离就是瓶子到垃圾桶的距离的2倍,也可以有两个出发点。找出两个最优的出发点,这个出发点到一个瓶子的距离小于瓶子到垃圾桶的距离,如果没有这样的点,那么就重新选取最优点,但一个瓶子的距离不会小于瓶子到垃圾桶的距离。

ac代码:

/* ***********************************************
Author       : AnICoo1
Created Time : 2016-08-24-10.57 Wednesday
File Name    : D:\MyCode\2016-8月\2016-8-24.cpp
LANGUAGE     : C++
Copyright  2016 clh All Rights Reserved
************************************************ */
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define LL long long
#define ll __int64
#define mem(x,y) memset(x,(y),sizeof(x))
#define PI acos(-1)
#define gn (sqrt(5.0)+1)/2
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
const ll INF=1e18+10;
const int MAXN=1e6+10;
const int MOD=1e9+7;
//head

struct point
{
    double x,y;
}p[MAXN],a,b,c;
double dis_c[MAXN],dis_a[MAXN],dis_b[MAXN];//瓶子和三个点的距离
int vis[MAXN];//标记瓶子

double Dis(point A,point B)
{
    return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
}



int main()
{
    cin>>a.x>>a.y>>b.x>>b.y>>c.x>>c.y;
    int n;cin>>n;mem(vis,0);
    for(int i=1;i<=n;i++) cin>>p[i].x>>p[i].y;
    for(int i=1;i<=n;i++)
    {
        dis_a[i]=Dis(a,p[i]);
        dis_b[i]=Dis(b,p[i]);
        dis_c[i]=Dis(c,p[i]);
    }



    double ans=0.0;
    //找出第一个出发点
    double M2=0.0;int k2=-1,flag=0;
    for(int i=1;i<=n;i++)
    {
        double disa=dis_c[i]-dis_a[i];
        if(disa>M2) M2=disa,k2=i,flag=1;
        double disb=dis_c[i]-dis_b[i];
        if(disb>M2) M2=disb,k2=i,flag=-1;
    }
    if(k2!=-1)
    {
        if(flag>0)
            ans+=(dis_a[k2]+dis_c[k2]);
        else if(flag<0)
            ans+=(dis_b[k2]+dis_c[k2]);
        vis[k2]=1;
    }


    //找出第二个出发点
    int k1=-1;
    if(flag>0)
    {
        double M1=0.0;k1=-1;
        for(int i=1;i<=n;i++)
        {
            if(vis[i]) continue;
            double dis=dis_c[i]-dis_b[i];
            if(dis>M1)
                M1=dis,k1=i;
        }
        if(k1!=-1)
            ans+=(dis_b[k1]+dis_c[k1]),vis[k1]=1;
    }
    else if(flag<0)
    {
        double M1=0.0;k1=-1;
        for(int i=1;i<=n;i++)
        {
            if(vis[i]) continue;
            double dis=dis_c[i]-dis_a[i];
            if(dis>M1)
                M1=dis,k1=i;
        }
        if(k1!=-1)
            ans+=(dis_a[k1]+dis_c[k1]),vis[k1]=1;
    }

    //找出了两个出发点,检查两个出发点的位置是A还是B,取两种方案最优
    double Ans=0.0;
    swap(k1,k2);
    if(flag>0)
        Ans=dis_a[k2]+dis_c[k2]+dis_b[k1]+dis_c[k1];
    else
        Ans=dis_b[k2]+dis_c[k2]+dis_a[k1]+dis_c[k1];
    ans=min(ans,Ans);

    //如果没有符合条件的出发点,那么只能选择最优的一个出发点
    if(k1==-1&&k2==-1)
    {
        int k=0,bz=0;double M=-INF*1.0;
        for(int i=1;i<=n;i++)
        {
            double disa=dis_c[i]-dis_a[i];
            if(disa>M) M=disa,k=i,bz=1;
            double disb=dis_c[i]-dis_b[i];
            if(disb>M) M=disb,k=i,bz=-1;
        }
        vis[k]=1;
        if(bz>0)  ans+=(dis_a[k]+dis_c[k]);
        else if(bz<0) ans+=(dis_b[k]+dis_c[k]);
    }
    for(int i=1;i<=n;i++)
    {
        if(vis[i]) continue;
        ans+=dis_c[i]*2.0;
    }
    printf("%.7lf\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值