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

来源:互联网 发布:淘宝群控系统骗局 编辑:程序博客网 时间:2024/06/04 19:12

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 031 12 12 3
output
11.084259940083
input
5 0 4 2 2 055 23 05 53 53 3
output
33.121375178000
题意:有两个人和一堆罐子,分别在不同的位置,还有垃圾桶的位置用(x,y)坐标表示,问把所以罐子捡到垃圾桶的最短路程。

思路:我们用逆向思维,很明显总路程与罐子和垃圾桶的距离有关,我们先假设总距离为所有罐子与垃圾桶距离的两倍,我们只需挑两个或者一个罐子给这两个人去捡就行了,我们接下来的任务就是保证至少有一个人的情况的,保证总路程最短。我们要省路程,要看人和罐子的距离与罐子和垃圾桶的距离的比较。如果人和罐子的距离小,就可以省距离了,我们把距离都枚举出来,找出最省距离的罐子,但是找出最省距离的罐子还不行,如果两个人的最省距离罐子是同一个,就要找一个备胎,因此要记录最省和次省距离的罐子,当两个人的最省罐子不同,且省的距离大于0,就用总路程减去省去的距离,如果最省罐子为同一个,就选择次省的,但是次省的一定要大于0,不然我只需一个人的就行了。

#include<cstdio>#include<cmath>#include<algorithm>using namespace std;int x3, y3, x2, y2, xt, yt;int n;double ans11, ans12, ans21, ans22;int id11, id12, id21, id22;double tot;double sqr(int x) {return 1.*x*x;}double dist(int x3, int y3, int x2, int y2) {return sqrt(sqr(x3-x2) + sqr(y3-y2));}void update(double &ans1, int &id1, double &ans2, int &id2, double ans, int id) {if(id1<0 || ans<ans1) {ans2=ans1;id2=id1;ans1=ans;id1=id;} else if(id2<0 || ans<ans2) {ans2=ans;id2=id;}}int main() {scanf("%d%d%d%d%d%d", &x3, &y3, &x2, &y2, &xt, &yt);scanf("%d", &n);tot=0;id11=id12=id21=id22=-1;for(int i=0;i<n;i++) {int x, y;scanf("%d%d", &x, &y);double v = dist(x, y, xt, yt);double d1 = dist(x, y, x3, y3) - v;update(ans11, id11, ans12, id12, d1, i);double d2 = dist(x, y, x2, y2) - v;update(ans21, id21, ans22, id22, d2, i);tot+=v*2;}if(ans11<0&&ans21<0) {if(id11==id21) {tot+=min(ans11+min(0., ans22), ans21+min(0., ans12));} else tot+=ans11+ans21;} else tot+= min(ans11, ans21);printf("%.12lf\n", tot);return 0;}

总结:贪心思想,理清问题 ,找到最优方案。



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 人已故欠的公款怎么办 论文抄了表格数据怎么办 电子转账转错了怎么办 苹果手机付款方式有问题怎么办 合同中付款方式错怎么办? 优步付款方式无效怎么办 工程付款方式变更没有合同怎么办 银行账号被锁了怎么办? 街电押金退不了怎么办 佣金宝账号忘了怎么办 如果汇款汇错了怎么办 手机汇款汇错了怎么办 汇款时少了数字怎么办 打过流脑后发烧怎么办 甲醛公司除完后怎么办 发票系统导出的xml 怎么办 新买的书包味道太大怎么办 alt+a截图热键冲突怎么办 白背心领发黄了怎么办 房屋装修后出现质量问题怎么办 华为p9手机音量小怎么办 华为畅享8玩游戏卡怎么办 华为畅享7玩游戏卡怎么办 华为p9升级以后屏幕失灵怎么办 荣耀9青春版玩游戏卡怎么办 华为p9屏幕不亮了怎么办 华为p9入水黑屏怎么办 农信密码忘记了怎么办 小米4g信号差怎么办 手机的调频调制器坏了怎么办 调制解调器的灯一直闪怎么办 691宽带用户名和密码无效怎么办 房间里有狐臭味怎么办 吃冰冻水果胃特别痛怎么办 小米5s屏幕闪烁怎么办 系统重装重启了怎么办 华为p10开不了机怎么办 眼镜腿夹的头疼怎么办 摩托车车头锁坏了怎么办 夏天骑摩托车戴头盔热怎么办 头盔玻璃罩边固定老是掉怎么办