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
和
思路:因为要求最短距离,所以说
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;
}