codeforces 672C Recycling Bottles (枚举+贪心)

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

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.


题目链接:http://codeforces.com/problemset/problem/672/C


题目大意:有两个人独立的捡垃圾,求他们一起捡完所有垃圾所走的最短路程,从初始点找到第一个垃圾,然后送到垃圾桶,再从垃圾桶出发捡垃圾再送回,人,垃圾,垃圾桶都在二维坐标系中

题目分析:设垃圾i到垃圾桶的距离为di,人到垃圾i的距离为si,若该垃圾是从人的出发点去捡则路程为di+si,若从垃圾桶出发则为2*di,则对于垃圾i可以节省的路程为2*di-(di+si) = di-si,也就是说,我们要选择di-si最大的那个垃圾作为人第一次去捡的垃圾,然后对于两个人枚举4种情况,先A后B,先B后A,全A,全B,只需要计算第一次选择的垃圾,后面捡垃圾的路程都是2*dj

#include 
#include 
#include 
#include 
using namespace std;
double const INF = 1e30;
int const MAX = 1e5 + 5;

struct POINT
{
    double x, y;
}p[MAX], a, b, t;
bool vis[MAX];
double dis[MAX];
int n;

double cal_d(POINT a, POINT b)
{
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

double get_val(POINT st)
{
    double tmp = -INF, res = 0;
    int pos = 0;
    for(int i = 0; i < n; i++)
    {
        double dif = dis[i] - cal_d(st, p[i]);
        if(!vis[i] && dif >= tmp)
        {
            res = cal_d(st, p[i]);
            tmp = dif;
            pos = i;
        }
    }
    vis[pos] = true;
    return res;
}

int main()
{
    scanf("%lf %lf %lf %lf %lf %lf", &a.x, &a.y, &b.x, &b.y, &t.x, &t.y);
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
        scanf("%lf %lf", &p[i].x, &p[i].y);
    for(int i = 0; i < n; i++)
        dis[i] = cal_d(p[i], t);
    double ans1, ans2, ans3, ans4;
    ans1 = get_val(a) + get_val(b);
    for(int i = 0; i < n; i++)
        ans1 += vis[i] ? dis[i] : dis[i] * 2.0;
    memset(vis, false, sizeof(vis));
    ans2 = get_val(b) + get_val(a);
    for(int i = 0; i < n; i++)
        ans2 += vis[i] ? dis[i] : dis[i] * 2.0;
    memset(vis, false, sizeof(vis));
    ans3 = get_val(a);
    for(int i = 0; i < n; i++)
        ans3 += vis[i] ? dis[i] : dis[i] * 2.0;
    memset(vis, false, sizeof(vis));
    ans4 = get_val(b);
    for(int i = 0; i < n; i++)
        ans4 += vis[i] ? dis[i] : dis[i] * 2.0;
    printf("%.10f\n", min(min(ans1, ans2), min(ans3, ans4)));
}



你可能感兴趣的:(ACM,贪心)