1、动态规划配对算法
空间里有n个点P0,P1,…,Pn-1,你的任务是把它们配成n/2对(n是偶数),使得每个点恰好在一个点对中。所有点对中两点的距离之和应尽量小。
2、思路
因为是对集合进行配对,自然需要记录当前集合的状态,老方法,二进制。 dp(s) = min(dist(i, j) + dp(s-i-j)) i是s集合中最小的元素的下标(最小最大都可以,只是作为检索起点),j是s集合中其他元素的下标 s从1到1左移n再-1进行迭代,因为s-i-j必然小于s,所以直接迭代就可以,连递归都省去了。因为没有在oj上找到类似的,就百度上找到别人做的一组数据,直接copy了,暂做测试。如有问题,请大家帮忙支出。
测试数据:
Input:
20
1 2 3
1 1 1
5 6 2
4 7 8
2 3 1
1 4 7
2 5 8
3 6 9
1 2 5
2 3 6
4 5 2
7 8 5
4 5 1
-1 2 3
-1 -9 -7
0 0 0
100 0 0
9 5 1
7 5 3
5 5 5
Output:
119.076
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <map>