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

1 1
2 1
2 3
output
11.084259940083
input
5 0 4 2 2 0

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.

题意:

C题题意:二维平面上有n个(n<=10^5)点(xi,yi),有两个起点A(ax,ay)和B(bx,by),一个终点T(tx,ty)(0<=横纵坐标<=10^9)

     可以从两个起点中的一个或两个出发,经过所有(xi,yi),且每到达一个(xi,yi)都要回到终点,问所有走过距离的最小值

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-;
const int inf =0x7f7f7f7f;
const double pi=acos(-);
const int maxn=+; struct point{
double x,y;
}p[maxn];
point a,b,o;
int a1,a2,b1,b2;
double l[maxn+],mina,minb,seca,secb,ca1,ca2,cb1,cb2; double dis(point a,point b)
{
return pow((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y),0.5);
} void solvea(int i)
{
if(l[i]-dis(a,p[i])>mina)
{
seca=mina;
ca2=ca1;
mina=l[i]-dis(a,p[i]);
ca1=i;
}
else if(l[i]-dis(a,p[i])>seca)
{
seca=l[i]-dis(a,p[i]);
ca2=i;
}
} void solveb(int i)
{
if(l[i]-dis(b,p[i])>minb)
{
secb=minb;
cb2=cb1;
minb=l[i]-dis(b,p[i]);
cb1=i;
}
else if(l[i]-dis(b,p[i])>secb)
{
secb=l[i]-dis(b,p[i]);
cb2=i;
}
} int main()
{
while(~scanf("%lf %lf %lf %lf %lf %lf",&a.x,&a.y,&b.x,&b.y,&o.x,&o.y))
{
mina=minb=seca=secb=-1e20;
ca1=,ca2=,cb1=,cb2=;
int n;double ans=; scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%lf %lf",&p[i].x,&p[i].y);
l[i]=dis(p[i],o);
ans+=*l[i];
solvea(i);
solveb(i);
} if(ca1!=cb1)
{
if(mina<||minb<)
ans-=max(mina,minb);//刚开始没有加这句就wa了,因为即使两人最优的瓶子不是同
//一个,却可能存在其中有人的收益为负的情况,这种情况下就应该取两者中收益较大的一个(满足至少一个,可以///只有一个)
else
ans-=mina+minb;
}
else
{
if(mina<||minb<)
ans-=max(mina,minb);
else
{
if(mina+max(secb,0.0)>minb+max(seca,0.0))
ans-=mina+max(secb,0.0);
else ans-=minb+max(seca,0.0);
}
}
printf("%.16f\n",ans);
}
return ;
}

分析:用图论里的最短路和次短路的思想记录下最短路和次短路就好,,其实写的有点挫,可以直接用pair<double,int>再sort排序就好,,最关键的是,,要分情况讨论,因为两个人至少有一个要去捡,也就是说可以只有一个,所以应分情况讨论,错误点见代码

CF 672C 两个人捡瓶子 最短路与次短路思想的更多相关文章

  1. 最短路和次短路问题,dijkstra算法

    /*  *题目大意:  *在一个有向图中,求从s到t两个点之间的最短路和比最短路长1的次短路的条数之和;  *  *算法思想:  *用A*求第K短路,目测会超时,直接在dijkstra算法上求次短路; ...

  2. UESTC30-最短路-Floyd最短路、spfa+链式前向星建图

    最短路 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 在每年的校赛里,所有进入决赛的同 ...

  3. hdu1688(dijkstra求最短路和次短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1688 题意:第k短路,这里要求的是第1短路(即最短路),第2短路(即次短路),以及路径条数,最后如果最 ...

  4. POJ 3463 Sightseeing 【最短路与次短路】

    题目 Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every day the ...

  5. POJ - 3463 Sightseeing 最短路计数+次短路计数

    F - Sightseeing 传送门: POJ - 3463 分析 一句话题意:给你一个有向图,可能有重边,让你求从s到t最短路的条数,如果次短路的长度比最短路的长度多1,那么在加上次短路的条数. ...

  6. poj 3463 Sightseeing( 最短路与次短路)

    http://poj.org/problem?id=3463 Sightseeing Time Limit: 2000MS   Memory Limit: 65536K Total Submissio ...

  7. POJ---3463 Sightseeing 记录最短路和次短路的条数

    Sightseeing Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9247   Accepted: 3242 Descr ...

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

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

  9. CF 586B 起点到终点的最短路和次短路之和

    起点是右下角  终点是左上角 每次数据都是两行的点  输入n 表示有n列 接下来来的2行是 列与列之间的距离 最后一行是  行之间的距离 枚举就行   Sample test(s) input 41 ...

随机推荐

  1. 关于maven自动部署tomcat9 步骤

    maven 自动部署tomcat9 (远程方法) 1.首先要去配置用户,在tomcat的conf中有tomcat_users.xml,在其中有tomcat-user的配置 配置:<tommcat ...

  2. 洛谷P1192台阶问题

    题目描述 有NN级的台阶,你一开始在底部,每次可以向上迈最多KK级台阶(最少11级),问到达第NN级台阶有多少种不同方式. 输入格式 两个正整数N,K. 输出格式 一个正整数,为不同方式数,由于答案可 ...

  3. EXKMP模版

    这道题目折腾了我好一会啊,出于尊重我要先放我们师兄的博客 1178: [视频]EXKMP模版:最长共同前缀长度 时间限制: 1 Sec  内存限制: 128 MB提交: 180  解决: 123[提交 ...

  4. HTTPS原理(三次握手)

    第一步: 客户端向服务器发送HTTPS请求,服务器将公钥以证书的形式发送到客户端(服务器端存放私钥和公钥). 第二步: 浏览器生成一串随机数,然后用公钥对随机数和hash签名进行加密,加密后发送给服务 ...

  5. 测试必知150个常用Linux命令,已为各位筛选整理

    ●线上查询及帮助命令(1 个) help   如:mkdir --help   ●文件和目录操作命令(12 个)    ls tree pwd mkdir rmdir cd touch cp mv r ...

  6. USB键盘驱动分析

    简介 本文介绍USB驱动程序编写的流程,分析一个键盘的USB程序,基于linux-2.6.39 USB驱动概要 分层 主机层面的USB驱动的整体架构可以分成4层,自顶到下依次是 1.USB设备驱动:本 ...

  7. shiro登陆流程

    登录请求被FormAuthenticationFilter拦截 FormAuthenticationFilter会执行其父类AdviceFilter的doFilterInternal方法 其代码如下: ...

  8. Abp添加新的接口(扩展底层接口)

    在https://aspnetboilerplate.com/Templates 创建项目之后,下载用Vs2019打开(vs2017不支持netcore3.0)结构如下: 一. 2. 在xx.core ...

  9. katalon设置Android SDK路径

    本文链接:https://blog.csdn.net/feiniao8651/article/details/82809147文章允许转载,请注明来源:https://blog.csdn.net/fe ...

  10. UITableViewCell背景色.选中背景色,分割线,字体颜色设置

    1.系统默认的颜色设置 //无色 cell.selectionStyle = UITableViewCellSelectionStyleNone; //蓝色 cell.selectionStyle = ...