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:
- Choose to stop or to continue to collect bottles.
- If the choice was to continue then choose some bottle and walk towards it.
- Pick this bottle and walk to the recycling bin.
- 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.
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.
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 .
3 1 1 2 0 0 3 1 1 2 1 2 3
11.084259940083
5 0 4 2 2 0 5 5 2 3 0 5 5 3 5 3 3
33.121375178000
思路:我们用逆向思维,很明显总路程与罐子和垃圾桶的距离有关,我们先假设总距离为所有罐子与垃圾桶距离的两倍,我们只需挑两个或者一个罐子给这两个人去捡就行了,我们接下来的任务就是保证至少有一个人的情况的,保证总路程最短。我们要省路程,要看人和罐子的距离与罐子和垃圾桶的距离的比较。如果人和罐子的距离小,就可以省距离了,我们把距离都枚举出来,找出最省距离的罐子,但是找出最省距离的罐子还不行,如果两个人的最省距离罐子是同一个,就要找一个备胎,因此要记录最省和次省距离的罐子,当两个人的最省罐子不同,且省的距离大于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; }
总结:贪心思想,理清问题 ,找到最优方案。