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 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

Copy
3 1 1 2 0 0
3
1 1
2 1
2 3
Output

Copy
11.084259940083
Input

Copy
5 0 4 2 2 0
5
5 2
3 0
5 5
3 5
3 3
Output

Copy
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.

题意:

两个人捡瓶子,分别从原位置出发,捡到一个后返回垃圾箱处放垃圾,两人独立;

问最后距离之和的 min;

令最开始的距离为 sum = ∑2*dist [ i ];

两人可以同时到一个点,那么距离就是 sum - dist [ i ] + disa [ i ] + disb [ i ]-dist [ i ];

当然也可以一个人去,那么就是 sum - dist [ i ] + ( disa [ i ] || disb[ i ] );

每次维护一个最小值即可;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 200005
#define inf 0x3f3f3f3f
#define INF 9999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ ll qpow(ll a, ll b, ll c) {
ll ans = 1;
a = a % c;
while (b) {
if (b % 2)ans = ans * a%c;
b /= 2; a = a * a%c;
}
return ans;
} double ax, ay, bx, by, tx, ty;
struct node {
double x, y;
}indx[maxn]; double dis(double a, double b, double x, double y) {
return sqrt(1.0*(a - x)*(a - x) + 1.0*(b - y)*(b - y))*1.0;
} double dist[maxn], disa[maxn], disb[maxn]; int main()
{
//ios::sync_with_stdio(0);
rdlf(ax); rdlf(ay); rdlf(bx); rdlf(by); rdlf(tx); rdlf(ty);
int n; rdint(n);
double ans = 0.0;
for (int i = 1; i <= n; i++) {
rdlf(indx[i].x), rdlf(indx[i].y);
dist[i] = 1.0*dis(indx[i].x, indx[i].y, tx, ty);
disa[i] = 1.0*dis(ax, ay, indx[i].x, indx[i].y);
disb[i] = 1.0*dis(bx, by, indx[i].x, indx[i].y);
ans += 2.0*dist[i];
}
double Max = INF * 1.0;
double Maxx = INF * 1.0;
int posa, posb; for (int i = 1; i <= n; i++) {
if (Max > disa[i] - dist[i]) {
Max =1.0* disa[i] - 1.0*dist[i]; posa = i;
}
if (Maxx > disb[i] - dist[i]) {
Maxx = 1.0*disb[i] - 1.0*dist[i]; posb = i;
}
}
// cout << posa << ' ' << posb << endl;
double sum = ans;
if (Maxx < 0 && Max < 0) {
if (posa != posb) {
sum = ans + Maxx * 1.0 + Max * 1.0;
}
else {
for (int i = 0; i <= n; i++) {
if (i != posa) {
sum = min(sum, ans - dist[posa] + disa[posa] - dist[i] + disb[i]);
}
}
for (int i = 0; i <= n; i++) {
if (i != posb) {
sum = min(sum, ans - dist[posb] + disb[posb] - dist[i] + disa[i]);
}
}
}
}
else {
if (Max < Maxx) {
sum = ans + disa[posa] - dist[posa];
}
else {
sum = ans + disb[posb] - dist[posb];
}
}
printf("%.9lf\n", 1.0*sum);
return 0;
}

CF671A Recycling Bottles 计算几何的更多相关文章

  1. codeforces 672C C. Recycling Bottles(计算几何)

    题目链接: C. Recycling Bottles time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  2. CF 672C Recycling Bottles[最优次优 贪心]

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

  3. Codeforces Round #352 (Div. 2) C. Recycling Bottles 贪心

    C. Recycling Bottles   It was recycling day in Kekoland. To celebrate it Adil and Bera went to Centr ...

  4. codeforces 352 div 2 C.Recycling Bottles 贪心

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

  5. Codeforces Recycling Bottles 模拟

    C. Recycling Bottles time limit per test: 2 seconds memory limit per test: 256 megabytes input: stan ...

  6. Codeforces Round #352 (Div. 1) A. Recycling Bottles 暴力

    A. Recycling Bottles 题目连接: http://www.codeforces.com/contest/671/problem/A Description It was recycl ...

  7. Codeforces 671 A——Recycling Bottles——————【思维题】

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

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

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

  9. 【18.69%】【codeforces 672C】Recycling Bottles

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

随机推荐

  1. PD中设置外键约束名称生成规则

    选择Database—>Edit Current DBMS选择Scripts->Objects->Reference->ConstName可以发现右侧的Value为: FK_% ...

  2. C++深度解析教程学习笔记(2)C++中的引用

    1.C++中的引用 (1)变量名的回顾 ①变量是一段实际连续存储空间的别名,程序中通过变量来申请并命名存储空间 ②通过变量的名字可以使用存储空间.(变量的名字就是变量的值,&变量名是取地址操作 ...

  3. java执行linux命令的工具类

    package com.starfast.common.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ja ...

  4. 剑指offer 39_二叉树的深度

    #include <stdio.h> #include <malloc.h> typedef int Item; typedef struct node{ Item m_val ...

  5. 使用线程池优化Echo模型

    在上一篇文章中 http://www.cnblogs.com/gosaint/p/8492356.html         我阐述了使用线程为每一个客户端创建一个工作线程来负责任务的执行.但是会存在如 ...

  6. 第2章 netty介绍与相关基础知识

    NIO有一个零拷贝的特性.Java的内存有分为堆和栈,以及还有字符串常量池等等.如果有一些数据需要从IO里面读取并且放到堆里面,中间其实会经过一些缓冲区.我们要去读,它会分成两个步骤,第一块它会把我们 ...

  7. css3(border-radius)边框圆角详解(转)

    css3(border-radius)边框圆角详解 (2014-05-19 16:16:29) 转载▼ 标签: divcss html it css3 分类: 网页技术 传统的圆角生成方案,必须使用多 ...

  8. Python-黑客-004 用Python构建一个SSH僵尸网络-02 手动与SSH交互

    用Python构建一个SSH僵尸网络-02 手动与SSH交互 - 登录SSH服务器端的 root 用户 我的电脑(攻击者)的系统:Ubuntu14.04 : 用户名: aobosir@ubuntu:~ ...

  9. Evil Book -- CodeChef

    传送门 分析 对于这道题,我们首先思考一个贪心策略,即对于所有我们要打败的厨师我们肯定可以先打败需使用帮助次数少的厨师再打败需使用帮助次数多的厨师 ,因为这样可以使得能支付得起帮助费用的可能性尽可能的 ...

  10. A - Dictionary

    传送门 题目大意 给你n个字符串,问是否可以通过改变26个字母的排列顺序是这n个字符串的字典序是非降排列的. 分析 我们考虑设相邻两个字符串的第一个不相同字符的位置为j,以为要求字典序不降,所以有第i ...