CF之Recycling Bottles

本文介绍了一个典型的计算几何问题,通过优化路径以最小化两角色在收集并回收瓶子过程中的总行走距离。通过代码示例详细解释了算法实现,并讨论了其中的潜在bug。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

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 .


解答:
典型的计算几何题目,可以发现实际总路径是很2 * sum(dis) - balabala,所以直接看代码就行了
#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;
}

其实这段代码中是有bug的,不知道你有没有发现呀,说明CF的测试数据并不够全啊。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值