CodeForces - 672C Recycling Bottles (贪心)

CodeForces - 672C
Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u

Status

Description

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 .

Sample Input

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

Sample Output

Hint

Source

//题意:
两个人要将n个瓶子放到垃圾箱里,每个人每次只能拿一个瓶子,问他们走的最短的路是多长?
先输入A的坐标,B的坐标,垃圾箱的坐标,再输入一个n,表示马上又n个瓶子,然后是n个坐标。
//思路:
贪心&模拟,分四种情况,
1、A先取,B后取。
2、B先取,A后取
3、只有A取
4、只有B取
输出最短的就行了。
Hait:0x3f3f3f3f太小了,得重新定义。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#define INF 1e30
#define N 100010
using namespace std;
struct zz
{
	double x;
	double y;
	double la;
	double lb;
	double lt;
}p[N];
int main()
{
	double ax,ay,bx,by,tx,ty;
	int n,i,j;
	while(scanf("%lf%lf%lf%lf%lf%lf",&ax,&ay,&bx,&by,&tx,&ty)!=EOF)
	{
		double len=INF;
		int m;
		scanf("%d",&n);
		for(i=0;i<n;i++)
		{
			scanf("%lf%lf",&p[i].x,&p[i].y);
			p[i].la=sqrt((p[i].x-ax)*(p[i].x-ax)+(p[i].y-ay)*(p[i].y-ay));
			p[i].lb=sqrt((p[i].x-bx)*(p[i].x-bx)+(p[i].y-by)*(p[i].y-by));
			p[i].lt=sqrt((p[i].x-tx)*(p[i].x-tx)+(p[i].y-ty)*(p[i].y-ty));
		}
		int ma=-1,mb=-1;
		double la=-INF,lb=-INF;
		for(i=0;i<n;i++)
		{
			if(p[i].lt-p[i].la>=la)
			{
				la=p[i].lt-p[i].la;
				ma=i;
			}
		}
		for(i=0;i<n;i++)
		{
			if(i==ma)
				continue;
			if(p[i].lt-p[i].lb>=lb)
			{
				lb=p[i].lt-p[i].lb;
				mb=i;
			}
		}
		double sum1=0;
		for(i=0;i<n;i++)
		{
			if(i==ma||i==mb)
				continue;
			sum1+=2*p[i].lt;
		}
		sum1+=p[ma].la+p[mb].lb+p[ma].lt+p[mb].lt;
		
		ma=-1;mb=-1;	
		lb=-INF;la=-INF;
		for(i=0;i<n;i++)
		{
			if(p[i].lt-p[i].lb>=lb)
			{
				lb=p[i].lt-p[i].lb;
				mb=i;
			}
		}
		for(i=0;i<n;i++)
		{
			if(i==mb)
				continue;
			if(p[i].lt-p[i].la>=la)
			{
				la=p[i].lt-p[i].la;
				ma=i;
			}
		}
		double sum2=0;
		for(i=0;i<n;i++)
		{
			if(i==ma||i==mb)
				continue;
			sum2+=2*p[i].lt;
		}
		sum2+=p[ma].la+p[mb].lb+p[ma].lt+p[mb].lt;
		
		ma=-1;la=-INF;
		double sum3=0;
		for(i=0;i<n;i++)
		{
			if(p[i].lt-p[i].la>=la)
			{
				la=p[i].lt-p[i].la;
				ma=i;
			}
		}
		for(i=0;i<n;i++)
		{
			if(i==ma)
				continue;
			sum3+=2*p[i].lt;
		}
		sum3+=p[ma].la+p[ma].lt;

		mb=-1;lb=-INF;
		double sum4=0;
		for(i=0;i<n;i++)
		{
			if(p[i].lt-p[i].lb>=lb)
			{
				lb=p[i].lt-p[i].lb;
				mb=i;
			}
		}
		for(i=0;i<n;i++)
		{
			if(i==mb)
				continue;
			sum4+=2*p[i].lt;
		}
		sum4+=p[mb].lb+p[mb].lt;

		printf("%.10lf\n",min(min(sum1,sum2),min(sum3,sum4)));
	}
	return 0;
} 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值