codeforces 672C. Recycling Bottles

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


题意:有两个人和一个垃圾桶, 还有n个瓶子, 现在要人去吧每个瓶子捡起来然后扔到垃圾箱中,求最短的距离,不要去最后人都要回到垃圾箱


思路:假设人都在垃圾箱上,那么总距离为2 * 每个瓶子到箱子的距离,现在人不在上面,所以只要求出

瓶子到箱子的距离 - 人到瓶子的距离的最大值即可再用总和减去这个即可, 注意, 肯定要有一个人先捡瓶子再回垃圾箱的,如果另一个人的最大值小于0,那么久不需要他捡了, 还有注意只有1个瓶子的情况。

(能否ac 取决于你是否细心,诶,比赛时没有考虑全面,1个多小时后被hack了哭,然后导致D题敲到一半放下去研究C结果,,,你懂得!!)


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
const int inf = 2147483647;
const double PI = acos(-1.0);
const double e = 2.718281828459;
const int mod = 1000000007;
typedef long long LL;
#pragma comment(linker, "/STACK:102400000,102400000")
//freopen("in.txt","r",stdin); //输入重定向,输入数据将从in.txt文件中读取
//freopen("out.txt","w",stdout); //输出重定向,输出数据将保存在out.txt文件中cin
struct node
{
	double x, y;
	int id;
}a[100005], b[100005];
int vis[100005];
double xx1, yy1, xx2, yy2, xx, yy;
double dis(double x1, double y1, double x2, double y2)
{
	return sqrt((x1 - x2)*(x1 - x2) + (y1 - y2) * (y1 - y2));
}
bool cmp1(node a, node b)
{
	return (dis(a.x, a.y, xx, yy) - dis(a.x, a.y, xx1, yy1)) > (dis(b.x, b.y, xx, yy) - dis(b.x, b.y, xx1, yy1));
}
bool cmp2(node a, node b)
{
	return (dis(a.x, a.y, xx, yy) - dis(a.x, a.y, xx2, yy2)) > (dis(b.x, b.y, xx, yy) - dis(b.x, b.y, xx2, yy2));
}
int main()
{
	int i, j, n;
	while (cin >> xx1 >> yy1 >> xx2 >> yy2 >> xx >> yy)
	{
		scanf("%d", &n);
		memset(vis, 0, sizeof(vis));
		double s = 0;
		for (i = 1; i <= n; ++i)
		{
			scanf("%lf%lf", &a[i].x, &a[i].y);
			a[i].id = i;
			b[i].id = i;
			b[i].x = a[i].x;
			b[i].y = a[i].y;
			s += sqrt((a[i].x - xx)*(a[i].x - xx) + (a[i].y - yy) * (a[i].y - yy)) * 2;
			
		}
		double mmax = 0;
		double a1, b1, a2, b2;
		sort(a + 1, a + 1 + n, cmp1);
	
		vis[a[1].id] = 1;
		
		a1 = dis(a[1].x, a[1].y, xx, yy) - dis(a[1].x, a[1].y, xx1, yy1);
		a2 = dis(a[2].x, a[2].y, xx, yy) - dis(a[2].x, a[2].y, xx1, yy1);
		sort(b + 1, b + 1 + n, cmp2);
		b1 = dis(b[1].x, b[1].y, xx, yy) - dis(b[1].x, b[1].y, xx2, yy2);
		b2 = dis(b[2].x, b[2].y, xx, yy) - dis(b[2].x, b[2].y, xx2, yy2);
	
		if (n == 1)
		{
			mmax = max(a1, b1);
		}
		else
		{
			if (vis[b[1].id])
			{
				mmax = max(max(a1, b2) + max(min(a1, b2), 0.0), max(b1, a2) + max(min(b1, a2), 0.0));//注意要和0比较
			}
			else
				mmax = max(a1, b1) + max(min(a1, b1), 0.0);
		}
		
		printf("%.12lf\n", s - mmax);
		
	}
}
/*
2 1 1 2 0 0
1 
1 1
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值