codeforces 672C 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 ≤ 10^9) — 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 ≤ 10^9) — 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.

 

题意:

给出两个人的初始位置和垃圾桶的位置以及那些瓶子的位置,每个人可以按上面给的步骤进行,问最后两个人走的最少的距离是多少;

 

思路:

 

把每个瓶子到垃圾桶和两个人的距离都求出来,再贪心找到最小距离;

最小距离的基础是每个瓶子到垃圾桶距离和的2倍即sum=Σdis[i],然后再选择一个人或者两个人去捡一个瓶子使sum-dis[i]+disa[i]-dis[j]+disb[j]最小,当然这是两个人都去捡的情况,还有就是一个人去捡的情况sum-dis[i]+(disa[i]ordisb[i]);

反正就是找最小的值啦;

本来思路是对的,比赛的时候代码有个地方是b的我直接把a的复制过去忘记改成b了,最后挂在了st上了;

 

AC代码:

 

#include <bits/stdc++.h>
/*#include <iostream>
#include <queue>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdio>
*/
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int N=1e5+25;
double fun(double xx,double yy,double fx,double fy)
{
    return sqrt((xx-fx)*(xx-fx)+(yy-fy)*(yy-fy));
}
double ax,ay,bx,by,tx,ty,x[N],y[N],dis[N],disa[N],disb[N];
int n;
int main()
{
    scanf("%lf%lf%lf%lf%lf%lf",&ax,&ay,&bx,&by,&tx,&ty);
    scanf("%d",&n);
    double ans=0;
    Riep(n)
    {
        scanf("%lf%lf",&x[i],&y[i]);
        dis[i]=fun(x[i],y[i],tx,ty);
        disa[i]=fun(x[i],y[i],ax,ay);
        disb[i]=fun(x[i],y[i],bx,by);
        ans+=2*dis[i];
    }
    disa[0]=0;
    disb[0]=0;
    dis[0]=0;
    double mmina=999999999999999,mminb=99999999999999,s=ans;
    int posa=0,posb=0;
    for(int i=1;i<=n;i++)
    {
        if(disa[i]-dis[i]<mmina)
        {
            mmina=disa[i]-dis[i];
            posa=i;
        }
        if(disb[i]-dis[i]<mminb)
        {
            mminb=disb[i]-dis[i];
            posb=i;
        }
    }
    if(mmina<0&&mminb<0)
    {
        if(posa==posb)
        {
            for(int i=0;i<=n;i++)
            {
                if(i!=posa)
                s=min(s,ans-dis[posa]+disa[posa]-dis[i]+disb[i]);
            }
            for(int i=0;i<=n;i++)
            {
                if(i!=posb)
                    s=min(s,ans-dis[posb]+disb[posb]-dis[i]+disa[i]);
            }
        }
        else
        {
            s=ans+mmina+mminb;
        }
    }
    else
    {
        if(mmina<mminb)
        {
            s=ans-dis[posa]+disa[posa];
        }
        else
        {
            s=ans-dis[posb]+disb[posb];
        }
    }
printf("%.10lf\n",s);
    return 0;
}

 

 

转载于:https://www.cnblogs.com/zhangchengc919/p/5484544.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,Codeforces Round 511 (Div. 1)是一个比赛的名称。然而,引用内容中没有提供与这个比赛相关的具体信息或问题。因此,我无法回答关于Codeforces Round 511 (Div. 1)的问题。如果您有关于这个比赛的具体问题,请提供更多的信息,我将尽力回答。 #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces Round 867 (Div. 3)(A题到E题)](https://blog.csdn.net/wdgkd/article/details/130370975)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值